-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathmaxconnections.go
37 lines (31 loc) · 1.03 KB
/
maxconnections.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package maxconnections
import "net/http"
// See tooManyRequests from k8s.io/apiserver/pkg/server/filters/maxinflight.go
func defaultOverloadHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Retry-After", "1") // Docker ignores this header, though.
http.Error(w, "Too many requests, please try again later.", http.StatusTooManyRequests)
}
// Handler implements the http.Handler interface.
type Handler struct {
limiter Limiter
handler http.Handler
OverloadHandler http.Handler
}
// New returns an http.Handler that uses limiter to control h invocation.
// If limiter prohibits starting a new handler, OverloadHandler will be
// invoked.
func New(limiter Limiter, h http.Handler) *Handler {
return &Handler{
limiter: limiter,
handler: h,
OverloadHandler: http.HandlerFunc(defaultOverloadHandler),
}
}
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if !h.limiter.Start(r.Context()) {
h.OverloadHandler.ServeHTTP(w, r)
return
}
defer h.limiter.Done()
h.handler.ServeHTTP(w, r)
}