Skip to content

Commit

Permalink
[HUDI-4055]refactor ratelimiter to avoid stack overflow (apache#5530)
Browse files Browse the repository at this point in the history
  • Loading branch information
guanziyue authored and cdmikechen committed May 13, 2022
1 parent 82c22b3 commit fb65e19
Showing 1 changed file with 10 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,22 @@ private RateLimiter(int permits, TimeUnit timePeriod) {
}

public boolean tryAcquire(int numPermits) {
if (numPermits > maxPermits) {
acquire(maxPermits);
return tryAcquire(numPermits - maxPermits);
} else {
return acquire(numPermits);
int remainingPermits = numPermits;
while (remainingPermits > 0) {
if (remainingPermits > maxPermits) {
acquire(maxPermits);
remainingPermits -= maxPermits;
} else {
return acquire(remainingPermits);
}
}
return true;
}

public boolean acquire(int numOps) {
try {
if (!semaphore.tryAcquire(numOps)) {
while (!semaphore.tryAcquire(numOps)) {
Thread.sleep(WAIT_BEFORE_NEXT_ACQUIRE_PERMIT_IN_MS);
return acquire(numOps);
}
LOG.debug(String.format("acquire permits: %s, maxPremits: %s", numOps, maxPermits));
} catch (InterruptedException e) {
Expand Down

0 comments on commit fb65e19

Please sign in to comment.