0

AWS Lambdaで実行されるJavaモジュールがあります。つまり、モジュールの複数のインスタンスを同時に実行できます。Redisson分散ロックの最大許可数を設定する方法

モジュールは、特定のAPIにアクセスし、そこからデータを取得するために使用されます。問題は、APIが40までのAPI呼び出しを制限するリークバケットアルゴリズムを使用し、0.5秒ごとにAPI呼び出しが使用可能になることです。 このため、私はRequest limit exceededの例外を受け取ります。

これを解決するために、分散ロックを実装し、redissonをAWS ElastiCache(分散Redisクラスタ)と使用することに決めました。 redissonのドキュメントを確認した後、私はPermitExpirableSemaphoreを使用して、リースを使ってロックを作成できると判断しました(私の場合は500ミリ秒です)。

問題は、私はあなたがこれを行う方法を知っていますか40

に利用可能なパーミットを制限する方法を見つけることができないこと、ありますか?ここで

は、私のコードの例です:

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

答えて

0

だから私は解決策を見つけました。

RedissonにはaddPermitsメソッドがあり、これを使用して許可数を追加できます。また、セマフォには1回だけ使用する必要があります。

 // 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); 
     } 
関連する問題