Created
July 15, 2013 04:10
-
-
Save AlexMocioi/5997447 to your computer and use it in GitHub Desktop.
Describe how a http handler is treated in server.go
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
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