From db5ff83057e7a8a2e885c5b4746b6d24b1da4374 Mon Sep 17 00:00:00 2001 From: zhangxu Date: Thu, 10 Jan 2019 12:33:33 +0800 Subject: [PATCH] fix: analysis for api circuit breaker --- pkg/proxy/filter_analysis.go | 7 ++++--- pkg/util/analysis.go | 36 ++++++++++++++++++++---------------- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/pkg/proxy/filter_analysis.go b/pkg/proxy/filter_analysis.go index b5e069cb..7beb6f95 100644 --- a/pkg/proxy/filter_analysis.go +++ b/pkg/proxy/filter_analysis.go @@ -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()) } diff --git a/pkg/util/analysis.go b/pkg/util/analysis.go index 7e0ff860..acfd11c7 100644 --- a/pkg/util/analysis.go +++ b/pkg/util/analysis.go @@ -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() }