Forked from HeshanSudarshana/rateLimitterImplementation.java
Created
October 9, 2019 07:24
-
-
Save zhangw/e2a0b25f1c01c32877684709637f2c99 to your computer and use it in GitHub Desktop.
RateLimitter Implementation
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
| // if you are allowing 5 requests per second | |
| final RateLimiter rateLimiter = RateLimiter.create(5.0); | |
| void throttler() { | |
| rateLimiter.acquire(); // may wait | |
| doSomething(); | |
| } | |
| // if you are allowing 5000 bytes per second | |
| final RateLimiter rateLimiter = RateLimiter.create(5000.0); | |
| void submitPacket(byte[] packet) { | |
| rateLimiter.acquire(packet.length); | |
| networkService.send(packet); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment