Documentation
¶
Index ¶
Constants ¶
const ( // DefaultFailureRatio is the default failure ratio above which requests are rejected. DefaultFailureRatio = 0.5 // DefaultMinRequests is the default number of requests in the window below which no request is rejected. DefaultMinRequests = 20 // DefaultWindow is the default duration of the measurement window. DefaultWindow = 10 * time.Second // DefaultMaxRejectRatio is the default maximum ratio of rejected requests. DefaultMaxRejectRatio = 0.9 )
Variables ¶
var ErrInvalidConfig = errors.New("invalid config")
Functions ¶
func DefaultFailure ¶
DefaultFailure reports a response status of 502, 503 or 504 as a failure.
func DefaultReject ¶
DefaultReject responds with 503 Service Unavailable.
func Middleware ¶
func Middleware(opts ...Option) (fox.MiddlewareFunc, error)
Middleware returns a fox.MiddlewareFunc that probabilistically rejects requests when the failure ratio goes above the configured ratio, based on the adaptive throttling described in the Google SRE book. Each route gets its own instance of a given breaker. One instance can reject requests while another does not, their state is not shared. To share state across routes, use New.
Types ¶
type Breaker ¶ added in v0.2.0
type Breaker struct {
// contains filtered or unexported fields
}
Breaker is the state shared by every handler wrapped by its middleware.
func New ¶ added in v0.2.0
New returns a Breaker that probabilistically rejects requests when the failure ratio goes above the configured ratio. Its state is shared by every handler wrapped by Breaker.Middleware.
func (*Breaker) Middleware ¶ added in v0.2.0
func (b *Breaker) Middleware() fox.MiddlewareFunc
Middleware returns a fox.MiddlewareFunc that uses the state of b on every handler it wraps.
func (*Breaker) RejectRatio ¶ added in v0.2.0
RejectRatio returns the probability that the next request is rejected.
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option configures the middleware.
func WithFailure ¶
WithFailure sets the function that reports whether a handled request failed. The default is DefaultFailure.
func WithFailureRatio ¶
WithFailureRatio sets the failure ratio, in [0, 1), above which requests start to be rejected. The default is DefaultFailureRatio.
func WithMaxRejectRatio ¶
WithMaxRejectRatio sets the maximum ratio, in (0, 1], of rejected requests. The remaining requests always reach the handler, which lets the middleware detect a recovery. The default is DefaultMaxRejectRatio.
func WithMinRequests ¶
WithMinRequests sets the number of requests in the window below which no request is rejected. The default is DefaultMinRequests.
func WithRejectHandler ¶
func WithRejectHandler(handler fox.HandlerFunc) Option
WithRejectHandler sets the handler called for rejected requests. The default is DefaultReject.
func WithWindow ¶
WithWindow sets the duration of the measurement window, between 1s and 10m. The default is DefaultWindow.