Skip to content

Commit 4afdc8d

Browse files
committed
Use AtomicLongMap to calculate attempts
1 parent e85e988 commit 4afdc8d

File tree

3 files changed

+19
-25
lines changed

3 files changed

+19
-25
lines changed

extended/src/main/java/io/kubernetes/client/extended/workqueue/ratelimiter/BucketRateLimiter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public BucketRateLimiter(long capacity, long tokensGeneratedInPeriod, Duration p
3030
}
3131

3232
@Override
33-
public synchronized Duration when(T item) {
33+
public Duration when(T item) {
3434
DelayGetter delayGetter = new DelayGetter();
3535
bucket.asAsyncScheduler().consume(1, delayGetter).complete(null);
3636
return delayGetter.getDelay();
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package io.kubernetes.client.extended.workqueue.ratelimiter;
22

3+
import com.google.common.util.concurrent.AtomicLongMap;
34
import java.time.Duration;
4-
import java.util.HashMap;
5-
import java.util.Map;
65

76
/**
87
* ItemExponentialFailureRateLimiter does a simple baseDelay*10<sup>num-failures</sup> limit dealing
@@ -13,30 +12,29 @@ public class ItemExponentialFailureRateLimiter<T> implements RateLimiter<T> {
1312
private Duration baseDelay;
1413
private Duration maxDelay;
1514

16-
private Map<T, Integer> failures;
15+
private AtomicLongMap<T> failures;
1716

1817
public ItemExponentialFailureRateLimiter(Duration baseDelay, Duration maxDelay) {
1918
this.baseDelay = baseDelay;
2019
this.maxDelay = maxDelay;
2120

22-
failures = new HashMap<>();
21+
failures = AtomicLongMap.create();
2322
}
2423

2524
@Override
26-
public synchronized Duration when(T item) {
27-
int exp = failures.getOrDefault(item, 0);
28-
failures.put(item, exp + 1);
25+
public Duration when(T item) {
26+
long exp = failures.getAndIncrement(item);
2927
long d = maxDelay.toMillis() >> exp;
3028
return d > baseDelay.toMillis() ? baseDelay.multipliedBy(1 << exp) : maxDelay;
3129
}
3230

3331
@Override
34-
public synchronized void forget(T item) {
32+
public void forget(T item) {
3533
failures.remove(item);
3634
}
3735

3836
@Override
39-
public synchronized int numRequeues(T item) {
40-
return failures.getOrDefault(item, 0);
37+
public int numRequeues(T item) {
38+
return (int) failures.get(item);
4139
}
4240
}
Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,44 @@
11
package io.kubernetes.client.extended.workqueue.ratelimiter;
22

3+
import com.google.common.util.concurrent.AtomicLongMap;
34
import java.time.Duration;
4-
import java.util.HashMap;
5-
import java.util.Map;
65

76
/**
87
* ItemFastSlowRateLimiter does a quick retry for a certain number of attempts, then a slow retry
98
* after that
109
*/
1110
public class ItemFastSlowRateLimiter<T> implements RateLimiter<T> {
1211

13-
private Map<T, Integer> failures;
14-
1512
private Duration fastDelay;
1613
private Duration slowDelay;
1714
private int maxFastAttempts;
1815

16+
private AtomicLongMap<T> failures;
17+
1918
public ItemFastSlowRateLimiter(Duration fastDelay, Duration slowDelay, int maxFastAttempts) {
2019
this.fastDelay = fastDelay;
2120
this.slowDelay = slowDelay;
2221
this.maxFastAttempts = maxFastAttempts;
2322

24-
failures = new HashMap<>();
23+
failures = AtomicLongMap.create();
2524
}
2625

2726
@Override
28-
public synchronized Duration when(T item) {
29-
int attempts = failures.getOrDefault(item, 0);
30-
failures.put(item, attempts + 1);
31-
32-
if (attempts + 1 <= maxFastAttempts) {
27+
public Duration when(T item) {
28+
long attempts = failures.incrementAndGet(item);
29+
if (attempts <= maxFastAttempts) {
3330
return fastDelay;
3431
}
35-
3632
return slowDelay;
3733
}
3834

3935
@Override
40-
public synchronized void forget(T item) {
36+
public void forget(T item) {
4137
failures.remove(item);
4238
}
4339

4440
@Override
45-
public synchronized int numRequeues(T item) {
46-
return failures.getOrDefault(item, 0);
41+
public int numRequeues(T item) {
42+
return (int) failures.get(item);
4743
}
4844
}

0 commit comments

Comments
 (0)