Skip to content

Commit

Permalink
tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
hiteshk25 committed Oct 24, 2024
1 parent 72541cb commit 8d6eb7c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class RateLimiterConfig {
public final boolean isSlotBorrowingEnabled;
public final int guaranteedSlotsThreshold;
public final Boolean priorityBasedEnabled;

/**
* We store the config definition in order to determine whether anything has changed that would
* call for re-initialization.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
* priorities {@link SolrRequest.RequestPriorities} FOREGROUND and {@link
* SolrRequest.RequestPriorities} BACKGROUND Requests. Client can pass the {@link
* org.apache.solr.common.params.CommonParams} SOLR_REQUEST_TYPE_PARAM request header to indicate
* the foreground and background request. Foreground requests has high priority than background requests
* the foreground and background request. Foreground requests has high priority than background
* requests
*/
public class PriorityBasedRateLimiter extends RequestRateLimiter {
private final AtomicInteger priorityOneRequests = new AtomicInteger();
Expand Down Expand Up @@ -44,7 +45,7 @@ public SlotReservation handleRequest(String requestPriority) {
if (!acquire(requestPriority)) {
return null;
}
}catch (InterruptedException ie) {
} catch (InterruptedException ie) {
return null;
}
return () -> PriorityBasedRateLimiter.this.release(requestPriority);
Expand All @@ -66,7 +67,8 @@ private boolean acquire(String priority) throws InterruptedException {
}

private boolean nextInQueue() throws InterruptedException {
boolean acquired = this.numRequestsAllowed.tryAcquire(1, this.waitTimeoutInMillis, TimeUnit.MILLISECONDS);
boolean acquired =
this.numRequestsAllowed.tryAcquire(1, this.waitTimeoutInMillis, TimeUnit.MILLISECONDS);
if (!acquired) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -665,36 +665,38 @@ public void testPriorityBasedRateLimiterTimeout() throws Exception {

// PriorityBasedRateLimiter
RateLimiterConfig rateLimiterConfig =
new RateLimiterConfig(
SolrRequest.SolrRequestType.QUERY,
true,
1,
10,
1 /* allowedRequests */,
true /* isSlotBorrowing */,
true);
new RateLimiterConfig(
SolrRequest.SolrRequestType.QUERY,
true,
1,
10,
1 /* allowedRequests */,
true /* isSlotBorrowing */,
true);

PriorityBasedRateLimiter requestRateLimiter = new PriorityBasedRateLimiter(rateLimiterConfig);

rateLimitManager.registerRequestRateLimiter(
requestRateLimiter, SolrRequest.SolrRequestType.PRIORITY_BASED);
requestRateLimiter, SolrRequest.SolrRequestType.PRIORITY_BASED);

HttpServletRequest firstRequest = new DummyRequest(null, "FOREGROUND");

RequestRateLimiter.SlotReservation firstRequestAllowed = rateLimitManager.handleRequest(firstRequest);
RequestRateLimiter.SlotReservation firstRequestAllowed =
rateLimitManager.handleRequest(firstRequest);
assertNotNull(firstRequestAllowed);
assertEquals(1, requestRateLimiter.getRequestsAllowed());


HttpServletRequest secondRequest = new DummyRequest(null, "FOREGROUND");

RequestRateLimiter.SlotReservation secondRequestNotAllowed = rateLimitManager.handleRequest(secondRequest);
assertNull(secondRequestNotAllowed);
assertEquals(1, requestRateLimiter.getRequestsAllowed());
RequestRateLimiter.SlotReservation secondRequestNotAllowed =
rateLimitManager.handleRequest(secondRequest);
assertNull(secondRequestNotAllowed);
assertEquals(1, requestRateLimiter.getRequestsAllowed());

HttpServletRequest thirdRequest = new DummyRequest(null, "BACKGROUND");

RequestRateLimiter.SlotReservation thirdRequestNotAllowed = rateLimitManager.handleRequest(thirdRequest);
RequestRateLimiter.SlotReservation thirdRequestNotAllowed =
rateLimitManager.handleRequest(thirdRequest);
assertNull(thirdRequestNotAllowed);
assertEquals(1, requestRateLimiter.getRequestsAllowed());

Expand Down

0 comments on commit 8d6eb7c

Please sign in to comment.