Skip to content

Commit 37650f7

Browse files
committed
feat: add configurable polling interval for CloudflareSolver
- Introduced a new pollingInterval parameter to allow customization of the polling delay in CloudflareSolver. - Updated LinkSocksManager and MaskTunnelManager to use a configurable startup wait time.
1 parent 32ff349 commit 37650f7

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

src/main/java/site/zetx/cloudflyer/CloudflareSolver.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public class CloudflareSolver implements AutoCloseable {
4040
private final boolean solve;
4141
private final boolean onChallenge;
4242
private final boolean usePolling;
43+
private final int pollingInterval;
4344
private final int timeout;
4445
private final String proxy;
4546
private final boolean useLinkSocks;
@@ -71,6 +72,7 @@ private CloudflareSolver(Builder builder) {
7172
this.solve = builder.solve;
7273
this.onChallenge = builder.onChallenge;
7374
this.usePolling = builder.usePolling;
75+
this.pollingInterval = builder.pollingInterval;
7476
this.timeout = builder.timeout;
7577
this.proxy = builder.proxy;
7678
this.useLinkSocks = builder.useLinkSocks;
@@ -520,7 +522,7 @@ private JsonObject waitForResult(String taskId, int timeout) throws CFSolverExce
520522
try (okhttp3.Response response = pollClient.newCall(request).execute()) {
521523
if (response.code() != 200) {
522524
if (usePolling) {
523-
Thread.sleep(2000);
525+
Thread.sleep(pollingInterval);
524526
}
525527
continue;
526528
}
@@ -531,7 +533,7 @@ private JsonObject waitForResult(String taskId, int timeout) throws CFSolverExce
531533

532534
if ("processing".equals(status)) {
533535
if (usePolling) {
534-
Thread.sleep(2000);
536+
Thread.sleep(pollingInterval);
535537
}
536538
continue;
537539
}
@@ -567,7 +569,7 @@ private JsonObject waitForResult(String taskId, int timeout) throws CFSolverExce
567569
} catch (IOException e) {
568570
if (usePolling) {
569571
try {
570-
Thread.sleep(2000);
572+
Thread.sleep(pollingInterval);
571573
} catch (InterruptedException ie) {
572574
Thread.currentThread().interrupt();
573575
}
@@ -930,6 +932,7 @@ public static class Builder {
930932
private boolean solve = true;
931933
private boolean onChallenge = true;
932934
private boolean usePolling = false;
935+
private int pollingInterval = 2000;
933936
private int timeout = 30000;
934937
private String proxy;
935938
private okhttp3.Authenticator proxyAuth;
@@ -986,6 +989,17 @@ public Builder usePolling(boolean usePolling) {
986989
return this;
987990
}
988991

992+
/**
993+
* Set the interval between polling attempts when usePolling is true.
994+
*
995+
* @param pollingInterval interval in milliseconds (default: 2000)
996+
* @return this builder
997+
*/
998+
public Builder pollingInterval(int pollingInterval) {
999+
this.pollingInterval = pollingInterval;
1000+
return this;
1001+
}
1002+
9891003
/**
9901004
* Set the request timeout in milliseconds.
9911005
*

src/main/java/site/zetx/cloudflyer/LinkSocksManager.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class LinkSocksManager {
2525
private final OkHttpClient httpClient;
2626
private final String upstreamProxy;
2727
private final ToolDownloader toolDownloader;
28+
private final int startupWaitMs;
2829

2930
private Process linksocksProcess;
3031
private LinkSocksConfig config;
@@ -52,10 +53,15 @@ String getWsUrl() {
5253
}
5354

5455
LinkSocksManager(String apiBase, String apiKey, OkHttpClient httpClient, String upstreamProxy) {
56+
this(apiBase, apiKey, httpClient, upstreamProxy, 2000);
57+
}
58+
59+
LinkSocksManager(String apiBase, String apiKey, OkHttpClient httpClient, String upstreamProxy, int startupWaitMs) {
5560
this.apiBase = apiBase;
5661
this.apiKey = apiKey;
5762
this.httpClient = httpClient;
5863
this.upstreamProxy = upstreamProxy;
64+
this.startupWaitMs = startupWaitMs;
5965
this.toolDownloader = new ToolDownloader(httpClient);
6066
}
6167

@@ -120,7 +126,7 @@ synchronized void connect() throws IOException {
120126

121127
// Wait for connection
122128
try {
123-
Thread.sleep(2000);
129+
Thread.sleep(startupWaitMs);
124130
} catch (InterruptedException e) {
125131
Thread.currentThread().interrupt();
126132
}

src/main/java/site/zetx/cloudflyer/MaskTunnelManager.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,25 @@ public class MaskTunnelManager implements Closeable {
2424
private final int port;
2525
private final String upstreamProxy;
2626
private final ToolDownloader toolDownloader;
27+
private final int startupWaitMs;
2728
private Process process;
2829
private Thread outputThread;
2930
private Thread errorThread;
3031

3132
public MaskTunnelManager(String addr, int port, String upstreamProxy, OkHttpClient httpClient) {
33+
this(addr, port, upstreamProxy, httpClient, 2000);
34+
}
35+
36+
public MaskTunnelManager(String addr, int port, String upstreamProxy, OkHttpClient httpClient, int startupWaitMs) {
3237
this.addr = addr;
3338
this.port = port;
3439
this.upstreamProxy = upstreamProxy;
3540
this.toolDownloader = new ToolDownloader(httpClient);
41+
this.startupWaitMs = startupWaitMs;
3642
}
3743

3844
public MaskTunnelManager(int port, String upstreamProxy, OkHttpClient httpClient) {
39-
this("127.0.0.1", port, upstreamProxy, httpClient);
45+
this("127.0.0.1", port, upstreamProxy, httpClient, 2000);
4046
}
4147

4248
/**
@@ -70,7 +76,7 @@ public void start() throws IOException {
7076

7177
// Wait for startup
7278
try {
73-
Thread.sleep(2000);
79+
Thread.sleep(startupWaitMs);
7480
} catch (InterruptedException e) {
7581
Thread.currentThread().interrupt();
7682
}

0 commit comments

Comments
 (0)