Exemplo n.º 1
0
// New creates a new CircuitBreaker middleware
func New(condition threshold.Predicate, fallback middleware.Middleware, options Options) (*CircuitBreaker, error) {
	if condition == nil || fallback == nil {
		return nil, fmt.Errorf("provide non nil condition and fallback")
	}
	o, err := setDefaults(options)
	if err != nil {
		return nil, err
	}

	mt, err := metrics.NewRoundTripMetrics(metrics.RoundTripOptions{TimeProvider: o.TimeProvider})
	if err != nil {
		return nil, err
	}

	cb := &CircuitBreaker{
		tm:          o.TimeProvider,
		o:           o,
		condition:   condition,
		fallback:    fallback,
		metrics:     mt,
		m:           &sync.RWMutex{},
		checkPeriod: o.CheckPeriod,
	}
	return cb, nil
}
Exemplo n.º 2
0
func statsOK() *metrics.RoundTripMetrics {
	m, err := metrics.NewRoundTripMetrics(metrics.RoundTripOptions{})
	if err != nil {
		panic(err)
	}
	return m
}
Exemplo n.º 3
0
func statsLatencyAtQuantile(quantile float64, value time.Duration) *metrics.RoundTripMetrics {
	m, err := metrics.NewRoundTripMetrics(metrics.RoundTripOptions{})
	if err != nil {
		panic(err)
	}
	m.RecordMetrics(&request.BaseAttempt{Duration: value})
	return m
}
Exemplo n.º 4
0
func statsResponseCodes(codes ...statusCode) *metrics.RoundTripMetrics {
	m, err := metrics.NewRoundTripMetrics(metrics.RoundTripOptions{})
	if err != nil {
		panic(err)
	}
	for _, c := range codes {
		for i := int64(0); i < c.Count; i++ {
			m.RecordMetrics(&request.BaseAttempt{Response: &http.Response{StatusCode: c.Code}})
		}
	}

	return m
}
Exemplo n.º 5
0
func statsNetErrors(threshold float64) *metrics.RoundTripMetrics {
	m, err := metrics.NewRoundTripMetrics(metrics.RoundTripOptions{})
	if err != nil {
		panic(err)
	}
	for i := 0; i < 100; i++ {
		if i < int(threshold*100) {
			m.RecordMetrics(&request.BaseAttempt{Error: fmt.Errorf("boo")})
		} else {
			m.RecordMetrics(&request.BaseAttempt{Response: &http.Response{StatusCode: 200}})
		}
	}
	return m
}