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);