Make request for feed more resilient

This commit tackles the following issue:
When the release feed takes a long time to read (however), the former
solution would propagete requests to the SCM-Manager to multiple
requests of the specified feed url. This can, in the worst case, cause
a drain of resources like request threads.

We fix this with two actions:
1. We wrap the request itself in an executor with a timeout,
2. We only trigger one request at a time, so that we will not flood
   the feed server with requests.
This commit is contained in:
René Pfeuffer
2020-09-24 10:03:18 +02:00
parent 23581e780d
commit 6a80cce87a
2 changed files with 102 additions and 9 deletions

View File

@@ -31,25 +31,71 @@ import sonia.scm.net.ahc.AdvancedHttpClient;
import sonia.scm.version.Version;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.io.IOException;
import java.util.Comparator;
import java.util.Optional;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
@Singleton
public class ReleaseFeedParser {
public static final int DEFAULT_TIMEOUT_IN_MILLIS = 1000;
private static final Logger LOG = LoggerFactory.getLogger(ReleaseFeedParser.class);
private final AdvancedHttpClient client;
private final ExecutorService executorService;
private final long timeoutInMillis;
private Future<Optional<UpdateInfo>> updateInfoFuture;
@Inject
public ReleaseFeedParser(AdvancedHttpClient client) {
this(client, DEFAULT_TIMEOUT_IN_MILLIS);
}
public ReleaseFeedParser(AdvancedHttpClient client, long timeoutInMillis) {
this.client = client;
this.timeoutInMillis = timeoutInMillis;
this.executorService = Executors.newSingleThreadExecutor();
}
Optional<UpdateInfo> findLatestRelease(String url) {
LOG.info("Search for newer versions of SCM-Manager");
Optional<ReleaseFeedDto.Release> latestRelease = parseLatestReleaseFromRssFeed(url);
return latestRelease.map(release -> new UpdateInfo(release.getTitle(), release.getLink()));
Future<Optional<UpdateInfo>> currentUpdateInfoFuture;
boolean updateInfoFutureCreated = false;
try {
synchronized (this) {
currentUpdateInfoFuture = this.updateInfoFuture;
if (currentUpdateInfoFuture == null) {
currentUpdateInfoFuture = submitRequest(url);
this.updateInfoFuture = currentUpdateInfoFuture;
updateInfoFutureCreated = true;
}
}
try {
return currentUpdateInfoFuture.get(timeoutInMillis, TimeUnit.MILLISECONDS);
} catch (Exception e) {
LOG.error("Could not read release feed", e);
return Optional.empty();
}
} finally {
if (updateInfoFutureCreated) {
synchronized (this) {
this.updateInfoFuture = null;
}
}
}
}
private Future<Optional<UpdateInfo>> submitRequest(String url) {
return executorService.submit(() -> {
LOG.info("Search for newer versions of SCM-Manager");
Optional<ReleaseFeedDto.Release> latestRelease = parseLatestReleaseFromRssFeed(url);
return latestRelease.map(release -> new UpdateInfo(release.getTitle(), release.getLink()));
});
}
private Optional<ReleaseFeedDto.Release> parseLatestReleaseFromRssFeed(String url) {