-
Notifications
You must be signed in to change notification settings - Fork 19.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/main/java/com/thealgorithms/datastructures/queues/TokenBucket.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.thealgorithms.datastructures.queues; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* TokenBucket implements a token bucket rate limiter algorithm. | ||
* This class is used to control the rate of requests in a distributed system. | ||
* It allows a certain number of requests (tokens) to be processed in a time frame, | ||
* based on the defined refill rate. | ||
* | ||
* Applications: Computer networks, API rate limiting, distributed systems, etc. | ||
* | ||
* @author Hardvan | ||
*/ | ||
public final class TokenBucket { | ||
private final int maxTokens; | ||
private final int refillRate; // tokens per second | ||
private int tokens; | ||
private long lastRefill; // Timestamp in nanoseconds | ||
|
||
/** | ||
* Constructs a TokenBucket instance. | ||
* | ||
* @param maxTokens Maximum number of tokens the bucket can hold. | ||
* @param refillRate The rate at which tokens are refilled (tokens per second). | ||
*/ | ||
public TokenBucket(int maxTokens, int refillRate) { | ||
this.maxTokens = maxTokens; | ||
this.refillRate = refillRate; | ||
this.tokens = maxTokens; | ||
this.lastRefill = System.nanoTime(); | ||
} | ||
|
||
/** | ||
* Attempts to allow a request based on the available tokens. | ||
* If a token is available, it decrements the token count and allows the request. | ||
* Otherwise, the request is denied. | ||
* | ||
* @return true if the request is allowed, false if the request is denied. | ||
*/ | ||
public synchronized boolean allowRequest() { | ||
refillTokens(); | ||
if (tokens > 0) { | ||
tokens--; | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* Refills the tokens based on the time elapsed since the last refill. | ||
* The number of tokens to be added is calculated based on the elapsed time | ||
* and the refill rate, ensuring the total does not exceed maxTokens. | ||
*/ | ||
private void refillTokens() { | ||
long now = System.nanoTime(); | ||
long tokensToAdd = (now - lastRefill) / TimeUnit.SECONDS.toNanos(1) * refillRate; | ||
tokens = Math.min(maxTokens, tokens + (int) tokensToAdd); | ||
lastRefill = now; | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
src/test/java/com/thealgorithms/datastructures/queues/TokenBucketTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package com.thealgorithms.datastructures.queues; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
public class TokenBucketTest { | ||
|
||
@Test | ||
public void testRateLimiter() throws InterruptedException { | ||
TokenBucket bucket = new TokenBucket(5, 1); | ||
for (int i = 0; i < 5; i++) { | ||
assertTrue(bucket.allowRequest()); | ||
} | ||
assertFalse(bucket.allowRequest()); | ||
Thread.sleep(1000); | ||
assertTrue(bucket.allowRequest()); | ||
} | ||
|
||
@Test | ||
public void testRateLimiterWithExceedingRequests() throws InterruptedException { | ||
TokenBucket bucket = new TokenBucket(3, 1); | ||
|
||
for (int i = 0; i < 3; i++) { | ||
assertTrue(bucket.allowRequest()); | ||
} | ||
assertFalse(bucket.allowRequest()); | ||
|
||
Thread.sleep(1000); | ||
assertTrue(bucket.allowRequest()); | ||
assertFalse(bucket.allowRequest()); | ||
} | ||
|
||
@Test | ||
public void testRateLimiterMultipleRefills() throws InterruptedException { | ||
TokenBucket bucket = new TokenBucket(2, 1); | ||
|
||
assertTrue(bucket.allowRequest()); | ||
assertTrue(bucket.allowRequest()); | ||
assertFalse(bucket.allowRequest()); | ||
|
||
Thread.sleep(1000); | ||
assertTrue(bucket.allowRequest()); | ||
|
||
Thread.sleep(1000); | ||
assertTrue(bucket.allowRequest()); | ||
assertFalse(bucket.allowRequest()); | ||
} | ||
|
||
@Test | ||
public void testRateLimiterEmptyBucket() { | ||
TokenBucket bucket = new TokenBucket(0, 1); | ||
|
||
assertFalse(bucket.allowRequest()); | ||
} | ||
|
||
@Test | ||
public void testRateLimiterWithHighRefillRate() throws InterruptedException { | ||
TokenBucket bucket = new TokenBucket(5, 10); | ||
|
||
for (int i = 0; i < 5; i++) { | ||
assertTrue(bucket.allowRequest()); | ||
} | ||
|
||
assertFalse(bucket.allowRequest()); | ||
|
||
Thread.sleep(1000); | ||
|
||
for (int i = 0; i < 5; i++) { | ||
assertTrue(bucket.allowRequest()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testRateLimiterWithSlowRequests() throws InterruptedException { | ||
TokenBucket bucket = new TokenBucket(5, 1); | ||
|
||
for (int i = 0; i < 5; i++) { | ||
assertTrue(bucket.allowRequest()); | ||
} | ||
|
||
Thread.sleep(1000); | ||
assertTrue(bucket.allowRequest()); | ||
|
||
Thread.sleep(2000); | ||
assertTrue(bucket.allowRequest()); | ||
assertTrue(bucket.allowRequest()); | ||
} | ||
} |