Advertisement
Guest User

Untitled

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