@@ -21,18 +21,10 @@ public final class SemaphoreRequestQueueManager implements RequestQueueManager,
2121 private final long queueResetTimeInMillis ;
2222
2323 public SemaphoreRequestQueueManager (int size , Duration resetIn ) {
24- this (size , resetIn , resetIn );
25- }
26-
27- public SemaphoreRequestQueueManager (int size , Duration resetIn , Duration delayIn ) {
28- this (size , resetIn , delayIn , size );
29- }
30-
31- public SemaphoreRequestQueueManager (int size , Duration queueResetTimeIn , Duration delayIn , int initialSize ) {
32- this .semaphore = new Semaphore (initialSize );
33- this .queueResetTimeInMillis = queueResetTimeIn .toMillis ();
34- this .executorService .scheduleAtFixedRate (releaseLocks (size + 1 ),
35- delayIn .toMillis (), queueResetTimeInMillis , TimeUnit .MILLISECONDS );
24+ this .semaphore = new Semaphore (0 );
25+ this .queueResetTimeInMillis = resetIn .toMillis ();
26+ this .executorService .scheduleAtFixedRate (releaseLocks (size ),
27+ resetIn .toMillis (), queueResetTimeInMillis , TimeUnit .MILLISECONDS );
3628 }
3729
3830 @ SuppressWarnings ("java:S899" )
@@ -46,7 +38,13 @@ public void takeTurn() {
4638 }
4739
4840 private Runnable releaseLocks (int toRelease ) {
49- return () -> semaphore .release (toRelease );
41+ return () -> {
42+ int availablePermits = semaphore .availablePermits ();
43+ int neededPermits = toRelease - availablePermits ;
44+ if (neededPermits > 0 ) {
45+ semaphore .release (neededPermits );
46+ }
47+ };
5048 }
5149
5250 @ Override
0 commit comments