Skip to content

feat(network): handle a busy server with Retry-After (retryAfter/retryCode, gate, reset_retry_after) - #89

Merged
andinux merged 9 commits into
mainfrom
feat/soft-overload-retry-after
Oct 21, 2026
Merged

andinux merged 9 commits into
mainfrom
feat/soft-overload-retry-after

Conversation

@andinux

@andinux andinux commented Oct 20, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Client side of CloudSync soft-overload handling, compatible with the server changes proposed in sqliteai/cloudsync#94 (S1 delayed /check, S3 rate limit) and tenant load shedding. Against current servers, which send neither 429 nor Retry-After, behaviour is unchanged.

  • Busy is a result, not an SQL error. A 429, or a 503 carrying Retry-After, on /apply, /upload, /status or /check returns the normal JSON with retryAfter (seconds; 5 when a 429 carries no usable hint) and retryCode (errors[0].code, or http_<status>) in the refused send/receive block. A 503 without Retry-After (an outage), or with an HTTP-date value, stays an error.
  • Never "call again now": a busy or held send reports status: "busy", a busy or held receive reports complete: true, so loops written against the earlier API.md guidance stop after one call (see the comments below).
  • Send: a busy reply stops the batch and keeps the checkpoint, including after a mid-batch refusal; the next call re-sends from chunk 0 as a new batch. sync() still runs its receive phase after a busy send.
  • Receive loop: sync(wait_ms, max_retries) waits a Retry-After instead of wait_ms when it fits the remaining wait_ms × (max_retries − 1) budget, and returns reporting it otherwise. A call never waits longer than before.
  • Gate: until a deadline passes, send and receive (separate deadlines, in memory per connection, monotonic clock) return the same result with a countdown retryAfter and make no request. New cloudsync_network_reset_retry_after() lifts it for user-requested refreshes; a new cloudsync_network_init() clears it, a token refresh keeps it. has_unsent_changes() and network_status() stay ungated.
  • Transports: NETWORK_RESULT gains trailing http_status, has_retry_after, retry_after, filled by libcurl and network.m. Custom transports that leave them zero keep the previous behaviour (docs/internal/network.md).

Jitter is requested server-side in https://github.com/sqliteai/cloudsync/issues/94#issuecomment-6294151260. Wrappers need no change: the new fields flow through the SQL JSON. Changelog entries are under [Unreleased].

Test plan

  • make network-unittest: loopback HTTP server cases for 429/503 with and without Retry-After, HTTP-date, busy /upload, mid-batch busy, delayed and busy /check, sync after busy send, budget waits and early return, gated send/receive/sync, pending deadline waited within budget, reset; Retry-After parsing
  • Old-style loops (send until not out-of-sync, receive while not complete) stop after one call against a busy server
  • Mutation checks on the busy rule, the budget logic, the checkpoint fallback, the gate, the default wait, the status/complete rules and the init reset (all caught)
  • make unittest
  • network.m and CLOUDSYNC_OMIT_CURL builds compile without warnings locally
  • Windows and Apple CI (Windows now always includes windows.h, guarded with WIN32_LEAN_AND_MEAN)

🤖 Generated with Claude Code

andinux and others added 3 commits October 20, 2026 10:52
… Retry-After in sync

A CloudSync answer of 429, or 503 with Retry-After, is now a result instead of
an SQL error. send_changes, receive_changes and sync return their normal JSON
with retryAfter (seconds, 0 without a hint) and retryCode (errors[0].code, or
http_<status>) in the refused block. A refused send keeps its checkpoint and is
re-sent as a new batch; sync still runs its receive phase after one.

The sync receive loop waits a Retry-After instead of wait_ms when it fits the
remaining wait_ms * (max_retries - 1) budget, and returns reporting it
otherwise, so a call never waits longer than before. A 503 without Retry-After
and an HTTP-date Retry-After keep today's behaviour, as does every reply from
current servers, which send neither.

NETWORK_RESULT gains trailing http_status/has_retry_after/retry_after fields,
filled by the libcurl and Apple transports; custom transports that leave them
zero keep the previous behaviour.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…loudsync_network_reset_retry_after()

After a busy refusal, or a /check answered with a delay, send and receive now
return the same result with a countdown retryAfter, without contacting the
server, until the deadline passes. Send and receive keep separate deadlines, in
memory per connection, on the monotonic clock. sync(wait_ms, max_retries)
waits out a pending deadline that fits its budget before the first /check.

