Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @author chmilevfa
- * @since 24.04.17
- */
- public class RpsChecker {
- private final int RATE = 1000;
- private int rpr; //requests per rate
- private long lastTime;
- private double threshold;
- public RpsChecker(int rpr) {
- this.rpr = rpr;
- this.lastTime = System.currentTimeMillis();
- this.threshold = 0;
- }
- public boolean isRequestAllowed() {
- long currentTime = System.currentTimeMillis();
- long diffTime = currentTime - lastTime;
- lastTime = currentTime;
- if (threshold > rpr) {
- threshold = rpr;
- }
- threshold += diffTime * (rpr/RATE);
- if (threshold < 1.0) {
- return false;
- } else {
- threshold -= 1;
- return true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement