Skip to content

Commit

Permalink
feat(rule): add maxRetryTime option (#136)
Browse files Browse the repository at this point in the history
* feat(rule): add `maxRetryTime` option

* docs: add `maxRetryTime`

* fix: change default value of maxRetryTime 0 -> 10

* docs: change default value of maxRetryTime

* fix: prefer to use After-Retry value
  • Loading branch information
chick-p committed Nov 8, 2021
1 parent 260db6a commit 9f9c636
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
7 changes: 6 additions & 1 deletion ReadMe.md
Expand Up @@ -66,7 +66,8 @@ The default options are:
"preferGET": [],
"ignoreRedirects": false,
"retry": 3,
"userAgent": "textlint-rule-no-dead-link/1.0"
"userAgent": "textlint-rule-no-dead-link/1.0",
"maxRetryTime": 10
}
}
}
Expand Down Expand Up @@ -151,6 +152,10 @@ The default max retry count is `3`.

Customize `User-Agent` http header.

### maxRetryTime

The max of allow waiting time [second] for retry, if response header has `Retry-After`.

## Tests

```
Expand Down
15 changes: 11 additions & 4 deletions src/no-dead-link.js
Expand Up @@ -20,7 +20,8 @@ const DEFAULT_OPTIONS = {
interval: 500, // The length of time in milliseconds before the interval count resets. Must be finite. [Experimental]
intervalCap: 8, // The max number of runs in the given interval of time. [Experimental]
keepAlive: false, // {boolean} if it is true, use keepAlive for checking request [Experimental]
userAgent: 'textlint-rule-no-dead-link/1.0' // {String} a UserAgent
userAgent: 'textlint-rule-no-dead-link/1.0', // {String} a UserAgent,
maxRetryTime: 10, // (number) The max of waiting seconds for retry, if response returns `After-Retry` header.
};

// Adopted from http://stackoverflow.com/a/3809435/951517
Expand Down Expand Up @@ -186,11 +187,17 @@ const createCheckAliveURL = (ruleOptions) => {

// try to fetch again if not reach max retry count
if (currentRetryCount < maxRetryCount) {
// exponential retry
// 0ms -> 100ms -> 200ms -> 400ms -> 800ms ...
await waitTimeMs((currentRetryCount ** 2) * 100);
const retrySeconds = res.headers.get('Retry-After');
// If the response has `Retry-After` header, prefer it
// else exponential retry: 0ms -> 100ms -> 200ms -> 400ms -> 800ms ...
const retryWaitTimeMs = retrySeconds !== null ? retrySeconds * 1000 : currentRetryCount ** 2 * 100;
const maxRetryTimeMs = ruleOptions.maxRetryTime * 1000;
if (retryWaitTimeMs <= maxRetryTimeMs) {
await waitTimeMs(retryWaitTimeMs);
}
return isAliveURI(uri, 'GET', maxRetryCount, currentRetryCount + 1);
}

return {
ok: res.ok,
message: `${res.status} ${res.statusText}`,
Expand Down

0 comments on commit 9f9c636

Please sign in to comment.