-
Notifications
You must be signed in to change notification settings - Fork 149
Add an implicit rate limiter to help with bulk-operation security (Tested) #327
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
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a44b028
Merge pull request #1 from HypixelDev/master
HydroPage 693dbdb
Revision one
HydroPage 6a92106
Follow repo conventions :P
HydroPage f7be5aa
Make RateLimiter use its own local lock instead
HydroPage 836db47
Hmmm, nevermind. I don't want the lock to be public
HydroPage 21d94d7
Missed some sync safety
HydroPage f35d1b4
Docs and conceptual decoupling
HydroPage bdddce7
Last little decoupling tweak, I think
HydroPage 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
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
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,62 @@ | ||
package net.hypixel.api.util; | ||
|
||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
|
||
import static java.util.concurrent.TimeUnit.MINUTES; | ||
|
||
public class RateLimiter { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should make sure to format this class similar to other classes in the project, using 4 spaces for indentation instead of tabs are the main thing |
||
private final Object lock = new Object(); | ||
|
||
private final ScheduledExecutorService resetter = Executors.newSingleThreadScheduledExecutor(); | ||
private volatile int limitPerMinute; | ||
private int actionsThisMinute; | ||
|
||
/** | ||
* @param limitPerMinute Maximum amount of times {@link RateLimiter#beforeAction()} can be called in the same minute | ||
* before it begins blocking threads until the next minute. | ||
*/ | ||
public RateLimiter(int limitPerMinute) { | ||
this.limitPerMinute = limitPerMinute; | ||
resetter.scheduleAtFixedRate(this::reset, 1, 1, MINUTES); | ||
} | ||
|
||
private void reset() { | ||
synchronized ( lock ) { | ||
actionsThisMinute = 0; | ||
lock.notifyAll(); | ||
} | ||
} | ||
|
||
/** | ||
* Set the action queue limit to be used by {@link RateLimiter#beforeAction()}. | ||
* | ||
* @param limitPerMinute The new limit. | ||
*/ | ||
public void setRate(int limitPerMinute) { | ||
this.limitPerMinute = limitPerMinute; | ||
} | ||
|
||
/** | ||
* Blocks the current thread in the event that the action queue has been filled for this minute. | ||
* Unblocks when the queue refreshes. | ||
* | ||
* @throws InterruptedException If interrupted while waiting. | ||
*/ | ||
public void beforeAction() throws InterruptedException { | ||
synchronized ( lock ) { | ||
while (actionsThisMinute >= limitPerMinute) { | ||
lock.wait(); | ||
} | ||
|
||
actionsThisMinute++; | ||
} | ||
} | ||
|
||
/** | ||
* Shut down the internal executor service. | ||
*/ | ||
public void shutdown() { | ||
resetter.shutdown(); | ||
} | ||
} |
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.