// New creates a new CircuitBreaker middleware func New(next http.Handler, expression string, options ...CircuitBreakerOption) (*CircuitBreaker, error) { cb := &CircuitBreaker{ m: &sync.RWMutex{}, next: next, // Default values. Might be overwritten by options below. clock: &timetools.RealTime{}, checkPeriod: defaultCheckPeriod, fallbackDuration: defaultFallbackDuration, recoveryDuration: defaultRecoveryDuration, fallback: defaultFallback, log: utils.NullLogger, } for _, s := range options { if err := s(cb); err != nil { return nil, err } } condition, err := parseExpression(expression) if err != nil { return nil, err } cb.condition = condition mt, err := memmetrics.NewRTMetrics() if err != nil { return nil, err } cb.metrics = mt return cb, nil }
func statsOK() *memmetrics.RTMetrics { m, err := memmetrics.NewRTMetrics() if err != nil { panic(err) } return m }
func (m *mux) ServerStats(key engine.ServerKey) (*engine.RoundTripStats, error) { log.Infof("%s ServerStats", m) m.mtx.RLock() defer m.mtx.RUnlock() b, ok := m.backends[key.BackendKey] if !ok { return nil, fmt.Errorf("%v not found", key.BackendKey) } srv, ok := b.findServer(key) if !ok { return nil, fmt.Errorf("%v not found", key) } u, err := url.Parse(srv.URL) if err != nil { return nil, err } rtm, err := memmetrics.NewRTMetrics() if err != nil { return nil, err } for _, f := range m.frontends { if f.backend.backend.Id != key.BackendKey.Id { continue } if err := f.watcher.collectServerMetrics(rtm, u); err != nil { return nil, err } } return engine.NewRoundTripStats(rtm) }
func (mx *mux) serverStats(key engine.ServerKey) (*engine.RoundTripStats, error) { b, ok := mx.backends[key.BackendKey] if !ok { return nil, fmt.Errorf("%v not found", key.BackendKey) } srv, ok := b.findServer(key) if !ok { return nil, fmt.Errorf("%v not found", key) } u, err := url.Parse(srv.URL) if err != nil { return nil, err } m, err := memmetrics.NewRTMetrics() if err != nil { return nil, err } for _, f := range mx.frontends { if f.backend.backend.Id != key.BackendKey.Id { continue } if err := f.watcher.collectServerMetrics(m, u); err != nil { return nil, err } } return engine.NewRoundTripStats(m) }
func statsLatencyAtQuantile(quantile float64, value time.Duration) *memmetrics.RTMetrics { m, err := memmetrics.NewRTMetrics() if err != nil { panic(err) } m.Record(http.StatusOK, value) return m }
func newSval(s engine.Server) (*sval, error) { rtm, err := memmetrics.NewRTMetrics() if err != nil { return nil, err } u, err := url.Parse(s.URL) if err != nil { return nil, err } return &sval{srv: &s, rtm: rtm, url: u}, nil }
func statsResponseCodes(codes ...statusCode) *memmetrics.RTMetrics { m, err := memmetrics.NewRTMetrics() if err != nil { panic(err) } for _, c := range codes { for i := int64(0); i < c.Count; i++ { m.Record(c.Code, 0) } } return m }
func statsNetErrors(threshold float64) *memmetrics.RTMetrics { m, err := memmetrics.NewRTMetrics() if err != nil { panic(err) } for i := 0; i < 100; i++ { if i < int(threshold*100) { m.Record(http.StatusGatewayTimeout, 0) } else { m.Record(http.StatusOK, 0) } } return m }
func (mx *mux) backendStats(key engine.BackendKey) (*engine.RoundTripStats, error) { m, err := memmetrics.NewRTMetrics() if err != nil { return nil, err } for _, f := range mx.frontends { if f.backend.backend.Id != key.Id { continue } if err := f.watcher.collectMetrics(m); err != nil { return nil, err } } return engine.NewRoundTripStats(m) }
func (m *mux) BackendStats(key engine.BackendKey) (*engine.RoundTripStats, error) { log.Infof("%s BackendStats", m) m.mtx.RLock() defer m.mtx.RUnlock() rtm, err := memmetrics.NewRTMetrics() if err != nil { return nil, err } for _, f := range m.frontends { if f.backend.backend.Id != key.Id { continue } if err := f.watcher.collectMetrics(rtm); err != nil { return nil, err } } return engine.NewRoundTripStats(rtm) }