2

We are using guava 16.0.1

On RateLimiter.create(MaxRequestsPerSecond), I cannot burst right away. We would like to allow this as customer rate limiters are only created on their first request and are cached right now(too many customers to hold all of them in).

Ideally, I would just set storedPermits to some number but I can't seem to do that. Also, the warming up only allows 2x or 3x the request rate or something so they can't just do 3 requests at the same time right out of the gate.

Is there a way too allow for a burst right away on creation of the RateLimiter?

thanks, Dean

Dean Hiller
  • 19,235
  • 25
  • 129
  • 212
  • The documented behavior is that, if you create a RateLimiter object with a given number of requests-per-second, the "burstiness" is equal to one second's worth of requests. That burst should be available immediately. Is that not the behavior you're seeing? And if not, how are you measuring it? – David Jun 08 '15 at 22:13
  • The burst is NOT available immediately. We solved this by pre-creating N limiters on startup for X customers. Before, customer would hit the rate limit immediately as no bursting is available until time goes by after creation. – Dean Hiller Jun 09 '15 at 19:16

2 Answers2

1

Yes, the RateLimiter is package private so you can extend it yourself.

Create a class in the same package in your code to access the underlying storedPermits. See an implementation below and adjust based on which Guava version you are using as the internal implementation changed.

package com.google.common.util.concurrent;

import com.google.common.util.concurrent.RateLimiter.SleepingStopwatch;

public class RateLimiters {

    public static RateLimiter createCharged(double permitsPerSecond, double maxBurstSeconds) {
        SmoothRateLimiter.SmoothBursty rateLimiter = new SmoothRateLimiter.SmoothBursty(SleepingStopwatch.createFromSystemTimer(), maxBurstSeconds);
        rateLimiter.setRate(permitsPerSecond);
        rateLimiter.storedPermits = maxBurstSeconds * permitsPerSecond;
        return rateLimiter;
    }
}
Anon
  • 11
  • 1
0

Our work around was to pre-create RateLimiters since there is no way to initialize StoredPermits. We grab one (and add a new one) hoping the new one has enough time to build up permits for the next customer as long as our queue size is large enough for bursts of customers coming in.

Dean Hiller
  • 19,235
  • 25
  • 129
  • 212