Advertisement
pshishkin

Untitled

Apr 25th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1.  
  2.  
  3. /**
  4. * @author chmilevfa
  5. * @since 24.04.17
  6. */
  7. public class RpsChecker {
  8.  
  9. private final int RATE = 1000;
  10.  
  11. private int rpr; //requests per rate
  12. private long lastTime;
  13. private double threshold;
  14.  
  15. public RpsChecker(int rpr) {
  16. this.rpr = rpr;
  17. this.lastTime = System.currentTimeMillis();
  18. this.threshold = 0;
  19. }
  20.  
  21. public boolean isRequestAllowed() {
  22. long currentTime = System.currentTimeMillis();
  23. long diffTime = currentTime - lastTime;
  24. lastTime = currentTime;
  25.  
  26. if (threshold > rpr) {
  27. threshold = rpr;
  28. }
  29. threshold += diffTime * (rpr/RATE);
  30. if (threshold < 1.0) {
  31. return false;
  32. } else {
  33. threshold -= 1;
  34. return true;
  35. }
  36. }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement