Skip to content

Commit

Permalink
Avoid long waiting times when GitHub API calls exceed the rate limit (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
baixinsui authored Oct 11, 2024
1 parent 72cd4ae commit 1b669c0
Showing 1 changed file with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@

package org.eclipse.xpanse.terraform.boot.terraform.tool;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Pattern;
import javax.annotation.Nonnull;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.xpanse.terraform.boot.models.exceptions.InvalidTerraformToolException;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GHTag;
import org.kohsuke.github.GitHub;
import org.kohsuke.github.GitHubBuilder;
import org.kohsuke.github.GitHubRateLimitHandler;
import org.kohsuke.github.PagedIterable;
import org.kohsuke.github.connector.GitHubConnectorResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Retryable;
Expand Down Expand Up @@ -47,7 +51,10 @@ public class TerraformVersionsFetcher {
backoff = @Backoff(delayExpression = "${spring.retry.delay-millions}"))
public Set<String> fetchAvailableVersionsFromTerraformWebsite() throws Exception {
Set<String> allVersions = new HashSet<>();
GitHub gitHub = new GitHubBuilder().withEndpoint(terraformGithubApiEndpoint).build();
GitHub gitHub = new GitHubBuilder()
.withEndpoint(terraformGithubApiEndpoint)
.withRateLimitHandler(getGithubRateLimitHandler())
.build();
GHRepository repository = gitHub.getRepository(terraformGithubRepository);
PagedIterable<GHTag> tags = repository.listTags();
tags.forEach(tag -> {
Expand Down Expand Up @@ -78,4 +85,18 @@ public Set<String> getDefaultVersionsFromConfig() {
return defaultVersions;
}

private GitHubRateLimitHandler getGithubRateLimitHandler() {
return new GitHubRateLimitHandler() {
@Override
public void onError(@Nonnull GitHubConnectorResponse response) throws IOException {
String limit = response.header("X-RateLimit-Limit");
String remaining = response.header("X-RateLimit-Remaining");
String reset = response.header("X-RateLimit-Reset");
String errorMsg = String.format("GitHub API rate limit exceeded. "
+ "Rate limit: %s, remaining: %s, reset time: %s", limit, remaining, reset);
throw new IOException(errorMsg);
}
};
}

}

0 comments on commit 1b669c0

Please sign in to comment.