Skip to content

Commit 0f5b1b3

Browse files
committed
chore(postgrest): address copilot comments
1 parent d0ef8a5 commit 0f5b1b3

3 files changed

Lines changed: 28 additions & 13 deletions

File tree

packages/core/postgrest-js/src/PostgrestBuilder.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,24 @@ import { ContainsNull } from './select-query-parser/types'
1818

1919
/**
2020
* Sleep for a given number of milliseconds.
21+
* If an AbortSignal is provided, the sleep resolves early when the signal is aborted.
2122
*/
22-
function sleep(ms: number): Promise<void> {
23-
return new Promise((resolve) => setTimeout(resolve, ms))
23+
function sleep(ms: number, signal?: AbortSignal): Promise<void> {
24+
return new Promise((resolve) => {
25+
if (signal?.aborted) {
26+
resolve()
27+
return
28+
}
29+
const id = setTimeout(() => {
30+
signal?.removeEventListener('abort', onAbort)
31+
resolve()
32+
}, ms)
33+
function onAbort() {
34+
clearTimeout(id)
35+
resolve()
36+
}
37+
signal?.addEventListener('abort', onAbort)
38+
})
2439
}
2540

2641
/**
@@ -159,9 +174,9 @@ export default abstract class PostgrestBuilder<
159174
*
160175
* Configure retry behavior for this request.
161176
*
162-
* By default, retries are enabled for GET/HEAD requests that fail with
163-
* 520 status codes (Cloudflare timeout/connection errors). Retries use
164-
* exponential backoff (1s, 2s, 4s) with a maximum of 3 attempts.
177+
* By default, retries are enabled for idempotent requests (GET, HEAD, OPTIONS)
178+
* that fail with network errors or specific HTTP status codes (503, 520).
179+
* Retries use exponential backoff (1s, 2s, 4s) with a maximum of 3 attempts.
165180
*
166181
* @param enabled - Whether to enable retries for this request
167182
*
@@ -251,7 +266,7 @@ export default abstract class PostgrestBuilder<
251266
if (this.retryEnabled && attemptCount < DEFAULT_MAX_RETRIES) {
252267
const delay = getRetryDelay(attemptCount)
253268
attemptCount++
254-
await sleep(delay)
269+
await sleep(delay, this.signal)
255270
continue
256271
}
257272

@@ -268,7 +283,7 @@ export default abstract class PostgrestBuilder<
268283
: getRetryDelay(attemptCount)
269284
await res.text()
270285
attemptCount++
271-
await sleep(delay)
286+
await sleep(delay, this.signal)
272287
continue
273288
}
274289

packages/core/postgrest-js/src/PostgrestClient.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ export default class PostgrestClient<
5555
* @param options.timeout - Optional timeout in milliseconds for all requests. When set, requests will automatically abort after this duration to prevent indefinite hangs.
5656
* @param options.urlLengthLimit - Maximum URL length in characters before warnings/errors are triggered. Defaults to 8000.
5757
* @param options.retry - Enable or disable automatic retries for transient errors.
58-
* When enabled, GET/HEAD requests that fail with 520 errors (Cloudflare timeouts)
59-
* will be automatically retried up to 3 times with exponential backoff.
60-
* Defaults to `true`.
58+
* When enabled, idempotent requests (GET, HEAD, OPTIONS) that fail with network
59+
* errors or HTTP 503/520 responses will be automatically retried up to 3 times
60+
* with exponential backoff (1s, 2s, 4s). Defaults to `true`.
6161
* @example
6262
* ```ts
6363
* import { PostgrestClient } from '@supabase/postgrest-js'

packages/core/postgrest-js/src/PostgrestQueryBuilder.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ export default class PostgrestQueryBuilder<
2424

2525
/**
2626
* Enable or disable automatic retries for transient errors.
27-
* When enabled, idempotent requests (GET/HEAD/OPTIONS) that fail with 520 or 503 errors
28-
* are automatically retried with exponential backoff (up to 3 attempts).
29-
* Defaults to `true` when not specified.
27+
* When enabled, idempotent requests (GET/HEAD/OPTIONS) that fail with network
28+
* errors or HTTP 503/520 responses are automatically retried with exponential
29+
* backoff (1s, 2s, 4s, up to 3 attempts). Defaults to `true` when not specified.
3030
*/
3131
retry?: boolean
3232

0 commit comments

Comments
 (0)