Make keepalive period configurable

This commit is contained in:
Jannis Mattheis 2020-08-01 15:32:25 +02:00
parent 7993d0eef8
commit 909eeff406
4 changed files with 25 additions and 7 deletions

View File

@ -329,7 +329,7 @@ func TestMultipleClients(t *testing.T) {
userIDs := []uint{1, 1, 1, 2, 2, 3} userIDs := []uint{1, 1, 1, 2, 2, 3}
i := 0 i := 0
server, api := bootTestServer(func(context *gin.Context) { 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++ i++
}) })
defer server.Close() defer server.Close()

View File

@ -2,6 +2,7 @@
# Save it to `config.yml` when edited # Save it to `config.yml` when edited
server: 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 listenaddr: "" # the address to bind on, leave empty to bind on all addresses
port: 80 # the port the HTTP server will listen on port: 80 # the port the HTTP server will listen on

View File

@ -11,9 +11,11 @@ import (
// Configuration is stuff that can be configured externally per env variables or config file (config.yml). // Configuration is stuff that can be configured externally per env variables or config file (config.yml).
type Configuration struct { type Configuration struct {
Server struct { Server struct {
ListenAddr string `default:""` KeepAlivePeriodSeconds int
Port int `default:"80"` ListenAddr string `default:""`
SSL struct { Port int `default:"80"`
SSL struct {
Enabled *bool `default:"false"` Enabled *bool `default:"false"`
RedirectToHTTPS *bool `default:"true"` RedirectToHTTPS *bool `default:"true"`
ListenAddr string `default:""` ListenAddr string `default:""`

View File

@ -1,11 +1,14 @@
package runner package runner
import ( import (
"context"
"crypto/tls" "crypto/tls"
"fmt" "fmt"
"log" "log"
"net" "net"
"net/http" "net/http"
"strconv"
"time"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/gotify/server/v2/config" "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.Enabled {
if *conf.Server.SSL.RedirectToHTTPS { 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) 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) fmt.Println("Started Listening for TLS connection on " + addr)
go func() { 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) addr := fmt.Sprintf("%s:%d", conf.Server.ListenAddr, conf.Server.Port)
fmt.Println("Started Listening for plain HTTP connection on " + addr) 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 { func redirectToHTTPS(port string) http.HandlerFunc {