From 94c29189a53e34307cb1b7533ac6f4cc7eed7fea Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Tue, 15 Oct 2024 12:47:26 +0530 Subject: [PATCH 1/2] feat: Add `TokenBucket` new algorithm with Junit tests --- .../datastructures/queues/TokenBucket.java | 61 +++++++++++++ .../queues/TokenBucketTest.java | 90 +++++++++++++++++++ 2 files changed, 151 insertions(+) create mode 100644 src/main/java/com/thealgorithms/datastructures/queues/TokenBucket.java create mode 100644 src/test/java/com/thealgorithms/datastructures/queues/TokenBucketTest.java diff --git a/src/main/java/com/thealgorithms/datastructures/queues/TokenBucket.java b/src/main/java/com/thealgorithms/datastructures/queues/TokenBucket.java new file mode 100644 index 000000000000..999c963fab93 --- /dev/null +++ b/src/main/java/com/thealgorithms/datastructures/queues/TokenBucket.java @@ -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; + } +} diff --git a/src/test/java/com/thealgorithms/datastructures/queues/TokenBucketTest.java b/src/test/java/com/thealgorithms/datastructures/queues/TokenBucketTest.java new file mode 100644 index 000000000000..959ea8724f8b --- /dev/null +++ b/src/test/java/com/thealgorithms/datastructures/queues/TokenBucketTest.java @@ -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()); + } +} From fa418bf2c61f22787247f9909584813a76c617ba Mon Sep 17 00:00:00 2001 From: Hardvan Date: Tue, 15 Oct 2024 07:17:43 +0000 Subject: [PATCH 2/2] Update directory --- DIRECTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 6073904a745e..5ff9817fa817 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -197,6 +197,7 @@ * [PriorityQueues](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/queues/PriorityQueues.java) * [Queue](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/queues/Queue.java) * [QueueByTwoStacks](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/queues/QueueByTwoStacks.java) + * [TokenBucket](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/queues/TokenBucket.java) * stacks * [NodeStack](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/stacks/NodeStack.java) * [ReverseStack](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/stacks/ReverseStack.java) @@ -819,6 +820,7 @@ * [PriorityQueuesTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/queues/PriorityQueuesTest.java) * [QueueByTwoStacksTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/queues/QueueByTwoStacksTest.java) * [QueueTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/queues/QueueTest.java) + * [TokenBucketTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/queues/TokenBucketTest.java) * stacks * [LinkedListStackTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/stacks/LinkedListStackTest.java) * [StackArrayListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/stacks/StackArrayListTest.java)