madrob commented on a change in pull request #1686:
URL: https://github.com/apache/lucene-solr/pull/1686#discussion_r467104137
##########
File path: solr/core/src/java/org/apache/solr/servlet/RequestRateLimiter.java
##########
@@ -58,18 +58,18 @@ public RequestRateLimiter(RateLimiterConfig
rateLimiterConfig) {
public Pair<Boolean, AcquiredSlotMetadata> handleRequest() throws
InterruptedException {
if (!rateLimiterConfig.isEnabled) {
- return new Pair<Boolean, AcquiredSlotMetadata>(true, null);
+ return new Pair<Boolean, AcquiredSlotMetadata>(true, new
AcquiredSlotMetadata(null, null));
}
if
(guaranteedSlotsPool.tryAcquire(rateLimiterConfig.waitForSlotAcquisition,
TimeUnit.MILLISECONDS)) {
- return new Pair<Boolean, AcquiredSlotMetadata>(true, new
AcquiredSlotMetadata(this, false));
+ return new Pair<Boolean, AcquiredSlotMetadata>(true, new
AcquiredSlotMetadata(this, guaranteedSlotsPool));
}
if
(borrowableSlotsPool.tryAcquire(rateLimiterConfig.waitForSlotAcquisition,
TimeUnit.MILLISECONDS)) {
- return new Pair<Boolean, AcquiredSlotMetadata>(true, new
AcquiredSlotMetadata(this, true));
+ return new Pair<Boolean, AcquiredSlotMetadata>(true, new
AcquiredSlotMetadata(this, borrowableSlotsPool));
}
- return new Pair<Boolean, AcquiredSlotMetadata>(false, null);
+ return new Pair<Boolean, AcquiredSlotMetadata>(false, new
AcquiredSlotMetadata(null, null));
}
Review comment:
I would write this as something like:
```
class SemaphoreWrapper() {
Semaphore wrapped;
release() {
if (wrapped != null) wrapped.release();
}
}
public SempahoreWrapper handleRequest() throws InterruptedException {
if (!rateLimiterConfig.isEnabled) {
return nopPool; // = new SemaphoreWrapper(null);
}
if
(guaranteedSlotsPool.tryAcquire(rateLimiterConfig.waitForSlotAcquisition,
TimeUnit.MILLISECONDS)) {
return new SemaphoreWrapper(guaranteedSlotsPool);
}
if
(borrowableSlotsPool.tryAcquire(rateLimiterConfig.waitForSlotAcquisition,
TimeUnit.MILLISECONDS)) {
return new SemaphoreWrapper(borrowableSlotsPool);
}
return null;
}
```
And probably have the limiter own the wrappers and return the right thing
each time instead of creating a new wrapper.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]