-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Retry search requests upon hitting secondary rate limits
- Loading branch information
Showing
2 changed files
with
84 additions
and
1 deletion.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
src/main/java/io/quarkus/github/lottery/util/RetryingIterator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package io.quarkus.github.lottery.util; | ||
|
||
import java.io.InterruptedIOException; | ||
import java.io.UncheckedIOException; | ||
import java.util.Iterator; | ||
import java.util.function.Supplier; | ||
|
||
import org.kohsuke.github.GHException; | ||
import org.kohsuke.github.PagedIterator; | ||
|
||
import io.quarkus.logging.Log; | ||
|
||
// Workaround for https://github.com/hub4j/github-api/issues/2009 | ||
class RetryingIterator<T> implements Iterator<T> { | ||
private static final int MAX_RETRY = 3; | ||
|
||
// Wait for unambiguously over one minute per GitHub guidance | ||
private static final long DEFAULT_WAIT_MILLIS = 61 * 1000; | ||
|
||
private final PagedIterator<T> delegate; | ||
|
||
public RetryingIterator(PagedIterator<T> delegate) { | ||
this.delegate = delegate; | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return doWithRetry(delegate::hasNext); | ||
} | ||
|
||
@Override | ||
public T next() { | ||
return doWithRetry(delegate::next); | ||
} | ||
|
||
private <T> T doWithRetry(Supplier<T> action) { | ||
RuntimeException rateLimitException = null; | ||
for (int i = 0; i < MAX_RETRY; i++) { | ||
if (rateLimitException != null) { | ||
waitBeforeRetry(rateLimitException); | ||
} | ||
try { | ||
return action.get(); | ||
} catch (RuntimeException e) { | ||
if (isSecondaryRateLimitReached(e)) { | ||
if (rateLimitException == null) { | ||
rateLimitException = e; | ||
} else { | ||
rateLimitException.addSuppressed(e); | ||
} | ||
} else { | ||
throw e; | ||
} | ||
} | ||
} | ||
throw rateLimitException; | ||
} | ||
|
||
private static boolean isSecondaryRateLimitReached(RuntimeException e) { | ||
return e instanceof GHException | ||
&& e.getCause() != null && e.getCause().getMessage().contains("secondary rate limit"); | ||
} | ||
|
||
private void waitBeforeRetry(RuntimeException e) { | ||
Log.infof("GitHub API reached a secondary rate limit; waiting %s ms before retrying...", DEFAULT_WAIT_MILLIS); | ||
try { | ||
Thread.sleep(DEFAULT_WAIT_MILLIS); | ||
} catch (InterruptedException ex) { | ||
throw new UncheckedIOException((InterruptedIOException) new InterruptedIOException().initCause(e)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters