Skip to content

Commit

Permalink
Set default connection/read timeouts for MavenPomDownloader (#4564)
Browse files Browse the repository at this point in the history
* Set default connection/read timeouts for `MavenPomDownloader`

Connection: 10s
Read: 30s

* update method name
  • Loading branch information
pstreef authored Oct 10, 2024
1 parent ed13f4a commit fb0c122
Showing 1 changed file with 12 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,9 @@ public MavenMetadata downloadMetadata(GroupArtifactVersion gav, @Nullable Resolv
try {
String scheme = URI.create(repo.getUri()).getScheme();
String baseUri = repo.getUri() + (repo.getUri().endsWith("/") ? "" : "/") +
requireNonNull(gav.getGroupId()).replace('.', '/') + '/' +
gav.getArtifactId() + '/' +
(gav.getVersion() == null ? "" : gav.getVersion() + '/');
requireNonNull(gav.getGroupId()).replace('.', '/') + '/' +
gav.getArtifactId() + '/' +
(gav.getVersion() == null ? "" : gav.getVersion() + '/');

if ("file".equals(scheme)) {
// A maven repository can be expressed as a URI with a file scheme
Expand Down Expand Up @@ -777,25 +777,21 @@ MavenRepository normalizeRepository(MavenRepository repository) throws Throwable
}

HttpSender.Request.Builder request = httpSender.options(httpsUri);
if (repository.getTimeout() != null) {
request = request.withConnectTimeout(repository.getTimeout())
.withReadTimeout(repository.getTimeout());
}

ReachabilityResult reachability = reachable(applyAuthenticationToRequest(repository, request));
ReachabilityResult reachability = reachable(applyAuthenticationAndTimeoutToRequest(repository, request));
if (reachability.isSuccess()) {
return repository.withUri(httpsUri);
}
reachability = reachable(applyAuthenticationToRequest(repository, request.withMethod(HttpSender.Method.HEAD).url(httpsUri)));
reachability = reachable(applyAuthenticationAndTimeoutToRequest(repository, request.withMethod(HttpSender.Method.HEAD).url(httpsUri)));
if (reachability.isReachable()) {
return repository.withUri(httpsUri);
}
if (!originalUrl.equals(httpsUri)) {
reachability = reachable(applyAuthenticationToRequest(repository, request.withMethod(HttpSender.Method.OPTIONS).url(originalUrl)));
reachability = reachable(applyAuthenticationAndTimeoutToRequest(repository, request.withMethod(HttpSender.Method.OPTIONS).url(originalUrl)));
if (reachability.isSuccess()) {
return repository.withUri(originalUrl);
}
reachability = reachable(applyAuthenticationToRequest(repository, request.withMethod(HttpSender.Method.HEAD).url(originalUrl)));
reachability = reachable(applyAuthenticationAndTimeoutToRequest(repository, request.withMethod(HttpSender.Method.HEAD).url(originalUrl)));
if (reachability.isReachable()) {
return repository.withUri(originalUrl);
}
Expand Down Expand Up @@ -851,10 +847,8 @@ private ReachabilityResult reachable(HttpSender.Request.Builder request) {
*/
private byte[] requestAsAuthenticatedOrAnonymous(MavenRepository repo, String uriString) throws HttpSenderResponseException, IOException {
try {
HttpSender.Request.Builder request = httpSender.get(uriString)
.withConnectTimeout(repo.getTimeout())
.withReadTimeout(repo.getTimeout());
return sendRequest(applyAuthenticationToRequest(repo, request).build());
HttpSender.Request.Builder request = httpSender.get(uriString);
return sendRequest(applyAuthenticationAndTimeoutToRequest(repo, request).build());
} catch (HttpSenderResponseException e) {
if (hasCredentials(repo) && e.isClientSideException()) {
return retryRequestAnonymously(uriString, e);
Expand Down Expand Up @@ -886,8 +880,10 @@ private MavenRepository applyAuthenticationToRepository(MavenRepository reposito
/**
* Returns a request builder with Authorization header set if the provided repository specifies credentials
*/
private HttpSender.Request.Builder applyAuthenticationToRequest(MavenRepository repository, HttpSender.Request.Builder request) {
private HttpSender.Request.Builder applyAuthenticationAndTimeoutToRequest(MavenRepository repository, HttpSender.Request.Builder request) {
if (mavenSettings != null && mavenSettings.getServers() != null) {
request.withConnectTimeout(repository.getTimeout() == null ? Duration.ofSeconds(10) : repository.getTimeout());
request.withReadTimeout(repository.getTimeout() == null ? Duration.ofSeconds(30) : repository.getTimeout());
for (MavenSettings.Server server : mavenSettings.getServers().getServers()) {
if (server.getId().equals(repository.getId()) && server.getConfiguration() != null) {
MavenSettings.ServerConfiguration configuration = server.getConfiguration();
Expand Down

0 comments on commit fb0c122

Please sign in to comment.