Skip to content

Commit

Permalink
feat: add maxRetryAfterTime option
Browse files Browse the repository at this point in the history
  • Loading branch information
azu committed Oct 30, 2022
1 parent 8446573 commit dbeed6b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ The default options are:
"ignoreRedirects": false,
"retry": 3,
"userAgent": "textlint-rule-no-dead-link/1.0",
"maxRetryTime": 10
"maxRetryTime": 10,
"maxRetryAfterTime": 90
}
}
}
Expand Down Expand Up @@ -151,10 +152,18 @@ Customize `User-Agent` http header.

### maxRetryTime

The max of allow waiting time second for retry, if response header has `Retry-After`.
The max of waiting seconds for retry. It is related to `retry` option.

:memo: It does affect to `Retry-After` header. If you want to max waiting seconds for `Retry-After` header, please use `maxRetryAfterTime` option.

Default: `10`

### maxRetryAfterTime

The max of allow waiting time second for `maxRetryAfterTime` header value.

Default: `90`

## Tests

```
Expand Down
27 changes: 18 additions & 9 deletions src/no-dead-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ export type Options = {
intervalCap: number; // The max number of runs in the given interval of time. [Experimental]
keepAlive: boolean; // {boolean} if it is true, use keepAlive for checking request [Experimental]
userAgent: string; // {String} a UserAgent,
maxRetryTime: number; // (number) The max of waiting seconds for retry, if response returns `After-Retry` header.
maxRetryTime: number; // (number) The max of waiting seconds for retry. It is related to `retry` option. It does affect to `Retry-After` header.
maxRetryAfterTime: number; // (number) The max of waiting seconds for `Retry-After` header.
};
const DEFAULT_OPTIONS: Options = {
checkRelative: true, // {boolean} `false` disables the checks for relative URIs.
Expand All @@ -38,7 +39,8 @@ const DEFAULT_OPTIONS: Options = {
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,
maxRetryTime: 10 // (number) The max of waiting seconds for retry, if response returns `After-Retry` header.
maxRetryTime: 10, // (number) The max of waiting seconds for retry. It is related to `retry` option. It does affect to `Retry-After` header.
maxRetryAfterTime: 90 // (number) The max of waiting seconds for `Retry-After` header.
};

// Adopted from http://stackoverflow.com/a/3809435/951517
Expand Down Expand Up @@ -227,14 +229,21 @@ const createCheckAliveURL = (ruleOptions: Options) => {

// try to fetch again if not reach max retry count
if (currentRetryCount < maxRetryCount) {
const retrySeconds = res.headers.get("Retry-After");
const retryAfter = 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 ? Number(retrySeconds) * 1000 : currentRetryCount ** 2 * 100;
const maxRetryTimeMs = ruleOptions.maxRetryTime * 1000;
if (retryWaitTimeMs <= maxRetryTimeMs) {
await waitTimeMs(retryWaitTimeMs);
if (retryAfter) {
const retryAfterMs = Number(retryAfter) * 1000;
const maxRetryAfterTimeMs = ruleOptions.maxRetryAfterTime * 1000;
if (retryAfterMs <= maxRetryAfterTimeMs) {
await waitTimeMs(retryAfterMs);
}
} else {
// exponential retry: 0ms -> 100ms -> 200ms -> 400ms -> 800ms ...
const retryWaitTimeMs = currentRetryCount ** 2 * 100;
const maxRetryTimeMs = ruleOptions.maxRetryTime * 1000;
if (retryWaitTimeMs <= maxRetryTimeMs) {
await waitTimeMs(retryWaitTimeMs);
}
}
return isAliveURI(uri, "GET", maxRetryCount, currentRetryCount + 1);
}
Expand Down

0 comments on commit dbeed6b

Please sign in to comment.