Skip to content

Commit 14757ec

Browse files
committed
add configurable delay
1 parent 63e406e commit 14757ec

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

src/nl/melp/linkchecker/LinkChecker.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public class LinkChecker {
4444
private final Set<Future> running;
4545
private final BiPredicate<URI, URI> shouldFollowLinks;
4646
private final BiPredicate<URI, HttpResponse<String>> shouldExtractLinks;
47+
private final int msDelay;
4748
private long startTimeMs;
4849

4950
public LinkChecker(
@@ -53,7 +54,8 @@ public LinkChecker(
5354
Map<String, Set<URI>> invalidUrls,
5455
BiPredicate<URI, URI> shouldFollowLinks,
5556
BiPredicate<URI, HttpResponse<String>> shouldExtractLinks,
56-
int numThreads
57+
int numThreads,
58+
int msDelay
5759
) {
5860
this.urls = urlsToCheck;
5961
this.shouldFollowLinks = shouldFollowLinks;
@@ -64,6 +66,7 @@ public LinkChecker(
6466
this.executor = Executors.newFixedThreadPool(numThreads);
6567
this.running = new HashSet<>();
6668
this.clients = new LinkedBlockingDeque<>(numThreads);
69+
this.msDelay = msDelay;
6770

6871
for (int i = 0; i < numThreads; i++) {
6972
clients.offer(HttpClient.newHttpClient());
@@ -138,9 +141,12 @@ public Map<URI, Integer> run() throws InterruptedException {
138141
if (statuses.get(url) >= 0) {
139142
return; // URL was resolved by another thread.
140143
}
144+
// give the system some rest (if configured)
145+
if (msDelay > 0) {
146+
Thread.sleep(msDelay);
147+
}
141148

142149
logger.trace("OPENING " + url);
143-
144150
HttpRequest request = HttpRequest.newBuilder()
145151
.uri(url)
146152
.timeout(Duration.ofSeconds(timeout))
@@ -443,7 +449,8 @@ public void checkServerTrusted(
443449
return false;
444450
},
445451
(context, response) -> !flags.contains("no-follow") && localHosts.contains(context.getHost()),
446-
opts.containsKey("threads") ? Integer.valueOf(opts.get("threads").stream().findFirst().orElse("40")) : 40
452+
opts.containsKey("threads") ? Integer.valueOf(opts.get("threads").stream().findFirst().orElse("40")) : 40,
453+
opts.containsKey("delay-ms") ? Integer.valueOf(opts.get("delay-ms").stream().findFirst().orElse("20")) : 20
447454
);
448455

449456
if (flags.contains("resume") || flags.contains("reset")) {

0 commit comments

Comments
 (0)