Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: configurable status code behaviours (#340)
- Loading branch information
1 parent
7d8897c
commit 3b7487e
Showing
7 changed files
with
115 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import {Link, Store} from '..'; | ||
import {Logger, Print} from '../../../logger'; | ||
import {delay, isStatusCodeInRange} from '../../../util'; | ||
import {Config} from '../../../config'; | ||
|
||
type Backoff = { | ||
count: number; | ||
time: number; | ||
}; | ||
|
||
const stores: Record<string, Backoff> = {}; | ||
|
||
export async function processBackoffDelay(store: Store, link: Link, statusCode: number): Promise<number> { | ||
/** | ||
* We treat statusCode 0 as successful as some of the puppeteer plugins | ||
* cause side-effects resulting in an empty response object even though | ||
* the page renders fine and its content is accessible. | ||
*/ | ||
|
||
let backoffStatusCodes = store.backoffStatusCodes; | ||
|
||
if (!backoffStatusCodes) { | ||
backoffStatusCodes = [403]; | ||
} | ||
|
||
const isBackoff = isStatusCodeInRange(statusCode, backoffStatusCodes); | ||
let backoff = stores[store.name]; | ||
|
||
if (!backoff) { | ||
backoff = {count: 0, time: Config.browser.minBackoff}; | ||
stores[store.name] = backoff; | ||
} | ||
|
||
if (!isBackoff) { | ||
if (backoff.count > 0) { | ||
backoff.count--; | ||
backoff.time = Math.max(backoff.time / 2, Config.browser.minBackoff); | ||
} | ||
|
||
return -1; | ||
} | ||
|
||
const backoffTime = backoff.time; | ||
Logger.debug(Print.backoff(link, store, {delay: backoffTime, statusCode}, true)); | ||
|
||
await delay(backoff.time); | ||
|
||
backoff.count++; | ||
backoff.time = Math.min(backoff.time * 2, Config.browser.maxBackoff); | ||
|
||
return backoffTime; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters