when a HTTP server is closed, open connections are now immediately closed and open routines are waited before the server is considered closed.
37 lines
468 B
Go
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)
|
|
}
|