Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.61 KB | None | 0 0
  1. public class TokenBucketRecord extends AbstractTokenRecord {
  2.  
  3.     protected Instant lastTokenDistributedTs;
  4.  
  5.     @Override
  6.     public RateLimitRecord updateWith(RefillPolicy policy, Instant requestTs) {
  7.         Duration timeElapsed = Duration.between(lastTokenDistributedTs, requestTs);
  8.         int numNewTokens = policy.distributeNewTokens(timeElapsed);
  9.         if (numNewTokens > 0) {
  10.             long newTokenCount = this.tokenCount + numNewTokens;
  11.             long capacity = policy.getCapacity();
  12.             if (newTokenCount > capacity) {
  13.                 newTokenCount = capacity;
  14.             }
  15.             return TokenBucketRecord.of(newTokenCount, requestTs);
  16.         }
  17.         return this;
  18.     }
  19.  
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement