Skip to content
This repository has been archived by the owner on May 23, 2024. It is now read-only.

Initialize rate limiter balance with maxBalance #135

Merged
merged 1 commit into from
Mar 29, 2017
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions sampler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,11 @@ func TestUpdateSampler(t *testing.T) {
assert.NotEqual(t, initSampler, sampler.sampler, "Sampler should have been updated")
assert.Equal(t, test.defaultProbability, s.defaultSampler.SamplingRate())

// First call is always sampled
sampled, tags := sampler.IsSampled(TraceID{Low: testMaxID + 10}, testOperationName)
assert.True(t, sampled)

sampled, tags = sampler.IsSampled(TraceID{Low: testMaxID + 10}, testOperationName)
assert.False(t, sampled)
sampled, tags = sampler.IsSampled(TraceID{Low: testMaxID - 10}, testOperationName)
assert.True(t, sampled)
Expand Down
2 changes: 1 addition & 1 deletion utils/rate_limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type rateLimiter struct {
func NewRateLimiter(creditsPerSecond, maxBalance float64) RateLimiter {
return &rateLimiter{
creditsPerSecond: creditsPerSecond,
balance: creditsPerSecond,
balance: maxBalance,
maxBalance: maxBalance,
lastTick: time.Now(),
timeNow: time.Now}
Expand Down
3 changes: 2 additions & 1 deletion utils/rate_limiter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ func TestMaxBalance(t *testing.T) {
limiter.(*rateLimiter).timeNow = func() time.Time {
return ts
}
assert.False(t, limiter.CheckCredit(1.0))
// on initialization, should have enough credits for 1 message
assert.True(t, limiter.CheckCredit(1.0))

// move time 20s forward, enough to accumulate credits for 2 messages, but it should still be capped at 1
limiter.(*rateLimiter).timeNow = func() time.Time {
Expand Down