Skip to content

Instantly share code, notes, and snippets.

@rubyist
Created November 2, 2013 21:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rubyist/9e3c089142945c27867e to your computer and use it in GitHub Desktop.
Save rubyist/9e3c089142945c27867e to your computer and use it in GitHub Desktop.
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.")
}
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