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

*: add API concurrency metrics #7541

Merged
merged 28 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
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
16 changes: 16 additions & 0 deletions pkg/ratelimit/concurrency_limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ type concurrencyLimiter struct {
mu syncutil.RWMutex
current uint64
limit uint64

// statistic
maxLimit uint64
}

func newConcurrencyLimiter(limit uint64) *concurrencyLimiter {
Expand All @@ -34,6 +37,9 @@ func (l *concurrencyLimiter) allow() bool {

if l.limit == unlimit || l.current+1 <= l.limit {
rleungx marked this conversation as resolved.
Show resolved Hide resolved
l.current++
if l.current > l.maxLimit {
l.maxLimit = l.current
}
return true
}
return false
Expand Down Expand Up @@ -68,3 +74,13 @@ func (l *concurrencyLimiter) getCurrent() uint64 {

return l.current
}

func (l *concurrencyLimiter) getMaxConcurrency() uint64 {
l.mu.RLock()
Copy link
Member

Choose a reason for hiding this comment

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

Should be Lock?

defer func() {
l.maxLimit = 0
l.mu.RUnlock()
}()

return l.maxLimit
}
9 changes: 9 additions & 0 deletions pkg/ratelimit/concurrency_limiter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,18 @@ func TestConcurrencyLimiter(t *testing.T) {
cl.release()
re.True(cl.allow())
re.Equal(uint64(10), cl.getLimit())
re.Equal(uint64(10), cl.getMaxConcurrency())
re.Equal(uint64(0), cl.getMaxConcurrency())
cl.setLimit(5)
re.Equal(uint64(5), cl.getLimit())
re.Equal(uint64(10), cl.getCurrent())
cl.release()
re.Equal(uint64(9), cl.getCurrent())
for i := 0; i < 9; i++ {
cl.release()
}
for i := 0; i < 5; i++ {
re.True(cl.allow())
}
re.Equal(uint64(5), cl.getMaxConcurrency())
}
2 changes: 1 addition & 1 deletion pkg/ratelimit/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (l *Controller) collectMetrics() {
label := key.(string)
// Due to not in hot path, no need to save sub Gauge.
if con := limiter.getConcurrencyLimiter(); con != nil {
l.concurrencyGauge.WithLabelValues(l.apiType, label).Set(float64(con.getCurrent()))
l.concurrencyGauge.WithLabelValues(l.apiType, label).Set(float64(con.getMaxConcurrency()))
}
return true
})
Expand Down
Loading