示例#1
0
文件: cbreaker.go 项目: lysu/oxy
// 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
}
示例#2
0
文件: cbreaker_test.go 项目: lysu/oxy
func statsOK() *memmetrics.RTMetrics {
	m, err := memmetrics.NewRTMetrics()
	if err != nil {
		panic(err)
	}
	return m
}
示例#3
0
文件: stats.go 项目: vulcand/vulcand
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)
}
示例#4
0
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)
}
示例#5
0
文件: cbreaker_test.go 项目: lysu/oxy
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
}
示例#6
0
文件: stats.go 项目: vulcand/vulcand
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
}
示例#7
0
文件: cbreaker_test.go 项目: lysu/oxy
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
}
示例#8
0
文件: cbreaker_test.go 项目: lysu/oxy
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
}
示例#9
0
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)
}
示例#10
0
文件: stats.go 项目: vulcand/vulcand
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)
}