Пример #1
0
func fromSpec(spec *Spec, next http.Handler) (*cbreaker.CircuitBreaker, error) {
	b, err := toBytes(spec.Fallback)
	if err != nil {
		return nil, err
	}

	fallback, err := actionFromJSON(b)
	if err != nil {
		return nil, err
	}
	var onTripped, onStandby cbreaker.SideEffect
	if spec.OnTripped != nil {
		b, err := toBytes(spec.OnTripped)
		if err != nil {
			return nil, err
		}
		if len(b) != 0 {
			v, err := sideEffectFromJSON(b)
			if err != nil {
				return nil, err
			}
			onTripped = v
		}
	}

	if spec.OnStandby != nil {
		b, err := toBytes(spec.OnStandby)
		if err != nil {
			return nil, err
		}
		if len(b) != 0 {
			v, err := sideEffectFromJSON(b)
			if err != nil {
				return nil, err
			}
			onStandby = v
		}
	}

	return cbreaker.New(next, spec.Condition,
		cbreaker.Fallback(fallback),
		cbreaker.FallbackDuration(spec.FallbackDuration),
		cbreaker.RecoveryDuration(spec.RecoveryDuration),
		cbreaker.OnTripped(onTripped),
		cbreaker.OnStandby(onStandby),
		cbreaker.CheckPeriod(spec.CheckPeriod))
}
Пример #2
0
func NewCircuitBreaker(next http.Handler, expression string, options ...cbreaker.CircuitBreakerOption) *CircuitBreaker {
	circuitBreaker, _ := cbreaker.New(next, expression, options...)
	return &CircuitBreaker{circuitBreaker}
}
Пример #3
0
func NewCircuitBreaker(next http.Handler, options ...cbreaker.CircuitBreakerOption) *CircuitBreaker {
	circuitBreaker, _ := cbreaker.New(next, "NetworkErrorRatio() > 0.5", options...)
	return &CircuitBreaker{circuitBreaker}
}