Skip to content

Commit 55bc9ff

Browse files
committed
Add maxRetries Option
1 parent 3e7f863 commit 55bc9ff

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/node_modules

index.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class ProxyChooser {
2222
* @property {number} [maxTimeout=1000] - Max timeout for a request
2323
* @property {string} [pingUrl="http://myexternalip.com/raw"] - URL to use to check proxy
2424
* @property {boolean} [forceRetry=false] - Whether function getProxy should continue searching even on error
25+
* @property {number} [maxRetries=100] - Max retries for a request
2526
*/
2627

2728
/**
@@ -47,7 +48,8 @@ class ProxyChooser {
4748
maxTimeout: 1000,
4849
verboseIdentifier: "[proxyChooser]",
4950
pingUrl: "http://myexternalip.com/raw",
50-
forceRetry: false
51+
forceRetry: false,
52+
maxRetries: 100
5153
};
5254
this.#cache = [];
5355
}
@@ -293,7 +295,7 @@ class ProxyChooser {
293295
* Gets the next working proxy from the proxy list.
294296
* @returns {Promise<string|null>} Next working proxy or null if no proxy is available
295297
*/
296-
async getProxy() {
298+
async getProxy(retries = 0) {
297299
this.validateOptions();
298300
try {
299301
const proxyList = this.#proxyList.filter(cproxy => !this.#cache.includes(cproxy));
@@ -319,15 +321,19 @@ class ProxyChooser {
319321
if (workingProxy) {
320322
return proxy;
321323
} else {
322-
return this.getProxy();
324+
if (retries < options.maxRetries) {
325+
return this.getProxy(retries + 1);
326+
} else {
327+
throw new Error(`Max retries (${options.maxRetries}) exceeded`);
328+
}
323329
}
324330
} catch (error) {
325331
if (this.#options.forceRetry) {
326332
if (this.#options.verbose) {
327333
console.log(`${this.#options.verboseIdentifier} ${error} | Retrying... (forceRetry enabled)`)
328334
}
329335
this.resetCache();
330-
return this.getProxy();
336+
return this.getProxy(retries + 1);
331337
} else {
332338
throw new Error(error);
333339
}

package-lock.json

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)