Check for retryable status codes also
This commit is contained in:
+27
-4
@@ -1,9 +1,30 @@
|
|||||||
|
import {XRPCError} from '@atproto/xrpc'
|
||||||
|
|
||||||
import {isNetworkError} from 'lib/strings/errors'
|
import {isNetworkError} from 'lib/strings/errors'
|
||||||
|
|
||||||
|
export const NETWORK_FAILURE_STATUSES = [
|
||||||
|
1, 408, 425, 429, 500, 502, 503, 504, 522, 524,
|
||||||
|
]
|
||||||
|
|
||||||
|
function isRetryable(e: unknown) {
|
||||||
|
if (e instanceof XRPCError) {
|
||||||
|
if (NETWORK_FAILURE_STATUSES.includes(e.status)) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return isNetworkError(e)
|
||||||
|
}
|
||||||
|
|
||||||
export async function retry<P>(
|
export async function retry<P>(
|
||||||
retries: number,
|
|
||||||
cond: (err: any) => boolean,
|
|
||||||
fn: () => Promise<P>,
|
fn: () => Promise<P>,
|
||||||
|
{
|
||||||
|
retries,
|
||||||
|
checkIsRetryable = isRetryable,
|
||||||
|
}: {
|
||||||
|
retries: number
|
||||||
|
checkIsRetryable?: (err: any) => boolean
|
||||||
|
},
|
||||||
): Promise<P> {
|
): Promise<P> {
|
||||||
let lastErr
|
let lastErr
|
||||||
while (retries > 0) {
|
while (retries > 0) {
|
||||||
@@ -11,7 +32,7 @@ export async function retry<P>(
|
|||||||
return await fn()
|
return await fn()
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
lastErr = e
|
lastErr = e
|
||||||
if (cond(e)) {
|
if (checkIsRetryable(e)) {
|
||||||
retries--
|
retries--
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -25,5 +46,7 @@ export async function networkRetry<P>(
|
|||||||
retries: number,
|
retries: number,
|
||||||
fn: () => Promise<P>,
|
fn: () => Promise<P>,
|
||||||
): Promise<P> {
|
): Promise<P> {
|
||||||
return retry(retries, isNetworkError, fn)
|
return retry(fn, {
|
||||||
|
retries,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user