Concurrency & Returnable Limits Try it

Sometimes you don't care exactly what the rate-limit is, but you want to ensure that something is only happening N times at once.

You can achieve this with in much the same way as a rate limit, but with the additional tweak of declaring that your limit is returnable.

When you mark your LimitDefinition as returnable, the burst value is used as the total number of concurrent tokens and the limit and limitPolicy are used to define the token expiration.

limiter = "ACCT|APIKEY")

# only need to do this on startup
# allow 2 tokens to be taken out at once
# allow tokens to be returned
# if processes don't return their token in 30 sec, they will become available again
limiter.upsert_concurrency_limit("concurrent", 2, 30)

5.times do |i| do
      limit_result = limiter.acquire_or_wait(key: "concurrent", acquire_amount: 1, wait_in_secs: 10)
    rescue RateLimIt::WaitExceeded => e
      puts "never ran #{i}"
    .setLimit(2)  // 2 per minute = 30 sec = timeout on token leases
    .setBurst(1) // one concurrent

Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder()
        .withWaitStrategy(WaitStrategies.exponentialWait(100, 1, TimeUnit.MINUTES))

Callable<Boolean> callable = () -> {
  final RateLimitProtos.LimitResponse limitResponse = apiClient.limitCheck(RateLimitProtos.LimitRequest.newBuilder()
    System.out.println("I was able to do the thing!");
    apiClient.limitReturn(limitResponse); //give the token back
    return true;
    return false;

Read about Batches & Acquiring More Than One Token