Skip to content

Instantly share code, notes, and snippets.

@AlexMocioi
Created July 15, 2013 04:10
Show Gist options
  • Save AlexMocioi/5997447 to your computer and use it in GitHub Desktop.
Save AlexMocioi/5997447 to your computer and use it in GitHub Desktop.
Describe how a http handler is treated in server.go
func (h *timeoutHandler) ServeHTTP(w ResponseWriter, r *Request) {
done := make(chan bool, 1)
tw := &timeoutWriter{w: w}
go func() {
h.handler.ServeHTTP(tw, r)
done <- true
}()
select {
case <-done:
return
case <-h.timeout():
tw.mu.Lock()
defer tw.mu.Unlock()
if !tw.wroteHeader {
tw.w.WriteHeader(StatusServiceUnavailable)
tw.w.Write([]byte(h.errorBody()))
}
tw.timedOut = true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment