Skip to content

Commit 416c32d

Browse files
committed
perf: avoid timer & worker overhead in unlimited mode
NewUnlimited fakes unlimited access with a MaxUint32 bucket refilled every millisecond. Each instance starts a ticker and worker that wake even when nobody is calling Take, and Take itself still pays for a channel hop. Make unlimited mode explicit instead. Take returns immediately and CanTake reports true without a ticker, token channel, or background goroutine. That also drops the extra cost for unlimited keys in MultiLimiter and AutoLimiter. Keep runtime config working by spinning up a finite bucket on the first SetLimit or SetDuration call. Keep the initial MaxUint32 burst, apply limit changes at the next refill, and leave the default interval at 1 ms. The refill clock now starts at the first setter, not at construction. Serialize bucket init, config, and Stop. Drop the mutex before waiting for a token, and don't let setters create a bucket after Stop. Keep the original GetLimit value and reject non-positive refill durations. PD consumers this hits: - subfinder, urlfinder, and tldfinder: create an unlimited bucket for each selected source that has no positive source-specific or global rate limit, so idle workers pile up on a normal run. - nuclei: uses an unlimited global limiter when global limiting is off or per-host limiting is on, plus extra SDK paths. - httpx: picks unlimited mode when neither the per-second nor per-minute rate is positive. - dnsx: creates an unlimited limiter before checking the configured rate. A positive rate replaces it without stopping the old worker, so the idle work still happens even with a finite limit. - uncover: exposes unlimited mode through sources.NewSession when rateLimit <= 0. The normal service path already falls back to a finite 30/min for a zero rate. - [and some internal projects that can't be named here] Those call sites pick up the win after they adopt a release with this change. This is about exposure, not measured app-level CPU savings. Ten-run benchmark medians on Linux/amd64 with Go 1.26.5 and GOMAXPROCS=4: before after Take 835 ns/op 23.3 ns/op Parallel Take 1334 ns/op 60.5 ns/op MultiLimiter Take 631 ns/op 40.0 ns/op Idle CPU, 30 limiters 127 ms/s 2.03 ms/s Idle CPU, 1000 limiters 1473 ms/s 2.11 ms/s Idle workers, 1000 1000 0 Idle CPU after the change is basically the zero-limiter control (1.92 ms/s). These numbers are unlimited mode before any setter calls; configured limiters still use the finite bucket. Signed-off-by: Dwi Siswanto <git@dw1.io>
1 parent 2e17cb0 commit 416c32d

5 files changed

Lines changed: 388 additions & 17 deletions

File tree

‎README.md‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,20 @@ The original library i.e `golang.org/x/time/rate` implements classic **token buc
1515

1616
This allows scanners to respect maximum defined rate limits, pause until the allowed interval hits, and then process again at maximum speed. The original library slowed down requests according to the refill ratio.
1717

18+
## Unlimited mode
19+
20+
`NewUnlimited(ctx)` permits requests immediately without a ticker, token channel,
21+
or background goroutine. `CanTake` returns true and `GetLimit` initially returns
22+
`math.MaxUint32`. `MultiLimiter` and `AutoLimiter` use this behavior for unlimited keys.
23+
24+
For compatibility, calling `SetLimit` or `SetDuration` creates a finite burst bucket.
25+
The initial burst is `math.MaxUint32`; a changed limit applies at the next refill.
26+
The default refill interval is 1 ms. The refill schedule starts when the first
27+
setter is called. `SetDuration` requires a positive duration.
28+
29+
`Stop` prevents later setters from starting a bucket. Requests remain nonblocking
30+
when an unconfigured unlimited limiter is stopped or its context is canceled.
31+
1832
## Example
1933

2034
An Example showing usage of ratelimit as a library is specified below:

‎ratelimit.go‎

Lines changed: 111 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package ratelimit
33
import (
44
"context"
55
"math"
6+
"sync"
67
"sync/atomic"
78
"time"
89

@@ -14,27 +15,55 @@ var minusOne = ^uint32(0)
1415

1516
// Limiter allows a burst of request during the defined duration
1617
type Limiter struct {
17-
strategy Strategy
18-
maxCount atomic.Uint32
19-
interval time.Duration
20-
count atomic.Uint32
21-
ticker *time.Ticker
22-
tokens chan struct{}
23-
ctx context.Context
18+
unlimited *unlimitedLimiter
19+
strategy Strategy
20+
maxCount atomic.Uint32
21+
interval time.Duration
22+
count atomic.Uint32
23+
ticker *time.Ticker
24+
tokens chan struct{}
25+
ctx context.Context
2426
// internal
2527
cancelFunc context.CancelFunc
2628

2729
// wraps uber's leaky bucket limiter sizing it to the desired tokens per duration
2830
leakyBucketLimiter *rate.Limiter
2931
}
3032

33+
// unlimitedLimiter creates a finite bucket only when a caller changes its settings.
34+
// Its mutex protects initialization and Stop; Take never holds it while waiting.
35+
type unlimitedLimiter struct {
36+
mu sync.Mutex
37+
finite *Limiter
38+
stopped bool
39+
}
40+
41+
func (u *unlimitedLimiter) bucket() *Limiter {
42+
u.mu.Lock()
43+
defer u.mu.Unlock()
44+
45+
return u.finite
46+
}
47+
48+
// Called with unlimited.mu held. Preserve the initial burst until the first refill.
49+
func (limiter *Limiter) initUnlimitedBucket() *Limiter {
50+
u := limiter.unlimited
51+
if u.finite == nil && !u.stopped {
52+
u.finite = New(limiter.ctx, math.MaxUint32, limiter.interval)
53+
u.finite.SetLimit(limiter.GetLimit())
54+
}
55+
56+
return u.finite
57+
}
58+
3159
func (limiter *Limiter) run(ctx context.Context) {
3260
defer close(limiter.tokens)
3361
for {
3462
if limiter.count.Load() == 0 {
3563
<-limiter.ticker.C
3664
limiter.count.Store(limiter.maxCount.Load())
3765
}
66+
3867
select {
3968
case <-ctx.Done():
4069
// Internal Context
@@ -53,6 +82,14 @@ func (limiter *Limiter) run(ctx context.Context) {
5382

5483
// Take one token from the bucket
5584
func (limiter *Limiter) Take() {
85+
if limiter.unlimited != nil {
86+
if finite := limiter.unlimited.bucket(); finite != nil {
87+
finite.Take()
88+
}
89+
90+
return
91+
}
92+
5693
switch limiter.strategy {
5794
case LeakyBucket:
5895
_ = limiter.leakyBucketLimiter.Wait(context.TODO())
@@ -63,6 +100,14 @@ func (limiter *Limiter) Take() {
63100

64101
// CanTake checks if the rate limiter has any token
65102
func (limiter *Limiter) CanTake() bool {
103+
if limiter.unlimited != nil {
104+
if finite := limiter.unlimited.bucket(); finite != nil {
105+
return finite.CanTake()
106+
}
107+
108+
return true
109+
}
110+
66111
switch limiter.strategy {
67112
case LeakyBucket:
68113
return limiter.leakyBucketLimiter.Tokens() > 0
@@ -76,18 +121,51 @@ func (limiter *Limiter) GetLimit() uint {
76121
return uint(limiter.maxCount.Load())
77122
}
78123

79-
// GetLimit returns current rate limit per given duration
124+
// SetLimit changes the rate limit. Burst buckets apply it at the next refill.
125+
// On an unlimited limiter, it starts a finite bucket with a 1 ms interval unless
126+
// SetDuration has already changed the interval.
80127
func (limiter *Limiter) SetLimit(max uint) {
128+
if limiter.unlimited != nil {
129+
limiter.unlimited.mu.Lock()
130+
defer limiter.unlimited.mu.Unlock()
131+
132+
limiter.maxCount.Store(uint32(max))
133+
134+
if finite := limiter.initUnlimitedBucket(); finite != nil {
135+
finite.SetLimit(max)
136+
}
137+
138+
return
139+
}
140+
81141
limiter.maxCount.Store(uint32(max))
142+
82143
switch limiter.strategy {
83144
case LeakyBucket:
84145
limiter.leakyBucketLimiter.SetBurst(int(max))
85146
default:
86147
}
87148
}
88149

89-
// GetLimit returns current rate limit per given duration
150+
// SetDuration changes the refill interval. For burst buckets, it panics if d is not positive.
151+
// On an unlimited limiter, it starts a finite bucket using the current limit.
90152
func (limiter *Limiter) SetDuration(d time.Duration) {
153+
if limiter.unlimited != nil {
154+
limiter.unlimited.mu.Lock()
155+
defer limiter.unlimited.mu.Unlock()
156+
157+
if d <= 0 {
158+
panic("non-positive interval for Ticker.Reset")
159+
}
160+
limiter.interval = d
161+
162+
if finite := limiter.initUnlimitedBucket(); finite != nil {
163+
finite.SetDuration(d)
164+
}
165+
166+
return
167+
}
168+
91169
limiter.interval = d
92170
switch limiter.strategy {
93171
case LeakyBucket:
@@ -99,6 +177,18 @@ func (limiter *Limiter) SetDuration(d time.Duration) {
99177

100178
// Stop the rate limiter canceling the internal context
101179
func (limiter *Limiter) Stop() {
180+
if limiter.unlimited != nil {
181+
limiter.unlimited.mu.Lock()
182+
defer limiter.unlimited.mu.Unlock()
183+
184+
limiter.unlimited.stopped = true
185+
if limiter.unlimited.finite != nil {
186+
limiter.unlimited.finite.Stop()
187+
}
188+
189+
return
190+
}
191+
102192
switch limiter.strategy {
103193
case LeakyBucket: // NOP
104194
default:
@@ -122,25 +212,27 @@ func New(ctx context.Context, max uint, duration time.Duration) *Limiter {
122212
strategy: None,
123213
interval: duration,
124214
}
215+
125216
limiter.maxCount.Store(uint32(max))
126217
limiter.count.Store(uint32(max))
218+
127219
go limiter.run(internalctx)
128220

129221
return limiter
130222
}
131223

132-
// NewUnlimited create a bucket with approximated unlimited tokens
224+
// NewUnlimited creates a limiter whose Take never waits and CanTake returns true
225+
// until SetLimit or SetDuration is called.
226+
// It creates no timer, token channel, or background goroutine until SetLimit or
227+
// SetDuration configures a finite bucket. GetLimit initially returns math.MaxUint32.
133228
func NewUnlimited(ctx context.Context) *Limiter {
134-
internalctx, cancel := context.WithCancel(context.TODO())
135229
limiter := &Limiter{
136-
ticker: time.NewTicker(time.Millisecond),
137-
tokens: make(chan struct{}),
138-
ctx: ctx,
139-
cancelFunc: cancel,
230+
unlimited: &unlimitedLimiter{},
231+
ctx: ctx,
232+
interval: time.Millisecond,
140233
}
234+
141235
limiter.maxCount.Store(math.MaxUint32)
142-
limiter.count.Store(math.MaxUint32)
143-
go limiter.run(internalctx)
144236

145237
return limiter
146238
}
@@ -151,7 +243,9 @@ func NewLeakyBucket(ctx context.Context, max uint, duration time.Duration) *Limi
151243
strategy: LeakyBucket,
152244
leakyBucketLimiter: rate.NewLimiter(rate.Every(duration), int(max)),
153245
}
246+
154247
limiter.maxCount.Store(uint32(max))
155248
limiter.interval = duration
249+
156250
return limiter
157251
}

‎unlimited_benchmark_test.go‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package ratelimit
2+
3+
import (
4+
"context"
5+
"testing"
6+
)
7+
8+
func BenchmarkUnlimitedTake(b *testing.B) {
9+
limiter := NewUnlimited(context.Background())
10+
defer limiter.Stop()
11+
b.ReportAllocs()
12+
for b.Loop() {
13+
limiter.Take()
14+
}
15+
}
16+
17+
func BenchmarkUnlimitedTakeParallel(b *testing.B) {
18+
limiter := NewUnlimited(context.Background())
19+
defer limiter.Stop()
20+
b.ReportAllocs()
21+
b.ResetTimer()
22+
b.RunParallel(func(pb *testing.PB) {
23+
for pb.Next() {
24+
limiter.Take()
25+
}
26+
})
27+
}
28+
29+
func BenchmarkUnlimitedLifecycle(b *testing.B) {
30+
b.ReportAllocs()
31+
for b.Loop() {
32+
limiter := NewUnlimited(context.Background())
33+
limiter.Stop()
34+
// Wait for the old implementation to exit so workers do not accumulate.
35+
if limiter.tokens != nil {
36+
for range limiter.tokens {
37+
}
38+
}
39+
}
40+
}
41+
42+
func BenchmarkMultiLimiterUnlimitedTake(b *testing.B) {
43+
limiter, err := NewMultiLimiter(context.Background(), &Options{Key: "unlimited", IsUnlimited: true})
44+
if err != nil {
45+
b.Fatal(err)
46+
}
47+
defer limiter.Stop()
48+
b.ReportAllocs()
49+
for b.Loop() {
50+
if err := limiter.Take("unlimited"); err != nil {
51+
b.Fatal(err)
52+
}
53+
}
54+
}

‎unlimited_idle_linux_test.go‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package ratelimit
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"runtime"
7+
"syscall"
8+
"testing"
9+
"time"
10+
)
11+
12+
// Run separately from other benchmarks: CPU usage includes the entire process.
13+
func BenchmarkUnlimitedIdle(b *testing.B) {
14+
for _, n := range []int{0, 30, 1000} {
15+
b.Run(fmt.Sprint(n), func(b *testing.B) {
16+
before := runtime.NumGoroutine()
17+
limiters := make([]*Limiter, n)
18+
for i := range limiters {
19+
limiters[i] = NewUnlimited(context.Background())
20+
}
21+
defer func() {
22+
for _, limiter := range limiters {
23+
limiter.Stop()
24+
}
25+
for _, limiter := range limiters {
26+
if limiter.tokens != nil {
27+
for range limiter.tokens {
28+
}
29+
}
30+
}
31+
}()
32+
workers := runtime.NumGoroutine() - before
33+
cpuTime := func() time.Duration {
34+
var usage syscall.Rusage
35+
if err := syscall.Getrusage(syscall.RUSAGE_SELF, &usage); err != nil {
36+
b.Fatal(err)
37+
}
38+
return time.Duration(usage.Utime.Nano() + usage.Stime.Nano())
39+
}
40+
b.ResetTimer()
41+
startCPU, start := cpuTime(), time.Now()
42+
for i := 0; i < b.N; i++ {
43+
time.Sleep(100 * time.Millisecond)
44+
}
45+
elapsed, cpu := time.Since(start), cpuTime()-startCPU
46+
b.StopTimer()
47+
b.ReportMetric(float64(cpu.Nanoseconds())/elapsed.Seconds(), "cpu-ns/s")
48+
b.ReportMetric(float64(workers), "goroutines")
49+
})
50+
}
51+
}

0 commit comments

Comments
 (0)
⌂ 换网址