/
RateLimiter.java
315 lines (278 loc) · 12.1 KB
/
RateLimiter.java
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.pulsar.common.util;
import static com.google.common.base.Preconditions.checkArgument;
import com.google.common.base.MoreObjects;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import lombok.Builder;
/**
* A Rate Limiter that distributes permits at a configurable rate. Each {@link #acquire()} blocks if necessary until a
* permit is available, and then takes it. Each {@link #tryAcquire()} tries to acquire permits from available permits,
* it returns true if it succeed else returns false. Rate limiter release configured permits at every configured rate
* time, so, on next ticket new fresh permits will be available.
*
* <p>For example: if RateLimiter is configured to release 10 permits at every 1 second then RateLimiter will allow to
* acquire 10 permits at any time with in that 1 second.
*
* <p>Comparison with other RateLimiter such as {@link com.google.common.util.concurrent.RateLimiter}
* <ul>
* <li><b>Per second rate-limiting:</b> Per second rate-limiting not satisfied by Guava-RateLimiter</li>
* <li><b>Guava RateLimiter:</b> For X permits: it releases X/1000 permits every msec. therefore,
* for permits=2/sec => it release 1st permit on first 500msec and 2nd permit on next 500ms. therefore,
* if 2 request comes with in 500msec duration then 2nd request fails to acquire permit
* though we have configured 2 permits/second.</li>
* <li><b>RateLimiter:</b> it releases X permits every second. so, in above usecase:
* if 2 requests comes at the same time then both will acquire the permit.</li>
* <li><b>Faster: </b>RateLimiter is light-weight and faster than Guava-RateLimiter</li>
* </ul>
*/
public class RateLimiter implements AutoCloseable{
private final ScheduledExecutorService executorService;
private long rateTime;
private TimeUnit timeUnit;
private final boolean externalExecutor;
private ScheduledFuture<?> renewTask;
private volatile long permits;
private volatile long acquiredPermits;
private boolean isClosed;
// permitUpdate helps to update permit-rate at runtime
private Supplier<Long> permitUpdater;
private RateLimitFunction rateLimitFunction;
private boolean isDispatchOrPrecisePublishRateLimiter;
public RateLimiter(final long permits, final long rateTime, final TimeUnit timeUnit) {
this(null, permits, rateTime, timeUnit);
}
public RateLimiter(final long permits, final long rateTime, final TimeUnit timeUnit, boolean isDispatchOrPrecisePublishRateLimiter) {
this(null, permits, rateTime, timeUnit, null, isDispatchOrPrecisePublishRateLimiter);
}
public RateLimiter(final long permits, final long rateTime, final TimeUnit timeUnit,
RateLimitFunction autoReadResetFunction) {
this(null, permits, rateTime, timeUnit, null, false);
this.rateLimitFunction = autoReadResetFunction;
}
public RateLimiter(final long permits, final long rateTime, final TimeUnit timeUnit,
RateLimitFunction autoReadResetFunction, boolean isDispatchOrPrecisePublishRateLimiter) {
this(null, permits, rateTime, timeUnit, null, isDispatchOrPrecisePublishRateLimiter);
this.rateLimitFunction = autoReadResetFunction;
}
public RateLimiter(final ScheduledExecutorService service, final long permits, final long rateTime,
final TimeUnit timeUnit) {
this(service, permits, rateTime, timeUnit, (Supplier<Long>) null);
}
public RateLimiter(final ScheduledExecutorService service, final long permits, final long rateTime,
final TimeUnit timeUnit, Supplier<Long> permitUpdater) {
this(service, permits, rateTime, timeUnit, permitUpdater, false);
}
public RateLimiter(final ScheduledExecutorService service, final long permits, final long rateTime,
final TimeUnit timeUnit, Supplier<Long> permitUpdater, boolean isDispatchOrPrecisePublishRateLimiter) {
this(service, permits, rateTime, timeUnit, permitUpdater, isDispatchOrPrecisePublishRateLimiter,
null);
}
@Builder
RateLimiter(final ScheduledExecutorService scheduledExecutorService, final long permits, final long rateTime,
final TimeUnit timeUnit, Supplier<Long> permitUpdater, boolean isDispatchOrPrecisePublishRateLimiter,
RateLimitFunction rateLimitFunction) {
checkArgument(permits > 0, "rate must be > 0");
checkArgument(rateTime > 0, "Renew permit time must be > 0");
this.rateTime = rateTime;
this.timeUnit = timeUnit;
this.permits = permits;
this.permitUpdater = permitUpdater;
this.isDispatchOrPrecisePublishRateLimiter = isDispatchOrPrecisePublishRateLimiter;
if (scheduledExecutorService != null) {
this.executorService = scheduledExecutorService;
this.externalExecutor = true;
} else {
final ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
executor.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
executor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
this.executorService = executor;
this.externalExecutor = false;
}
this.rateLimitFunction = rateLimitFunction;
}
// default values for Lombok generated builder class
public static class RateLimiterBuilder {
private long rateTime = 1;
private TimeUnit timeUnit = TimeUnit.SECONDS;
}
@Override
public synchronized void close() {
if (!isClosed) {
if (!externalExecutor) {
executorService.shutdownNow();
}
if (renewTask != null) {
renewTask.cancel(false);
}
isClosed = true;
}
}
public synchronized boolean isClosed() {
return isClosed;
}
/**
* Acquires the given number of permits from this {@code RateLimiter}, blocking until the request be granted.
*
* <p>This method is equivalent to {@code acquire(1)}.
*/
public synchronized void acquire() throws InterruptedException {
acquire(1);
}
/**
* Acquires the given number of permits from this {@code RateLimiter}, blocking until the request be granted.
*
* @param acquirePermit
* the number of permits to acquire
*/
public synchronized void acquire(long acquirePermit) throws InterruptedException {
checkArgument(!isClosed(), "Rate limiter is already shutdown");
checkArgument(acquirePermit <= this.permits,
"acquiring permits must be less or equal than initialized rate =" + this.permits);
// lazy init and start task only once application start using it
if (renewTask == null) {
renewTask = createTask();
}
boolean canAcquire = false;
do {
canAcquire = acquirePermit < 0 || acquiredPermits < this.permits;
if (!canAcquire) {
wait();
} else {
acquiredPermits += acquirePermit;
}
} while (!canAcquire);
}
/**
* Acquires permits from this {@link RateLimiter} if it can be acquired immediately without delay.
*
* <p>This method is equivalent to {@code tryAcquire(1)}.
*
* @return {@code true} if the permits were acquired, {@code false} otherwise
*/
public synchronized boolean tryAcquire() {
return tryAcquire(1);
}
/**
* Acquires permits from this {@link RateLimiter} if it can be acquired immediately without delay.
*
* @param acquirePermit
* the number of permits to acquire
* @return {@code true} if the permits were acquired, {@code false} otherwise
*/
public synchronized boolean tryAcquire(long acquirePermit) {
checkArgument(!isClosed(), "Rate limiter is already shutdown");
// lazy init and start task only once application start using it
if (renewTask == null) {
renewTask = createTask();
}
boolean canAcquire = acquirePermit < 0 || acquiredPermits < this.permits;
if (isDispatchOrPrecisePublishRateLimiter) {
// for dispatch rate limiter just add acquirePermit
acquiredPermits += acquirePermit;
// we want to back-pressure from the current state of the rateLimiter therefore we should check if there
// are any available premits again
canAcquire = acquirePermit < 0 || acquiredPermits < this.permits;
} else {
// acquired-permits can't be larger than the rate
if (acquirePermit > this.permits) {
acquiredPermits = this.permits;
return false;
}
if (canAcquire) {
acquiredPermits += acquirePermit;
}
}
return canAcquire;
}
/**
* Return available permits for this {@link RateLimiter}.
*
* @return returns 0 if permits is not available
*/
public long getAvailablePermits() {
return Math.max(0, this.permits - this.acquiredPermits);
}
/**
* Resets new rate by configuring new value for permits per configured rate-period.
*
* @param permits
*/
public synchronized void setRate(long permits) {
this.permits = permits;
}
/**
* Resets new rate with new permits and rate-time.
*
* @param permits
* @param rateTime
* @param timeUnit
* @param permitUpdaterByte
*/
public synchronized void setRate(long permits, long rateTime, TimeUnit timeUnit, Supplier<Long> permitUpdaterByte) {
if (renewTask != null) {
renewTask.cancel(false);
}
this.permits = permits;
this.rateTime = rateTime;
this.timeUnit = timeUnit;
this.permitUpdater = permitUpdaterByte;
this.renewTask = createTask();
}
/**
* Returns configured permit rate per pre-configured rate-period.
*
* @return rate
*/
public synchronized long getRate() {
return this.permits;
}
public synchronized long getRateTime() {
return this.rateTime;
}
public synchronized TimeUnit getRateTimeUnit() {
return this.timeUnit;
}
protected ScheduledFuture<?> createTask() {
return executorService.scheduleAtFixedRate(this::renew, this.rateTime, this.rateTime, this.timeUnit);
}
synchronized void renew() {
acquiredPermits = isDispatchOrPrecisePublishRateLimiter ? Math.max(0, acquiredPermits - permits) : 0;
if (permitUpdater != null) {
long newPermitRate = permitUpdater.get();
if (newPermitRate > 0) {
setRate(newPermitRate);
}
}
// release the back-pressure by applying the rateLimitFunction only when there are available permits
if (rateLimitFunction != null && this.getAvailablePermits() > 0) {
rateLimitFunction.apply();
}
notifyAll();
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this).add("rateTime", rateTime).add("permits", permits)
.add("acquiredPermits", acquiredPermits).toString();
}
}