Last active
October 9, 2019 07:24
-
-
Save HeshanSudarshana/369d585328c06ae97c2d23c9c14c4300 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