Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid long waiting times when GitHub API calls exceed the rate limit #148

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
};
}

}