Last active
April 21, 2018 17:04
-
-
Save jasonkeene/1f288e4f69a7b9152367530b82590601 to your computer and use it in GitHub Desktop.
I/O Blocking in http.Handler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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