From ada7214469f116bbc659ef02c1e6facdcc019a4d Mon Sep 17 00:00:00 2001 From: "kali kaneko (leap communications)" Date: Tue, 21 Sep 2021 23:17:18 +0200 Subject: add timeouts see https://ieftimov.com/post/make-resilient-golang-net-http-servers-using-timeouts-deadlines-context-cancellation/ --- main.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index dc6cfdf..811e737 100644 --- a/main.go +++ b/main.go @@ -271,11 +271,20 @@ func main() { log.Println("Started Geolocation Service") log.Printf("Listening on port %v...\n", *port) - pstr := ":" + strconv.Itoa(*port) + addr := ":" + strconv.Itoa(*port) + s := &http.Server{ + Addr: addr, + Handler: mux, + ReadTimeout: 1 * time.Second, + WriteTimeout: 1 * time.Second, + IdleTimeout: 30 * time.Second, + ReadHeaderTimeout: 2 * time.Second, + MaxHeaderBytes: 1 << 20, + } if *notls == true { - err = http.ListenAndServe(pstr, mux) + err = s.ListenAndServe() } else { - err = http.ListenAndServeTLS(pstr, *crt, *key, mux) + err = s.ListenAndServeTLS(*crt, *key) } if err != nil { -- cgit v1.2.3