fix(corrosion): handle resubscription with from_change=0 that streams rows in new corrosion version

This commit is contained in:
Pasha Sviderski
2026-06-23 09:34:45 +10:00
parent 5f07bc5832
commit 99e1ae0edb
2 changed files with 183 additions and 2 deletions
+25 -2
View File
@@ -287,8 +287,12 @@ func (c *APIClient) resubscribeWithBackoffFn(id string) func(context.Context, ui
}
return func(ctx context.Context, fromChange uint64) (*Subscription, error) {
return backoff.RetryWithData(func() (*Subscription, error) {
slog.Debug("Retrying to resubscribe to Corrosion query.", "id", id, "from_change", fromChange)
return c.ResubscribeContext(ctx, id, fromChange)
sub, err := c.ResubscribeContext(ctx, id, fromChange)
if err != nil {
slog.Debug("Failed to resubscribe to Corrosion query. Retrying with backoff.",
"id", id, "from_change", fromChange, "err", err)
}
return sub, err
}, c.newResubBackoff())
}
}
@@ -320,5 +324,24 @@ func (c *APIClient) ResubscribeContext(ctx context.Context, id string, fromChang
return nil, fmt.Errorf("unexpected status code %d: %s", resp.StatusCode, respBody)
}
// Since https://github.com/superfly/corrosion/pull/355, Corrosion treats a resubscription from change 0 like
// a fresh subscription: it replays the full query snapshot (a columns event, all rows, and an end-of-query event)
// before streaming changes. We don't expose rows in this case, so drain the snapshot here before consuming changes.
if fromChange == 0 {
rows, err := newRows(ctx, resp.Body, false)
if err != nil {
resp.Body.Close()
return nil, fmt.Errorf("parse resubscribe response: %w", err)
}
// Drain the replayed rows until the end-of-query event to reach the change stream.
for rows.Next() {
}
if err = rows.Err(); err != nil {
resp.Body.Close()
return nil, fmt.Errorf("drain resubscribe snapshot: %w", err)
}
return newSubscription(ctx, id, nil, rows.body, rows.decoder, c.resubscribeWithBackoffFn(id)), nil
}
return newSubscription(ctx, id, nil, resp.Body, nil, c.resubscribeWithBackoffFn(id)), nil
}