feat: add keep-alive

This commit is contained in:
2026-02-15 19:41:57 +01:00
parent df0bf9bed7
commit 336388041e
2 changed files with 13 additions and 6 deletions

17
main.go
View File

@@ -38,11 +38,15 @@ func main() {
func handleConnection(conn net.Conn) {
startTime := time.Now()
defer func() {
slog.Info("Request handled", "duration", time.Since(startTime).String())
}()
defer conn.Close()
durr, err := time.ParseDuration("5m")
if err != nil {
slog.Error("Could not parse duration", "err", err.Error())
return
}
plus5min := startTime.Add(durr)
conn.SetDeadline(plus5min)
reader := bufio.NewReader(conn)
headers := readHeaders(reader)
if headers == nil {
@@ -52,5 +56,10 @@ func handleConnection(conn net.Conn) {
slog.Info("Received request", "method", headers.Method, "uri", headers.Uri, "proto", headers.Proto, "client", conn.RemoteAddr().String())
conn.SetDeadline(time.Now().Add(durr))
HandleHTTPRequest(*headers, conn)
if headers.KV["Connection"] == "keep-alive" {
handleConnection(conn)
}
}