示例#1
0
func statsOK() *metrics.RoundTripMetrics {
	m, err := metrics.NewRoundTripMetrics(metrics.RoundTripOptions{})
	if err != nil {
		panic(err)
	}
	return m
}
示例#2
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
}
示例#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
}
示例#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
}
示例#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
}