-
-
Save rubyist/9e3c089142945c27867e to your computer and use it in GitHub Desktop.
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 ( | |
"fmt" | |
"io" | |
"net/http" | |
"os" | |
) | |
func doReq() io.ReadCloser { | |
resp, err := http.Get("http://localhost:48567/hello") | |
if err != nil { | |
fmt.Print(err) | |
os.Exit(1) | |
} | |
return resp.Body | |
} | |
func doit() { | |
resp := doReq() | |
defer resp.Close() | |
b := make([]byte, 1024) | |
_, err := resp.Read(b) | |
if err != nil { | |
fmt.Print(err) | |
} | |
} | |
func main() { | |
// Do it once, there's just an error | |
doit() | |
// Do it a second time (even in a different process), it blows the server up | |
doit() | |
fmt.Println("Boom.") | |
} |
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 ( | |
"fmt" | |
"github.com/daaku/go.grace/gracehttp" | |
"io" | |
"net/http" | |
"os" | |
) | |
func main() { | |
mux := http.NewServeMux() | |
mux.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) { | |
fmt.Println("got it") | |
f, _ := os.Open("/dev/random") | |
// The key is that this doesn't finish before the connection is closed | |
io.Copy(w, f) | |
}) | |
gracehttp.Serve( | |
&http.Server{Addr: ":48567", Handler: mux}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment