Repository navigation
Commit 416c32d
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
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
18 | 32 | | |
19 | 33 | | |
20 | 34 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
| |||
14 | 15 | | |
15 | 16 | | |
16 | 17 | | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
24 | 26 | | |
25 | 27 | | |
26 | 28 | | |
27 | 29 | | |
28 | 30 | | |
29 | 31 | | |
30 | 32 | | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
31 | 59 | | |
32 | 60 | | |
33 | 61 | | |
34 | 62 | | |
35 | 63 | | |
36 | 64 | | |
37 | 65 | | |
| 66 | + | |
38 | 67 | | |
39 | 68 | | |
40 | 69 | | |
| |||
53 | 82 | | |
54 | 83 | | |
55 | 84 | | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
56 | 93 | | |
57 | 94 | | |
58 | 95 | | |
| |||
63 | 100 | | |
64 | 101 | | |
65 | 102 | | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
66 | 111 | | |
67 | 112 | | |
68 | 113 | | |
| |||
76 | 121 | | |
77 | 122 | | |
78 | 123 | | |
79 | | - | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
80 | 127 | | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
81 | 141 | | |
| 142 | + | |
82 | 143 | | |
83 | 144 | | |
84 | 145 | | |
85 | 146 | | |
86 | 147 | | |
87 | 148 | | |
88 | 149 | | |
89 | | - | |
| 150 | + | |
| 151 | + | |
90 | 152 | | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
91 | 169 | | |
92 | 170 | | |
93 | 171 | | |
| |||
99 | 177 | | |
100 | 178 | | |
101 | 179 | | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
102 | 192 | | |
103 | 193 | | |
104 | 194 | | |
| |||
122 | 212 | | |
123 | 213 | | |
124 | 214 | | |
| 215 | + | |
125 | 216 | | |
126 | 217 | | |
| 218 | + | |
127 | 219 | | |
128 | 220 | | |
129 | 221 | | |
130 | 222 | | |
131 | 223 | | |
132 | | - | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
133 | 228 | | |
134 | | - | |
135 | 229 | | |
136 | | - | |
137 | | - | |
138 | | - | |
139 | | - | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
140 | 233 | | |
| 234 | + | |
141 | 235 | | |
142 | | - | |
143 | | - | |
144 | 236 | | |
145 | 237 | | |
146 | 238 | | |
| |||
151 | 243 | | |
152 | 244 | | |
153 | 245 | | |
| 246 | + | |
154 | 247 | | |
155 | 248 | | |
| 249 | + | |
156 | 250 | | |
157 | 251 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
0 commit comments