Meet the 500ms timeout

* First, lower the MaxResponseTime which is used
  in the Handler to cancel the running goroutines
  and finalize the result. Since we want to stay below 500ms
  we cannot pick 500ms, because the finalization steps
  take some time too.
* Second, set WriteTimeout to 500ms in order to make sure
  the handler never exceeds that timeframe.
This commit is contained in:
Julian Ospald 2017-09-11 12:12:23 +02:00
parent c76b5266e2
commit 771f7c6892

View File

@ -20,18 +20,21 @@ type Numbers struct {
}
// The maximum response time of the handlers.
var MaxResponseTime time.Duration = 500 * time.Millisecond
var MaxResponseTime time.Duration = 450 * time.Millisecond
// The main entry point of the backend.
func main() {
listenAddr := flag.String("http.addr", ":8090", "http listen address")
flag.Parse()
http.HandleFunc("/numbers", func(w http.ResponseWriter, r *http.Request) {
numbersHandler(w, r)
})
log.Fatal(http.ListenAndServe(*listenAddr, nil))
s := &http.Server{
Addr: ":8090",
ReadTimeout: 2 * time.Second,
WriteTimeout: 500 * time.Millisecond,
}
log.Fatal(s.ListenAndServe())
}
// The main handler. The expected request is of the form: