From 909eeff406fae900ae938cbb5fd59ca2797a2f46 Mon Sep 17 00:00:00 2001 From: Jannis Mattheis Date: Sat, 1 Aug 2020 15:32:25 +0200 Subject: [PATCH] Make keepalive period configurable --- api/stream/stream_test.go | 2 +- config.example.yml | 1 + config/config.go | 8 +++++--- runner/runner.go | 21 ++++++++++++++++++--- 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/api/stream/stream_test.go b/api/stream/stream_test.go index 41a6bd3..7b6330c 100644 --- a/api/stream/stream_test.go +++ b/api/stream/stream_test.go @@ -329,7 +329,7 @@ func TestMultipleClients(t *testing.T) { userIDs := []uint{1, 1, 1, 2, 2, 3} i := 0 server, api := bootTestServer(func(context *gin.Context) { - auth.RegisterAuthentication(context, nil, userIDs[i], "t"+string(userIDs[i])) + auth.RegisterAuthentication(context, nil, userIDs[i], "t"+fmt.Sprint(userIDs[i])) i++ }) defer server.Close() diff --git a/config.example.yml b/config.example.yml index 7192f9e..1292fc8 100644 --- a/config.example.yml +++ b/config.example.yml @@ -2,6 +2,7 @@ # Save it to `config.yml` when edited server: + keepaliveperiodseconds: 0 # 0 = use system default; set the interval in which keepalive packages will be sent. Only change this value if you know what you are doing. listenaddr: "" # the address to bind on, leave empty to bind on all addresses port: 80 # the port the HTTP server will listen on diff --git a/config/config.go b/config/config.go index bbf45a7..66f2d03 100644 --- a/config/config.go +++ b/config/config.go @@ -11,9 +11,11 @@ import ( // Configuration is stuff that can be configured externally per env variables or config file (config.yml). type Configuration struct { Server struct { - ListenAddr string `default:""` - Port int `default:"80"` - SSL struct { + KeepAlivePeriodSeconds int + ListenAddr string `default:""` + Port int `default:"80"` + + SSL struct { Enabled *bool `default:"false"` RedirectToHTTPS *bool `default:"true"` ListenAddr string `default:""` diff --git a/runner/runner.go b/runner/runner.go index effa35f..433430d 100644 --- a/runner/runner.go +++ b/runner/runner.go @@ -1,11 +1,14 @@ package runner import ( + "context" "crypto/tls" "fmt" "log" "net" "net/http" + "strconv" + "time" "github.com/gin-gonic/gin" "github.com/gotify/server/v2/config" @@ -18,7 +21,7 @@ func Run(engine *gin.Engine, conf *config.Configuration) { if *conf.Server.SSL.Enabled { if *conf.Server.SSL.RedirectToHTTPS { - httpHandler = redirectToHTTPS(string(conf.Server.SSL.Port)) + httpHandler = redirectToHTTPS(strconv.Itoa(conf.Server.SSL.Port)) } addr := fmt.Sprintf("%s:%d", conf.Server.SSL.ListenAddr, conf.Server.SSL.Port) @@ -38,12 +41,24 @@ func Run(engine *gin.Engine, conf *config.Configuration) { } fmt.Println("Started Listening for TLS connection on " + addr) go func() { - log.Fatal(s.ListenAndServeTLS(conf.Server.SSL.CertFile, conf.Server.SSL.CertKey)) + listener := startListening(addr, conf.Server.KeepAlivePeriodSeconds) + log.Fatal(s.ServeTLS(listener, conf.Server.SSL.CertFile, conf.Server.SSL.CertKey)) }() } addr := fmt.Sprintf("%s:%d", conf.Server.ListenAddr, conf.Server.Port) fmt.Println("Started Listening for plain HTTP connection on " + addr) - log.Fatal(http.ListenAndServe(addr, httpHandler)) + server := &http.Server{Addr: addr, Handler: httpHandler} + + log.Fatal(server.Serve(startListening(addr, conf.Server.KeepAlivePeriodSeconds))) +} + +func startListening(addr string, keepAlive int) net.Listener { + lc := net.ListenConfig{KeepAlive: time.Duration(keepAlive) * time.Second} + conn, err := lc.Listen(context.Background(), "tcp", addr) + if err != nil { + log.Fatalln("Could not listen on", addr, err) + } + return conn } func redirectToHTTPS(port string) http.HandlerFunc {