-
Notifications
You must be signed in to change notification settings - Fork 1
/
SchedulingExample.java
23 lines (19 loc) · 941 Bytes
/
SchedulingExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package io.github.meemaw.ratelimit.examples;
import io.github.ratelimit.core.RateLimitAlgorithm;
import io.github.ratelimit.core.RateLimitEntry;
import io.github.ratelimit.core.RateLimiting;
import io.github.ratelimit.core.RefillPolicy;
import io.github.ratelimit.core.impl.EntryBuilder;
import io.github.ratelimit.core.impl.SimpleRefillPolicy;
public class SchedulingExample {
public static void main(String[] args) {
EntryBuilder builder = RateLimiting.schedulerBuilder().withAlgorithm(RateLimitAlgorithm.TOKEN_BUCKET);
RefillPolicy policy = SimpleRefillPolicy.perSecond(2);
RateLimitEntry record = builder.withRefillPolicy(policy).build();
long start = System.currentTimeMillis();
while (record.tryConsume(1)) {
double secondsPassed = (System.currentTimeMillis() - start) / 1000.0;
System.out.println(secondsPassed); // or someVeryExpensiveTask();
}
}
}