Repository navigation
feat(postgrest): abort requests - #1368
Conversation
|
For now waiting for the Dart sdk bump across all packages. |
|
Now that we've already bumped the minimum versions in v2 this has not to wait for v3. |
spydon
left a comment
There was a problem hiding this comment.
Lgtm, I'll make sure that we add an example in the docs for this.
There was a problem hiding this comment.
LLM review:
Two things I'd like to sort out before merging:
-
API shape. The builder only ever reads
abortCompleter.future, it never completes it. Taking aCompleterforces callers to allocate one even when they already have a future. Accepting the trigger directly is strictly more flexible and matches howhttp'sAbortablemodels it:final response = await client.from('table').select() .abortSignal(Future.delayed(Duration(seconds: 5)));
On naming: you raised this yourself, and I'd lean towards
abortSignal(Future<void> signal). It mirrors supabase-js's.abortSignal, and aFutureis the natural Dart analog of anAbortSignal. If we change the name, let's update thesdk-compliance.yamlsymbol to match. -
The exception isn't reachable by consumers.
RequestAbortedExceptioncomes frompackage:httpand isn't re-exported bypostgrest(orsupabase), so theon RequestAbortedExceptioncatch in your example only compiles if the user addshttpas a direct dependency. Could we re-export it frompostgrest.dart, or wrap it in a postgrest-native exception?
A few minor things, none blocking:
- The empty
if/elsebranches in the newsendclosure are a bit awkward. Settingrequest.bodyonly for post/put/patch and validating unknown methods separately would read cleaner. - Bumping the shared
_buildClientretry delay to 500ms plus the mock's 200ms delay slows the whole retry suite noticeably. Could the delay be scoped to just the abort test? - The
basic_testabort test catches the exception and only prints it, relying on the elapsed-time bounds. An explicitthrowsA(isA<RequestAbortedException>())would fail louder if abort silently regressed.
Deferring functions/storage to a follow-up is fine by me.
More info:
The PR's Completer is one indirection above the http primitive. It only unwraps to .future internally anyway, so wrapping it adds nothing. Two supporting points from http's own docs:
- It explicitly blesses both patterns: Completer for event-driven abort, Future.delayed for timeouts. Accepting a Future supports both; accepting a Completer only covers the event case cleanly (a timeout caller would have to build a Completer + Timer instead of a one-liner Future.delayed).
- "This future must not complete with an error." — a contract worth mirroring in our doc comment.
…eout Makes the automatic retry behavior configurable through retryEnabled, retryCount and retryableStatusCodes on PostgrestClient and PostgrestClientOptions, and per request via PostgrestBuilder.retry (which now also accepts a count). All defaults preserve the previous behavior. Rebased on top of the request abort feature (#1368) and now builds the request timeout on top of it. A configured requestTimeout aborts the in-flight request through AbortableRequest and stops any pending retries instead of merely abandoning the future like Future.timeout would. The retry loop rethrows RequestAbortedException so an abort always stops retries, and requestTimeout and abortSignal are threaded through the reconstructing builders so they survive withConverter and count chains.
…eout Makes the automatic retry behavior configurable through retryEnabled, retryCount and retryableStatusCodes on PostgrestClient and PostgrestClientOptions, and per request via PostgrestBuilder.retry (which now also accepts a count). All defaults preserve the previous behavior. Rebased on top of the request abort feature (#1368) and now builds the request timeout on top of it. A configured requestTimeout aborts the in-flight request through AbortableRequest and stops any pending retries instead of merely abandoning the future like Future.timeout would. The retry loop rethrows RequestAbortedException so an abort always stops retries, and requestTimeout and abortSignal are threaded through the reconstructing builders so they survive withConverter and count chains.
…eout Makes the automatic retry behavior configurable through retryEnabled, retryCount and retryableStatusCodes on PostgrestClient and PostgrestClientOptions, and per request via PostgrestBuilder.retry (which now also accepts a count). All defaults preserve the previous behavior. Rebased on top of the request abort feature (#1368) and now builds the request timeout on top of it. A configured requestTimeout aborts the in-flight request through AbortableRequest and stops any pending retries instead of merely abandoning the future like Future.timeout would. The retry loop rethrows RequestAbortedException so an abort always stops retries, and requestTimeout and abortSignal are threaded through the reconstructing builders so they survive withConverter and count chains.
…eout Makes the automatic retry behavior configurable through retryEnabled, retryCount and retryableStatusCodes on PostgrestClient and PostgrestClientOptions, and per request via PostgrestBuilder.retry (which now also accepts a count). All defaults preserve the previous behavior. Rebased on top of the request abort feature (#1368) and now builds the request timeout on top of it. A configured requestTimeout aborts the in-flight request through AbortableRequest and stops any pending retries instead of merely abandoning the future like Future.timeout would. The retry loop rethrows RequestAbortedException so an abort always stops retries, and requestTimeout and abortSignal are threaded through the reconstructing builders so they survive withConverter and count chains.
…eout Makes the automatic retry behavior configurable through retryEnabled, retryCount and retryableStatusCodes on PostgrestClient and PostgrestClientOptions, and per request via PostgrestBuilder.retry (which now also accepts a count). All defaults preserve the previous behavior. Rebased on top of the request abort feature (#1368) and now builds the request timeout on top of it. A configured requestTimeout aborts the in-flight request through AbortableRequest and stops any pending retries instead of merely abandoning the future like Future.timeout would. The retry loop rethrows RequestAbortedException so an abort always stops retries, and requestTimeout and abortSignal are threaded through the reconstructing builders so they survive withConverter and count chains.
## Summary Makes the PostgREST client's automatic retry behavior configurable and adds an optional per-request timeout that actually cancels a stalled attempt. Previously the retry count and the retryable conditions were hard-coded, and there was no way to bound the duration of a request. Rebased on top of the request abort feature (#1368), whose `AbortableRequest` support is what makes a real timeout possible. All defaults preserve the current behavior, so the change is non-breaking. ## Configurable auto-retry New optional configuration on `PostgrestClient` (and per request via `PostgrestBuilder.retry`): - `retryEnabled` (default `true`): whether retries are performed for retryable GET and HEAD requests. - `retryCount` (default `3`): number of retry attempts for a retryable request. A negative value throws an `ArgumentError`. - `retryableStatusCodes` (default `{503, 520}`): HTTP status codes that trigger a retry. The set is copied defensively so later mutation of the caller's set does not change retry behavior. - `PostgrestBuilder.retry({bool enabled = true, int? count, Duration? requestTimeout})`: the per-request override now also accepts a `count` and a `requestTimeout`. A `null` `requestTimeout` keeps the client-level timeout. The default retryable status codes are exposed as `PostgrestClient.defaultRetryableStatusCodes`, since the client is where retries are configured. ## Request timeout `requestTimeout` is available on `PostgrestClient` and `PostgrestClientOptions`, and can be overridden per request through `PostgrestBuilder.retry`. It bounds how long a single request attempt may take and is built on top of the abort mechanism from #1368, so it actually cancels a stalled attempt through `AbortableRequest` rather than leaving it running the way `Future.timeout` would. When `null` (the default) no timeout is applied. It is a per-attempt timeout, not a whole-operation deadline: a timed-out attempt is retried like any other transient failure, and a `TimeoutException` is thrown only once the retries are exhausted. A caller-provided `PostgrestBuilder.abortSignal`, by contrast, cancels the request outright and stops retrying immediately with a `RequestAbortedException`. Internally both share the same abort trigger per attempt; the source that fired is tracked so a timeout surfaces as `TimeoutException` while a manual abort keeps `RequestAbortedException`. If you need a hard deadline across all retries, wrap the returned future with `.timeout(...)` (`PostgrestBuilder` implements `Future`). ## Configuration through SupabaseClient `retryEnabled`, `retryCount`, `retryableStatusCodes`, and `requestTimeout` are surfaced through `PostgrestClientOptions` so they can be set on the top-level `SupabaseClient`. ## Builder internals The changes above required threading more state (retry settings, `requestTimeout`, `abortSignal`) through the immutable builder chain, so the copy internals were cleaned up along the way. These are internal-only and non-breaking: - Retry settings are bundled into a single immutable `_RetryConfig` value instead of separate fields, which also fixes `ResponsePostgrestBuilder` silently dropping the retry count and status codes on `count()` chains. - The non-generic request state (url, headers, schema, method, body, transport handles, count, retry, `requestTimeout`, `abortSignal`) is held in a single immutable `_RequestConfig` value with a `copyWith`. The typed builders now share and rewrap one config instance rather than re-listing every field, so `withConverter`, the transform and query chains, and the wrapper builders no longer drop or duplicate request state. ## Compliance matrix - `database.configuration.auto_retry` becomes `implemented`. - `database.configuration.request_timeout` becomes `implemented`, now that request cancellation exists to back it. ## Tests Extended `packages/postgrest/test/retry_test.dart` with cases for configurable retry count, per-request count override, `retryCount: 0`, negative `retryCount` validation, and custom retryable status codes. For the timeout: a timed-out attempt is retried rather than hard-stopped, retries recover once an attempt completes within the timeout, a request that finishes in time is not timed out, a per-request `retry(requestTimeout:)` override times out, and a manual `abortSignal` stops retrying immediately. SDK-1270
What kind of change does this PR introduce?
feature
What is the current behavior?
One cannot abort a started http request. Even by adding a
.timeoutto the feature only ignores the result of the future but does not cancel its execution.What is the new behavior?
Starting with
httpv1.5.0` it is now possible to abort requests.Supabase-js uses
.abortSignal, but since the dart implementation uses futures/completer I switched to.abortCompleter. But I'm not entirely sure and open for discussion about choosing a different name.related: #898
The issue covers the general feature to abort requests, so we can cover aborting the requests in functions in another pr.