1

I have a Java module, that is run with AWS Lambda. This means that multiple instances of the module can be run at the same time.

The module is used to access a certain API and retrieve data from it. The problem is that the API uses a leaky bucket algorithm, which limits the API calls to 40 and an API call is made available every 0.5 seconds. Because of this I get a Request limit exceeded exception.

In order to fix this I decided to implement a distributed lock and to use redisson with the AWS ElastiCache (distributed Redis cluster). After checking the redisson's documentation, I concluded that I should use a PermitExpirableSemaphore which can create a lock with a lease (in my case 500 ms).

The problem is, that I can't find a way to limit the available permits to 40.

Do you know of a way to do this?

Here is an example of my code:

    Config config = new Config();
    config.useElasticacheServers()
                .setScanInterval(2000) // cluster state scan interval in milliseconds
                .addNodeAddress("my.cache.amazonaws.com:6379");

    RedissonClient redissonClient = Redisson.create(config);
    RPermitExpirableSemaphore semaphore = redissonClient.getPermitExpirableSemaphore("mySemaphore");

    String permitId = semaphore.acquire(500, TimeUnit.MILLISECONDS);

    // Make the API call

    semaphore.release(permitId);
alexwlchan
  • 5,699
  • 7
  • 38
  • 49
Ivan Stoyanov
  • 5,412
  • 12
  • 55
  • 71

1 Answers1

0

So I found the solution.

The there is an addPermits method in Redisson, that can be used to add the number of permits. And it should be used only one time for a semaphore.

        // If there are no permits set this will throw a NullPointerException.
        // This means that the permits should be added. If the addPermits is executed more than once,
        // each consecutive call will add the permits to the existing ones. eg: 35, 70, etc.
        try
        {
            int permits = _semaphore.availablePermits();

        }
        catch (NullPointerException ex)
        {
            _semaphore.addPermits(35);
        }
Ivan Stoyanov
  • 5,412
  • 12
  • 55
  • 71