-
-
Notifications
You must be signed in to change notification settings - Fork 735
Add rate limiter exercise #3008
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 42 commits
Commits
Show all changes
49 commits
Select commit
Hold shift + click to select a range
0513d96
Run configlet create
andreatanky bfe4dbb
Update rate limiter implementation
andreatanky 81c6728
Add instructions
andreatanky ae33983
Add instructions
andreatanky 4f5abf3
Merge branch 'main' into add-rate-limiter-exercise
andreatanky 4f0c836
Remove interfaces
andreatanky a697b81
Update instructions
andreatanky 8092b9a
Merge branch 'add-rate-limiter-exercise' of github.com:andreatanky/ja…
andreatanky 87fc175
Add github handle to authors array
andreatanky 4479173
Add blurb
andreatanky d9520b7
Merge branch 'main' into add-rate-limiter-exercise
andreatanky 63ea8a3
Update long to use Duration
andreatanky 9f6a8bf
Update advance method to use Duration in tests
andreatanky a616a10
Remove unused advanceNanos method
andreatanky 9f077d0
Add non string tests
andreatanky 89ec426
Break down test cases
andreatanky 9657afc
Update to one sentence per line
andreatanky 98d6103
Update WindowState long type to use Instant
andreatanky 43610f1
Remove advanceNanos
andreatanky a61ef4d
Merge branch 'main' into add-rate-limiter-exercise
andreatanky 50b280c
Update naming of key to clientId
andreatanky 5971500
Merge branch 'add-rate-limiter-exercise' of github.com:andreatanky/ja…
andreatanky 69c1ba9
Update exercises/practice/rate-limiter/.docs/instructions.md
andreatanky 6f27c12
Update exercises/practice/rate-limiter/.docs/instructions.md
andreatanky 368d7ea
Remove custom object test
andreatanky 87c3d3c
Update exercises/practice/rate-limiter/src/test/java/RateLimiterTest.…
andreatanky d451060
Add tiny clock advances in tests
andreatanky 67baa14
Update difficulty to 4
andreatanky aba22d5
Add generic types to prereq
andreatanky 715f181
Update exercises/practice/rate-limiter/src/main/java/TimeSource.java
andreatanky efadb02
Update exercises/practice/rate-limiter/.meta/config.json
andreatanky 8dc4059
Update UUID test to include mixture of clock advance duration
andreatanky b6dea08
Merge branch 'add-rate-limiter-exercise' of github.com:andreatanky/ja…
andreatanky ae8d97e
Merge branch 'main' into add-rate-limiter-exercise
andreatanky 60736bb
Shift exercise in config.json
andreatanky 25a2876
Merge branch 'add-rate-limiter-exercise' of github.com:andreatanky/ja…
andreatanky d418567
Copy gradle wrapper
andreatanky baf39d2
Update exercises/practice/rate-limiter/.docs/instructions.md
andreatanky 7907757
Update exercises/practice/rate-limiter/.docs/instructions.md
andreatanky 6100e11
Add display name annotation
andreatanky 4d9b1bf
Merge branch 'add-rate-limiter-exercise' of github.com:andreatanky/ja…
andreatanky 6ccd97a
Run configlet to format config.json
andreatanky b839de1
Reformat files
andreatanky a8a6048
Update exercises/practice/rate-limiter/src/test/java/RateLimiterTest.…
andreatanky 6b8e310
Update exercises/practice/rate-limiter/src/test/java/RateLimiterTest.…
andreatanky b3667bc
Update exercises/practice/rate-limiter/src/test/java/RateLimiterTest.…
andreatanky 0b158f7
Update exercises/practice/rate-limiter/src/main/java/RateLimiter.java
andreatanky d5b13e9
Merge branch 'main' into add-rate-limiter-exercise
andreatanky 5e4cc5c
Update config.json
andreatanky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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
kahgoh marked this conversation as resolved.
Show resolved
Hide resolved
kahgoh marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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,23 @@ | ||
# Instructions | ||
|
||
Your task is to build a fixed‑window rate limiter. | ||
andreatanky marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Imagine a single server connected to one or more clients. | ||
A client sends a request, the server does some work, and returns a response. | ||
But processing takes time. | ||
If a client sends too many requests too quickly, the server can become overwhelmed — everything slows down or fails. | ||
|
||
A rate limiter is a small component that decides whether to allow or reject a request based on how frequently that client has been making requests. | ||
Different strategies exist; in this exercise you’ll implement a fixed‑window rate limiter. | ||
|
||
Fixed‑window rate limiting groups time into equal‑length windows (for example, every 10 seconds) and allows up to a certain number of requests within each window for each client. | ||
Once the window resets, the allowance refreshes for the next window. | ||
Each client is tracked separately, so another client can make requests within that same period. | ||
|
||
For example, consider a rate limiter configured to limit 2 requests per 10 seconds per client. | ||
Lets say a client sends a request: | ||
Check failure on line 18 in exercises/practice/rate-limiter/.docs/instructions.md
|
||
|
||
- Being its first request, the request is permitted. | ||
- A second request within 10 seconds after the first one is also permitted. | ||
- However, further requests after that would be denied _until_ at least 10 seconds has elapsed since the first request. | ||
- If a second client sends its first request within 10 seconds with of the first client's first request, it would also be permitted, _regardless_ of whether the first client has sent a second request. | ||
Check failure on line 23 in exercises/practice/rate-limiter/.docs/instructions.md
|
kahgoh marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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,21 @@ | ||
{ | ||
"authors": [ | ||
"andreatanky" | ||
], | ||
"files": { | ||
"solution": [ | ||
"src/main/java/RateLimiter.java" | ||
], | ||
"test": [ | ||
"src/test/java/RateLimiterTest.java" | ||
], | ||
"example": [ | ||
".meta/src/reference/java/RateLimiter.java", | ||
".meta/src/reference/java/TimeSource.java" | ||
], | ||
"editor": [ | ||
"src/main/java/TimeSource.java" | ||
] | ||
}, | ||
"blurb": "Practice stateful logic and time handling by implementing a fixed-window rate limiter" | ||
} |
49 changes: 49 additions & 0 deletions
49
exercises/practice/rate-limiter/.meta/src/reference/java/RateLimiter.java
This file contains hidden or 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,49 @@ | ||
import java.time.Duration; | ||
import java.time.Instant; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class RateLimiter<K> { | ||
|
||
private static final class WindowState { | ||
Instant windowStart; | ||
int usedCount; | ||
|
||
WindowState(Instant windowStart, int usedCount) { | ||
this.windowStart = windowStart; | ||
this.usedCount = usedCount; | ||
} | ||
} | ||
|
||
private final int limit; | ||
private final Duration windowSize; | ||
private final TimeSource timeSource; | ||
private final Map<K, WindowState> states = new HashMap<>(); | ||
|
||
public RateLimiter(int limit, Duration windowSize, TimeSource timeSource) { | ||
this.limit = limit; | ||
this.windowSize = windowSize; | ||
this.timeSource = timeSource; | ||
} | ||
|
||
public boolean allow(K clientId) { | ||
Instant now = timeSource.now(); | ||
|
||
WindowState s = states.get(clientId); | ||
if (s == null) { | ||
s = new WindowState(now, 0); | ||
states.put(clientId, s); | ||
} | ||
|
||
if (!now.isBefore(s.windowStart.plus(windowSize))) { | ||
s.windowStart = now; | ||
s.usedCount = 0; | ||
} | ||
|
||
if (s.usedCount < limit) { | ||
s.usedCount += 1; | ||
return true; | ||
} | ||
return false; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
exercises/practice/rate-limiter/.meta/src/reference/java/TimeSource.java
This file contains hidden or 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,18 @@ | ||
import java.time.Duration; | ||
import java.time.Instant; | ||
|
||
public class TimeSource { | ||
private Instant now; | ||
|
||
public TimeSource(Instant start) { | ||
this.now = start; | ||
} | ||
|
||
public Instant now() { | ||
return now; | ||
} | ||
|
||
public void advance(Duration d) { | ||
this.now = this.now.plus(d); | ||
} | ||
} |
This file contains hidden or 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,25 @@ | ||
plugins { | ||
id "java" | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
testImplementation platform("org.junit:junit-bom:5.10.0") | ||
testImplementation "org.junit.jupiter:junit-jupiter" | ||
testImplementation "org.assertj:assertj-core:3.25.1" | ||
|
||
testRuntimeOnly "org.junit.platform:junit-platform-launcher" | ||
} | ||
|
||
test { | ||
useJUnitPlatform() | ||
|
||
testLogging { | ||
exceptionFormat = "full" | ||
showStandardStreams = true | ||
events = ["passed", "failed", "skipped"] | ||
} | ||
} |
Binary file not shown.
7 changes: 7 additions & 0 deletions
7
exercises/practice/rate-limiter/gradle/wrapper/gradle-wrapper.properties
This file contains hidden or 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,7 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip | ||
networkTimeout=10000 | ||
validateDistributionUrl=true | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.