-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
540 additions
and
76 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
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 |
---|---|---|
@@ -1,43 +1,113 @@ | ||
import {RequestChunk, ResponseChunk, WardenStream} from "./warden-stream"; | ||
import {TransformCallback} from "stream"; | ||
import {StreamType} from "./stream-factory"; | ||
|
||
interface RetryDecoratedResponse extends ResponseChunk { | ||
retryCount: number; | ||
retryCount: number; | ||
} | ||
|
||
interface RetryDecoratedRequest extends RequestChunk { | ||
retryCount: number; | ||
retryCount: number; | ||
} | ||
|
||
interface RetryConfiguration { | ||
count: number; | ||
delay: number; | ||
logger?: (retry: number) => void; | ||
} | ||
|
||
interface RetryInputConfiguration { | ||
count?: number; | ||
delay?: number; | ||
logger?: (retry: number) => void; | ||
} | ||
|
||
const DEFAULT_RETRY_CONFIGURATION = { | ||
count: 1, | ||
delay: 100, | ||
}; | ||
|
||
const RETRYABLE_ERRORS = ['ECONNRESET', 'ENOTFOUND', 'ESOCKETTIMEDOUT', 'ETIMEDOUT', 'ECONNREFUSED', 'EHOSTUNREACH', 'EPIPE', 'EAI_AGAIN']; | ||
|
||
class Retry extends WardenStream { | ||
retryLimit: number; | ||
private configuration: RetryConfiguration; | ||
|
||
constructor(configuration: RetryConfiguration) { | ||
super(StreamType.RETRY); | ||
|
||
constructor(retryLimit: number) { | ||
super('Retry'); | ||
this.configuration = configuration; | ||
} | ||
|
||
this.retryLimit = retryLimit; | ||
static create(configuration: RetryInputConfiguration | true | number) { | ||
if (typeof configuration === "boolean") { | ||
return new Retry(DEFAULT_RETRY_CONFIGURATION); | ||
} else if (typeof configuration === "number") { | ||
return new Retry({ | ||
count: configuration, | ||
delay: 0 | ||
}); | ||
} else if (typeof configuration === "object") { | ||
const count = configuration.count || 1; | ||
const delay = configuration.delay || 0; | ||
return new Retry({ | ||
count, | ||
delay, | ||
logger: configuration.logger | ||
}); | ||
} else { | ||
return new Retry(DEFAULT_RETRY_CONFIGURATION); | ||
} | ||
} | ||
|
||
async onResponse(chunk: RetryDecoratedResponse, callback: TransformCallback): Promise<void> { | ||
if (chunk.error && chunk.retryCount <= this.retryLimit) { | ||
this.request<RetryDecoratedRequest>({ | ||
...chunk, | ||
retryCount: chunk.retryCount + 1 | ||
}); | ||
callback(undefined, null); | ||
} else { | ||
callback(undefined, chunk); | ||
} | ||
async onResponse(chunk: RetryDecoratedResponse, callback: TransformCallback): Promise<void> { | ||
if (chunk.retryCount <= this.configuration.count && this.shouldRetry(chunk)) { | ||
if (this.configuration.delay > 0) { | ||
setTimeout(() => { | ||
this.retry(chunk); | ||
}, this.configuration.delay); | ||
} else { | ||
this.retry(chunk); | ||
} | ||
callback(undefined, null); | ||
} else { | ||
callback(undefined, chunk); | ||
} | ||
} | ||
|
||
async onRequest(chunk: RetryDecoratedRequest, callback: TransformCallback): Promise<void> { | ||
callback(null, { | ||
...chunk, | ||
retryCount: 0 | ||
} as RequestChunk); | ||
async onRequest(chunk: RetryDecoratedRequest, callback: TransformCallback): Promise<void> { | ||
callback(null, { | ||
...chunk, | ||
retryCount: 0 | ||
} as RequestChunk); | ||
} | ||
|
||
private retry(chunk: RetryDecoratedResponse) { | ||
if (this.configuration.logger) this.configuration.logger(chunk.retryCount + 1); | ||
this.request<RetryDecoratedRequest>({ | ||
retryCount: chunk.retryCount + 1, | ||
cb: chunk.cb, | ||
key: chunk.key, | ||
requestOptions: chunk.requestOptions | ||
}); | ||
} | ||
|
||
private shouldRetry(chunk: RetryDecoratedResponse) { | ||
const statusCode = chunk.response ? chunk.response.statusCode : null; | ||
|
||
if (statusCode && (statusCode === 429 || (500 <= statusCode && statusCode < 600))) { | ||
return true; | ||
} | ||
|
||
if (chunk.error) { | ||
return RETRYABLE_ERRORS.includes(chunk.error.code); | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
|
||
export { | ||
Retry | ||
RetryInputConfiguration, | ||
RetryConfiguration, | ||
Retry | ||
}; |
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
Oops, something went wrong.