Documentation
¶
Index ¶
- Variables
- func Wrap(conn net.Conn, free func()) net.Conn
- type Config
- type Limiter
- func (l *Limiter) Accept(conn net.Conn) (func(), error)
- func (l *Limiter) Config() Config
- func (l *Limiter) HTTPConnStateFunc() func(net.Conn, http.ConnState)
- func (l *Limiter) HTTPConnStateFuncWithDefault429Handler(writeDeadlineMaxDelay time.Duration) func(net.Conn, http.ConnState)
- func (l *Limiter) HTTPConnStateFuncWithErrorHandler(errorHandler func(error, net.Conn)) func(net.Conn, http.ConnState)
- func (l *Limiter) NumOpen(addr net.Addr) int
- func (l *Limiter) SetConfig(c Config)
- type WrappedConn
Constants ¶
This section is empty.
Variables ¶
var ( // ErrPerClientIPLimitReached is returned if accepting a new conn would exceed // the per-client-ip limit set. ErrPerClientIPLimitReached = errors.New("client connection limit reached") )
Functions ¶
Types ¶
type Config ¶
type Config struct { // MaxConnsPerClientIP limits how many concurrent connections are allowed from // a given client IP. The IP is the one reported by the connection so cannot // be relied upon if clients are connecting through multiple proxies or able // to spoof their source IP address in some way. Similarly, multiple clients // connected via a proxy or NAT gateway or similar will all be seen as coming // from the same IP and so limited as one client. MaxConnsPerClientIP int }
Config is the configuration for the limiter.
type Limiter ¶
type Limiter struct {
// contains filtered or unexported fields
}
Limiter implements a simple limiter that tracks the number of connections from each client IP. It may be used in it's zero value although no limits will be configured initially - they can be set later with SetConfig.
func NewLimiter ¶
NewLimiter returns a limiter with the specified config.
func (*Limiter) Accept ¶
Accept is called as early as possible when handling a new conn. If the connection should be accepted according to the Limiter's Config, it will return a free func and nil error. The free func must be called when the connection is no longer being handled - typically in a defer statement in the main connection handling goroutine, this will decrement the counter for that client IP. If the configured limit has been reached, a no-op func is returned (doesn't need to be called), and ErrPerClientIPLimitReached is returned.
If any other error is returned it signifies something wrong with the config or transient failure to read or parse the remote IP. The free func will be a no-op in this case and need not be called.
func (*Limiter) Config ¶
Config returns the current limiter configuration. It is safe to call from any goroutine and does not block new connections being accepted.
func (*Limiter) HTTPConnStateFunc ¶
HTTPConnStateFunc is here for ascending compatibility reasons.
func (*Limiter) HTTPConnStateFuncWithDefault429Handler ¶ added in v0.3.0
func (l *Limiter) HTTPConnStateFuncWithDefault429Handler(writeDeadlineMaxDelay time.Duration) func(net.Conn, http.ConnState)
HTTPConnStateFuncWithDefault429Handler return an HTTP 429 if too many connections occur. BEWARE that returning HTTP 429 is done on critical path, you might choose to use HTTPConnStateFuncWithErrorHandler if you want to use a non-blocking strategy.
func (*Limiter) HTTPConnStateFuncWithErrorHandler ¶ added in v0.3.0
func (l *Limiter) HTTPConnStateFuncWithErrorHandler(errorHandler func(error, net.Conn)) func(net.Conn, http.ConnState)
HTTPConnStateFuncWithErrorHandler returns a func that can be passed as the ConnState field of an http.Server. This intercepts new HTTP connections to the server and applies the limiting to new connections.
Note that if the conn is hijacked from the HTTP server then it will be freed in the limiter as if it was closed. Servers that use Hijacking must implement their own calls if they need to continue limiting the number of concurrent hijacked connections. errorHandler MUST close the connection itself
type WrappedConn ¶
WrappedConn wraps a net.Conn and free() func returned by Limiter.Accept so that when the wrapped connections Close method is called, its free func is also called.
func (*WrappedConn) Close ¶
func (w *WrappedConn) Close() error
Close frees the tracked connection and closes the underlying net.Conn.