From c0e16386d9a54c5900062b7980913e3d695ec420 Mon Sep 17 00:00:00 2001 From: Bill Mill Date: Mon, 20 Jun 2022 09:28:45 -0400 Subject: [PATCH] handle timeouts --- server/main.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/server/main.go b/server/main.go index 0430e8b..3763acc 100644 --- a/server/main.go +++ b/server/main.go @@ -9,7 +9,6 @@ // // * scan for links as specified in the spec // * implement event logs -// * thread context through for timeouts package main @@ -101,14 +100,23 @@ func main() { db := initDB() server := newSpring83Server(db) - http.HandleFunc("/", server.RootHandler) host := getenv("HOST", "") port := getenv("PORT", "8000") addr := fmt.Sprintf("%s:%s", host, port) + timeoutMsg := "Request timed out" + + srv := &http.Server{ + Addr: addr, + ReadTimeout: 1 * time.Second, + WriteTimeout: 1 * time.Second, + IdleTimeout: 30 * time.Second, + ReadHeaderTimeout: 2 * time.Second, + Handler: http.TimeoutHandler(server, 2*time.Second, timeoutMsg), + } log.Printf("starting helloserver on %s", addr) - log.Fatal(http.ListenAndServe(addr, nil)) + log.Fatal(srv.ListenAndServe()) } func readTemplate(name string, fsys fs.FS) (string, error) { @@ -584,7 +592,7 @@ func (s *Spring83Server) Options(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNoContent) } -func (s *Spring83Server) RootHandler(w http.ResponseWriter, r *http.Request) { +func (s *Spring83Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { if r.Method == "OPTIONS" { s.Options(w, r) } else if r.Method == "PUT" {