Skip to content

Instantly share code, notes, and snippets.

@jasonkeene
Last active April 21, 2018 17:04
Show Gist options
  • Save jasonkeene/1f288e4f69a7b9152367530b82590601 to your computer and use it in GitHub Desktop.
Save jasonkeene/1f288e4f69a7b9152367530b82590601 to your computer and use it in GitHub Desktop.
I/O Blocking in http.Handler
package main
import (
"log"
"net"
)
func main() {
println("dialing")
conn, err := net.Dial("tcp", "localhost:12345")
if err != nil {
log.Fatal(err)
}
println("sending GET request")
_, err = conn.Write([]byte("GET / HTTP/1.1\r\n"))
if err != nil {
log.Fatal(err)
}
_, err = conn.Write([]byte("Host: localhost\r\n\r\n"))
if err != nil {
log.Fatal(err)
}
println("blocking and never reading")
select {}
}
package main
import (
"net/http"
"strings"
)
func main() {
h := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
println("before write")
for i := 0; i < 700; i++ {
rw.Write([]byte(strings.Repeat("x", 1024) + "\r\n"))
}
println("after write")
})
http.ListenAndServe(":12345", h)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment