Send 'Retry-After' to comply with RFC6585.
This commit is contained in:
parent
027093a5a5
commit
8d75aba7eb
29 changed files with 435 additions and 172 deletions
36
vendor/github.com/vulcand/oxy/connlimit/connlimit.go
generated
vendored
36
vendor/github.com/vulcand/oxy/connlimit/connlimit.go
generated
vendored
|
@ -1,4 +1,4 @@
|
|||
// package connlimit provides control over simultaneous connections coming from the same source
|
||||
// Package connlimit provides control over simultaneous connections coming from the same source
|
||||
package connlimit
|
||||
|
||||
import (
|
||||
|
@ -10,7 +10,7 @@ import (
|
|||
"github.com/vulcand/oxy/utils"
|
||||
)
|
||||
|
||||
// Limiter tracks concurrent connection per token
|
||||
// ConnLimiter tracks concurrent connection per token
|
||||
// and is capable of rejecting connections if they are failed
|
||||
type ConnLimiter struct {
|
||||
mutex *sync.Mutex
|
||||
|
@ -21,8 +21,10 @@ type ConnLimiter struct {
|
|||
next http.Handler
|
||||
|
||||
errHandler utils.ErrorHandler
|
||||
log *log.Logger
|
||||
}
|
||||
|
||||
// New creates a new ConnLimiter
|
||||
func New(next http.Handler, extract utils.SourceExtractor, maxConnections int64, options ...ConnLimitOption) (*ConnLimiter, error) {
|
||||
if extract == nil {
|
||||
return nil, fmt.Errorf("Extract function can not be nil")
|
||||
|
@ -33,6 +35,7 @@ func New(next http.Handler, extract utils.SourceExtractor, maxConnections int64,
|
|||
maxConnections: maxConnections,
|
||||
connections: make(map[string]int64),
|
||||
next: next,
|
||||
log: log.StandardLogger(),
|
||||
}
|
||||
|
||||
for _, o := range options {
|
||||
|
@ -41,11 +44,24 @@ func New(next http.Handler, extract utils.SourceExtractor, maxConnections int64,
|
|||
}
|
||||
}
|
||||
if cl.errHandler == nil {
|
||||
cl.errHandler = defaultErrHandler
|
||||
cl.errHandler = &ConnErrHandler{
|
||||
log: cl.log,
|
||||
}
|
||||
}
|
||||
return cl, nil
|
||||
}
|
||||
|
||||
// Logger defines the logger the connection limiter will use.
|
||||
//
|
||||
// It defaults to logrus.StandardLogger(), the global logger used by logrus.
|
||||
func Logger(l *log.Logger) ConnLimitOption {
|
||||
return func(cl *ConnLimiter) error {
|
||||
cl.log = l
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// Wrap sets the next handler to be called by connexion limiter handler.
|
||||
func (cl *ConnLimiter) Wrap(h http.Handler) {
|
||||
cl.next = h
|
||||
}
|
||||
|
@ -53,12 +69,12 @@ func (cl *ConnLimiter) Wrap(h http.Handler) {
|
|||
func (cl *ConnLimiter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
token, amount, err := cl.extract.Extract(r)
|
||||
if err != nil {
|
||||
log.Errorf("failed to extract source of the connection: %v", err)
|
||||
cl.log.Errorf("failed to extract source of the connection: %v", err)
|
||||
cl.errHandler.ServeHTTP(w, r, err)
|
||||
return
|
||||
}
|
||||
if err := cl.acquire(token, amount); err != nil {
|
||||
log.Debugf("limiting request source %s: %v", token, err)
|
||||
cl.log.Debugf("limiting request source %s: %v", token, err)
|
||||
cl.errHandler.ServeHTTP(w, r, err)
|
||||
return
|
||||
}
|
||||
|
@ -95,6 +111,7 @@ func (cl *ConnLimiter) release(token string, amount int64) {
|
|||
}
|
||||
}
|
||||
|
||||
// MaxConnError maximum connections reached error
|
||||
type MaxConnError struct {
|
||||
max int64
|
||||
}
|
||||
|
@ -103,12 +120,14 @@ func (m *MaxConnError) Error() string {
|
|||
return fmt.Sprintf("max connections reached: %d", m.max)
|
||||
}
|
||||
|
||||
// ConnErrHandler connection limiter error handler
|
||||
type ConnErrHandler struct {
|
||||
log *log.Logger
|
||||
}
|
||||
|
||||
func (e *ConnErrHandler) ServeHTTP(w http.ResponseWriter, req *http.Request, err error) {
|
||||
if log.GetLevel() >= log.DebugLevel {
|
||||
logEntry := log.WithField("Request", utils.DumpHttpRequest(req))
|
||||
if e.log.Level >= log.DebugLevel {
|
||||
logEntry := e.log.WithField("Request", utils.DumpHttpRequest(req))
|
||||
logEntry.Debug("vulcand/oxy/connlimit: begin ServeHttp on request")
|
||||
defer logEntry.Debug("vulcand/oxy/connlimit: completed ServeHttp on request")
|
||||
}
|
||||
|
@ -121,6 +140,7 @@ func (e *ConnErrHandler) ServeHTTP(w http.ResponseWriter, req *http.Request, err
|
|||
utils.DefaultHandler.ServeHTTP(w, req, err)
|
||||
}
|
||||
|
||||
// ConnLimitOption connection limit option type
|
||||
type ConnLimitOption func(l *ConnLimiter) error
|
||||
|
||||
// ErrorHandler sets error handler of the server
|
||||
|
@ -130,5 +150,3 @@ func ErrorHandler(h utils.ErrorHandler) ConnLimitOption {
|
|||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
var defaultErrHandler = &ConnErrHandler{}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue