feat: add keep-alive
This commit is contained in:
@@ -56,8 +56,6 @@ func HandleHTTPRequest(headers Headers, conn net.Conn) {
|
||||
|
||||
defer file.Close()
|
||||
sendFile(file, conn)
|
||||
|
||||
conn.Close()
|
||||
}
|
||||
|
||||
func sendFile(file *os.File, conn net.Conn) {
|
||||
|
||||
17
main.go
17
main.go
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user