title: How to set maximum number of permits for Redisson distributed lock
body: <p>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.</p>
  
  <p>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 <code>Request limit exceeded</code> exception. </p>
  
  <p>In order to fix this I decided to implement a distributed lock and to use <a href="https://github.com/redisson/redisson" rel="nofollow">redisson</a> with the AWS ElastiCache (distributed Redis cluster). After checking the redisson's documentation, I concluded that I should use a <code>PermitExpirableSemaphore</code> which can create a lock with a lease (in my case 500 ms). </p>
  
  <p>The problem is, that I can't find a way to limit the available permits to 40.</p>
  
  <p>Do you know of a way to do this?</p>
  
  <p>Here is an example of my code:</p>
  
  <pre><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);
  </code></pre>