circuitbreaker

package
v1.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 16, 2022 License: MIT Imports: 7 Imported by: 0

README

circuitbreaker

circuitbreaker 熔断器。


使用示例

gin circuitbreaker 熔断器中间件示例

func CircuitBreaker(opts ...CircuitBreakerOption) gin.HandlerFunc {
	o := defaultCircuitBreakerOptions()
	o.apply(opts...)

	return func(c *gin.Context) {
		breaker := o.group.Get(c.FullPath()).(circuitbreaker.CircuitBreaker)
		if err := breaker.Allow(); err != nil {
			// NOTE: when client reject request locally,
			// continue to add counter let the drop ratio higher.
			breaker.MarkFailed()
			response.Output(c, http.StatusServiceUnavailable, err.Error())
			c.Abort()
			return
		}

		c.Next()

		code := c.Writer.Status()
		// NOTE: need to check internal and service unavailable error
		if code == http.StatusInternalServerError || code == http.StatusServiceUnavailable || code == http.StatusGatewayTimeout {
			breaker.MarkFailed()
		} else {
			breaker.MarkSuccess()
		}
	}
}

Documentation

Index

Constants

View Source
const (
	// StateOpen when circuit breaker open, request not allowed, after sleep
	// some duration, allow one single request for testing the health, if ok
	// then state reset to closed, if not continue the step.
	StateOpen int32 = iota
	// StateClosed when circuit breaker closed, request allowed, the breaker
	// calc the succeed ratio, if request num greater request setting and
	// ratio lower than the setting ratio, then reset state to open.
	StateClosed
)

Variables

View Source
var ErrNotAllowed = errors.New("circuitbreaker: not allowed for circuit open")

ErrNotAllowed error not allowed.

Functions

This section is empty.

Types

type Breaker

type Breaker struct {
	// contains filtered or unexported fields
}

Breaker is a sre CircuitBreaker pattern.

func (*Breaker) Allow

func (b *Breaker) Allow() error

Allow request if error returns nil.

func (*Breaker) MarkFailed

func (b *Breaker) MarkFailed()

MarkFailed mark request is failed.

func (*Breaker) MarkSuccess

func (b *Breaker) MarkSuccess()

MarkSuccess mark requeest is success.

type CircuitBreaker

type CircuitBreaker interface {
	Allow() error
	MarkSuccess()
	MarkFailed()
}

CircuitBreaker is a circuit breaker.

func NewBreaker

func NewBreaker(opts ...Option) CircuitBreaker

NewBreaker return a sreBresker with options

type Option

type Option func(*options)

Option is sre breaker option function.

func WithBucket

func WithBucket(b int) Option

WithBucket set the bucket number in a window duration.

func WithRequest

func WithRequest(r int64) Option

WithRequest with the minimum number of requests allowed.

func WithSuccess

func WithSuccess(s float64) Option

WithSuccess with the K = 1 / Success value of sre breaker, default success is 0.5 Reducing the K will make adaptive throttling behave more aggressively, Increasing the K will make adaptive throttling behave less aggressively.

func WithWindow

func WithWindow(d time.Duration) Option

WithWindow with the duration size of the statistical window.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL