From 9e728d3e9c17e6735410ac1f5682d2ff47ce4833 Mon Sep 17 00:00:00 2001 From: Joakim Bygdell Date: Sun, 6 Nov 2022 10:13:42 +0100 Subject: [PATCH] Start https server with defined timeout values --- healthchecks.go | 10 +++++++++- main.go | 17 +++++++++++++---- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/healthchecks.go b/healthchecks.go index 62e2c15..4b1bb9a 100644 --- a/healthchecks.go +++ b/healthchecks.go @@ -44,7 +44,15 @@ func (h *HealthCheck) RunHealthChecks() { health.AddReadinessCheck("broker-tcp", healthcheck.TCPDialCheck(h.brokerURL, 50*time.Millisecond)) addr := ":" + strconv.Itoa(h.port) - if err := http.ListenAndServe(addr, health); err != nil { + server := &http.Server{ + Addr: addr, + Handler: health, + ReadTimeout: 5 * time.Second, + WriteTimeout: 5 * time.Second, + IdleTimeout: 30 * time.Second, + ReadHeaderTimeout: 3 * time.Second, + } + if err := server.ListenAndServe(); err != nil { panic(err) } } diff --git a/main.go b/main.go index 20d0eea..9e25f66 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "net/http" + "time" log "github.com/sirupsen/logrus" ) @@ -58,13 +59,21 @@ func main() { hc := NewHealthCheck(8001, config.S3, config.Broker, tlsProxy) go hc.RunHealthChecks() + server := &http.Server{ + Addr: ":8000", + ReadTimeout: 5 * time.Second, + WriteTimeout: 5 * time.Second, + IdleTimeout: 30 * time.Second, + ReadHeaderTimeout: 3 * time.Second, + } + if config.Server.cert != "" && config.Server.key != "" { - if e := http.ListenAndServeTLS(":8000", config.Server.cert, config.Server.key, nil); e != nil { - panic(e) + if err := server.ListenAndServeTLS(config.Server.cert, config.Server.key); err != nil { + panic(err) } } else { - if e := http.ListenAndServe(":8000", nil); e != nil { - panic(e) + if err := server.ListenAndServe(); err != nil { + panic(err) } } }