Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize: revise lock in cb metric #202

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions cloud/circuitbreaker/metricer.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,27 +116,25 @@ func (w *window) Succeed() {

// Fail records a failure in the current perPBucket.
func (w *window) Fail() {
w.rw.Lock()
rwx := w.rw.RLocker()
rwx.Lock()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since all the operations are atomic ops, is it really a need to lock here?

b := w.getBucket()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If getBucket is a readonly op, it can be moved out of the critical zone.

atomic.AddInt64(&w.conseErr, 1)
atomic.AddInt64(&w.allFailure, 1)
if atomic.LoadInt64(&w.errStart) == 0 {
atomic.StoreInt64(&w.errStart, time.Now().UnixNano())
}
w.rw.Unlock()
atomic.CompareAndSwapInt64(&w.errStart, 0, time.Now().UnixNano())
rwx.Unlock()
b.Fail()
}

// Timeout records a timeout in the current perPBucket
func (w *window) Timeout() {
w.rw.Lock()
rwx := w.rw.RLocker()
rwx.Lock()
b := w.getBucket()
atomic.AddInt64(&w.conseErr, 1)
atomic.AddInt64(&w.allTimeout, 1)
if atomic.LoadInt64(&w.errStart) == 0 {
atomic.StoreInt64(&w.errStart, time.Now().UnixNano())
}
w.rw.Unlock()
atomic.CompareAndSwapInt64(&w.errStart, 0, time.Now().UnixNano())
rwx.Unlock()
b.Timeout()
}

Expand Down