Skip to content

Commit

Permalink
fix: analysis for api circuit breaker
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangxu19830126 committed Jan 10, 2019
1 parent 1fd641e commit db5ff83
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
7 changes: 4 additions & 3 deletions pkg/proxy/filter_analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,18 @@ func (f *AnalysisFilter) Name() string {

// Pre execute before proxy
func (f *AnalysisFilter) Pre(c filter.Context) (statusCode int, err error) {
c.Analysis().Request(c.Server().ID)
// TODO: avoid lock overhead in every request
c.Analysis().Request(c.(*proxyContext).circuitResourceID())
return f.BaseFilter.Pre(c)
}

// Post execute after proxy
func (f *AnalysisFilter) Post(c filter.Context) (statusCode int, err error) {
c.Analysis().Response(c.Server().ID, c.EndAt().Sub(c.StartAt()).Nanoseconds())
c.Analysis().Response(c.(*proxyContext).circuitResourceID(), c.EndAt().Sub(c.StartAt()).Nanoseconds())
return f.BaseFilter.Post(c)
}

// PostErr execute proxy has errors
func (f *AnalysisFilter) PostErr(c filter.Context) {
c.Analysis().Failure(c.Server().ID)
c.Analysis().Failure(c.(*proxyContext).circuitResourceID())
}
36 changes: 20 additions & 16 deletions pkg/util/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,42 +310,46 @@ func (a *Analysis) GetContinuousFailureCount(server uint64) int {
// Reject incr reject count
func (a *Analysis) Reject(key uint64) {
a.Lock()
p := a.points[key]
p.rejects.Incr()
if p, ok := a.points[key]; ok {
p.rejects.Incr()
}
a.Unlock()
}

// Failure incr failure count
func (a *Analysis) Failure(key uint64) {
a.Lock()
p := a.points[key]
p.failure.Incr()
p.continuousFailure.Incr()
if p, ok := a.points[key]; ok {
p.failure.Incr()
p.continuousFailure.Incr()
}
a.Unlock()
}

// Request incr request count
func (a *Analysis) Request(key uint64) {
a.Lock()
p := a.points[key]
p.requests.Incr()
if p, ok := a.points[key]; ok {
p.requests.Incr()
}
a.Unlock()
}

// Response incr successed count
func (a *Analysis) Response(key uint64, cost int64) {
a.Lock()
p := a.points[key]
p.successed.Incr()
p.costs.Add(cost)
p.continuousFailure.Set(0)
if p, ok := a.points[key]; ok {
p.successed.Incr()
p.costs.Add(cost)
p.continuousFailure.Set(0)

if p.max.Get() < cost {
p.max.Set(cost)
}
if p.max.Get() < cost {
p.max.Set(cost)
}

if p.min.Get() == 0 || p.min.Get() > cost {
p.min.Set(cost)
if p.min.Get() == 0 || p.min.Get() > cost {
p.min.Set(cost)
}
}
a.Unlock()
}
Expand Down

0 comments on commit db5ff83

Please sign in to comment.