Files
Alessandro RosandGitHub 06c7cc3d7f fix race condition when closing HTTP servers (#5390)
when a HTTP server is closed, open connections are now immediately
closed and open routines are waited before the server is considered
closed.
2026-01-31 14:09:10 +01:00

37 lines
468 B
Go

package httpp
import (
"net/http"
"sync"
)
type handlerTracker struct {
h http.Handler
mutex sync.Mutex
wg sync.WaitGroup
closed bool
}
func (h *handlerTracker) close() {
h.mutex.Lock()
h.closed = true
h.mutex.Unlock()
h.wg.Wait()
}
func (h *handlerTracker) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.mutex.Lock()
if h.closed {
h.mutex.Unlock()
return
}
h.wg.Add(1)
h.mutex.Unlock()
defer h.wg.Done()
h.h.ServeHTTP(w, r)
}