-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHostRateLimiter.java
More file actions
79 lines (72 loc) · 3.1 KB
/
Copy pathHostRateLimiter.java
File metadata and controls
79 lines (72 loc) · 3.1 KB
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
package com.openelements.content;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import java.util.function.LongConsumer;
import java.util.function.LongSupplier;
/**
* A simple per-host rate limiter that spaces requests to the same host to a maximum rate, while
* treating different hosts independently.
*
* <p>The clock and the sleep operation are injected so the limiter is fully testable without real
* time: production wires {@link System#nanoTime()} and {@link Thread#sleep(long)}, tests supply a
* virtual clock and a recording sleeper.
*/
public class HostRateLimiter {
private final long minIntervalNanos;
private final LongSupplier clockNanos;
private final LongConsumer sleeperMillis;
private final Map<String, Long> nextAllowedNanos = new HashMap<>();
/**
* @param permitsPerSecond maximum requests per second per host (values {@code <= 0} disable throttling)
* @param clockNanos monotonic time source in nanoseconds
* @param sleeperMillis blocks the caller for the given number of milliseconds
*/
public HostRateLimiter(double permitsPerSecond, LongSupplier clockNanos, LongConsumer sleeperMillis) {
this.minIntervalNanos = permitsPerSecond <= 0 ? 0L : (long) (1_000_000_000.0 / permitsPerSecond);
this.clockNanos = clockNanos;
this.sleeperMillis = sleeperMillis;
}
/**
* Blocks until a request to {@code host} is allowed under the configured rate, then reserves the
* next slot for that host.
*
* @param host the target host (an empty or {@code null} host shares a single bucket)
*/
public void acquire(String host) {
acquire(host, Duration.ZERO);
}
/**
* Like {@link #acquire(String)}, but enforces at least {@code minInterval} between requests to the
* host — used to honor a {@code robots.txt} {@code Crawl-delay}, taking the stricter of the
* configured rate and the crawl delay.
*
* @param host the target host
* @param minInterval a minimum interval to enforce (e.g. from {@code Crawl-delay}); may be zero
*/
public void acquire(String host, Duration minInterval) {
long effectiveIntervalNanos = Math.max(minIntervalNanos, minInterval == null ? 0L : minInterval.toNanos());
if (effectiveIntervalNanos == 0L) {
return;
}
String key = host == null ? "" : host;
long waitNanos;
synchronized (this) {
long now = clockNanos.getAsLong();
long start = Math.max(now, nextAllowedNanos.getOrDefault(key, now));
waitNanos = start - now;
nextAllowedNanos.put(key, start + effectiveIntervalNanos);
}
if (waitNanos > 0) {
sleeperMillis.accept(Math.max(1, Math.round(waitNanos / 1_000_000.0)));
}
}
/** Production sleeper: blocks the current thread, restoring the interrupt flag if interrupted. */
static void sleepMillis(long millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}