From 32de06dba4ebe30a9f88b43c3bc0f767994467be Mon Sep 17 00:00:00 2001 From: Erik Dubbelboer Date: Sat, 16 Nov 2019 17:18:51 +0100 Subject: [PATCH] Don't start a goroutine when not needed sync.Once only has the overhead of a single atomic.LoadUint32 Fixes #693 --- header.go | 10 ++++++++-- server.go | 2 ++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/header.go b/header.go index ba647a1014..279d447ec9 100644 --- a/header.go +++ b/header.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "io" + "sync" "sync/atomic" "time" ) @@ -1436,7 +1437,7 @@ func isOnlyCRLF(b []byte) bool { return true } -func init() { +func updateServerDate() { refreshServerDate() go func() { for { @@ -1446,7 +1447,10 @@ func init() { }() } -var serverDate atomic.Value +var ( + serverDate atomic.Value + serverDateOnce sync.Once // serverDateOnce.Do(updateServerDate) +) func refreshServerDate() { b := AppendHTTPDate(nil, time.Now()) @@ -1493,6 +1497,8 @@ func (h *ResponseHeader) AppendBytes(dst []byte) []byte { if len(server) != 0 { dst = appendHeaderLine(dst, strServer, server) } + + serverDateOnce.Do(updateServerDate) dst = appendHeaderLine(dst, strDate, serverDate.Load().([]byte)) // Append Content-Type only for non-zero responses diff --git a/server.go b/server.go index 96a41ac838..55bf1ad625 100644 --- a/server.go +++ b/server.go @@ -2474,6 +2474,8 @@ func (s *Server) writeFastError(w io.Writer, statusCode int, msg string) { server = fmt.Sprintf("Server: %s\r\n", s.getServerName()) } + serverDateOnce.Do(updateServerDate) + fmt.Fprintf(w, "Connection: close\r\n"+ server+ "Date: %s\r\n"+