cloudsync_network_reset_retry_after() clears both deadlines, for refreshes the
user asked for explicitly.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…once

Loops written against the previous API.md guidance (send until status leaves
out-of-sync, receive while complete is false) would spin at full CPU during a
Retry-After wait, and a 429 without Retry-After would resend with no pause.

- A busy or held send reports status "syncing" (also covers a busy /status
  with nothing pending), a busy or held receive reports complete:true.
- A 429 without a usable Retry-After waits a default of 5 seconds.
- A new cloudsync_network_init() clears the wait; a token refresh keeps it.
- A busy refusal whose code cannot be copied sets no wait.
- One helper takes the busy fields at the three call sites.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@andinux

andinux commented Oct 20, 2026 •

Copy link
Copy Markdown
Collaborator Author

Superseded in part: a busy or held send now reports send.status: "busy", not "syncing"; see this follow-up. The receive side is unchanged.

Why a busy result now reports syncing / complete: true (4039c17)

A review of this PR found that apps which update the library without changing their code could misbehave. Until now API.md recommended two loops:

  • cloudsync_network_send_changes(n) repeated until send.status is no longer out-of-sync
  • cloudsync_network_receive_changes(n) repeated while receive.complete is false

Before this PR a busy server raised an SQL error, which ended those loops. With the first version of this PR, a busy reply reported out-of-sync / complete: false with retryAfter, and calls held by the wait returned immediately with no request. An unchanged loop would then spin at full CPU until the deadline. A 429 without Retry-After set no wait at all, so the loop would resend to the overloaded server with no pause.

We can't rely on every app updating its loops, so the results themselves now stop them:

  • A busy or held send reports status: "syncing". API.md already says to stop on syncing and check again on the normal schedule, which is the right action during a wait. Pending changes stay visible through localVersion vs serverVersion and cloudsync_network_has_unsent_changes(). This also fixes a busy /status with nothing to send, which used to report out-of-sync with localVersion == serverVersion.
  • A busy or held receive reports complete: true. That matches the documented meaning: false = "known to have more, call again now", true = "nothing more was ready just now". With retryAfter present more may be pending, and the next call after the delay resumes the drain where it stopped (the page cursor is kept), so nothing is skipped.
  • A 429 without a usable Retry-After waits a default of 5 s, so even a loop that ignores every field stops sending after the first refusal.

The new tests run both old-style loops against a busy server, and both stop after a single call. A loop like until status == 'synced' already spins today on syncing and is outside what this PR can fix.

Also in this commit, from the same review:

  • Wait across databases: a new cloudsync_network_init() clears the wait. A successful logout already frees the network context, and a token refresh keeps the wait, so the server's request isn't bypassed.
  • Out of memory: a busy refusal whose error code can't be copied sets no wait.
  • Docs: API.md notes that has_unsent_changes() stays ungated and raises an SQL error when the server is busy.
  • Cleanup: the busy handling is shared by one helper.

andinux and others added 6 commits October 20, 2026 17:03
Covered by network_unit: the loop drains a backlog in bounded sends and stops
after one call against a busy server.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Tested: the loop stops after two calls when the server keeps failing a send,
and on a WAL database another connection sees each checkpoint commit and can
write during the loop.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…tory (Android has no /tmp)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
syncing keeps one meaning (the server holds the changes); busy means the
server refused the send and asked to wait retryAfter seconds. Loops that
continue only on out-of-sync still stop after one call.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@andinux

andinux commented Oct 21, 2026

Copy link
Copy Markdown
Collaborator Author

Update: a refused send now reports send.status: "busy" (e491a53)

The previous comment explained why a busy or held send reported "syncing". We changed that to a new value, "busy":

  • Before this PR a busy server raised an SQL error, so no existing code handles a busy result yet. A new value breaks nothing that this PR doesn't already change, and the release is still unpublished.
  • "syncing" keeps one meaning: the server holds the changes. "busy" means the server refused the send and asked to wait retryAfter seconds. Nothing is lost: the checkpoint doesn't move.
  • A long overload is now visible to apps that read only status.
  • Old loops still stop after one call, because they only continue on "out-of-sync". The receive side is unchanged: busy or held still reports complete: true.

🤖 Generated with Claude Code

@andinux
andinux merged commit 20a1eaa into main Oct 21, 2026
38 checks passed
@andinux
andinux deleted the feat/soft-overload-retry-after branch October 21, 2026 16:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant