From d081a79f6d13c5bae09ae80e6b79c341baf7751b Mon Sep 17 00:00:00 2001 From: surtur Date: Wed, 19 Jul 2023 22:35:42 +0200 Subject: [PATCH] go: add server read/write timeouts --- main.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 34ed110..a148d17 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "io/fs" "log" "net/http" + "time" ) var version = "development" @@ -25,7 +26,15 @@ func main() { log.Printf("app built from revision '%s'\n", version) log.Print("Listening on :1314...") - err = http.ListenAndServe(":1314", nil) + // https://blog.cloudflare.com/the-complete-guide-to-golang-net-http-timeouts/ + srv := http.Server{ + ReadTimeout: 15 * time.Second, + WriteTimeout: 15 * time.Second, + Addr: ":1314", + Handler: nil, + } + + err = srv.ListenAndServe() if err != nil { log.Fatal(err) }