From cfcbbb09625aec3a800e8ad40d1b417364037688 Mon Sep 17 00:00:00 2001 From: nervuri Date: Sat, 11 Mar 2023 15:37:02 +0000 Subject: [PATCH] add proper read & write timeouts --- server.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/server.go b/server.go index 3712ac4..bd187b9 100644 --- a/server.go +++ b/server.go @@ -19,6 +19,9 @@ import ( "time" ) +const readTimeout = 10 // seconds +const writeTimeout = 10 // seconds + type tlsConnectionInfo struct { TlsVersion uint16 `json:"tls_version"` CipherSuite uint16 `json:"cipher_suite"` @@ -236,12 +239,24 @@ func main() { log.Println("Server started") for { + // Wait for a connection. conn, err := ln.Accept() if err != nil { log.Println("Error accepting: ", err.Error()) continue } - conn.SetReadDeadline(time.Now().Add(5 * time.Second)) // timeout + // Set connection timeouts. + err = conn.SetReadDeadline(time.Now().Add(readTimeout * time.Second)) + if err != nil { + log.Println("SetReadDeadline error: ", err.Error()) + continue + } + err = conn.SetWriteDeadline(time.Now().Add(writeTimeout * time.Second)) + if err != nil { + log.Println("SetWriteDeadline error: ", err.Error()) + continue + } + // Process the request. go peek(conn, &tlsConfig) } }