Skip to content

SSE: reconnecting Stream with Last-Event-ID replay#13

Merged
OmarAlJarrah merged 5 commits into
mainfrom
feat/sse-reconnect
Jun 16, 2026
Merged

SSE: reconnecting Stream with Last-Event-ID replay#13
OmarAlJarrah merged 5 commits into
mainfrom
feat/sse-reconnect

Conversation

@OmarAlJarrah

Copy link
Copy Markdown
Member

Summary

Completes the SSE subsystem with sse.Stream, a reconnecting consumer built on
the existing Parse. It auto-reconnects on stream end, replays the
Last-Event-ID, and honors the server's retry backoff.

What's included

  • ConnectFunc(ctx, lastEventID) — the caller opens a fresh event-stream
    (receiving the most recent event id to resume via Last-Event-ID). This keeps
    sse decoupled from any particular HTTP client.
  • Stream(ctx, connect, opts...) iter.Seq2[Event, error] — loops: connect →
    parse events → on stream end (EOF or a mid-stream read error) wait the
    reconnection delay → reconnect with the latest event id. A connect error is
    terminal (yielded); a mid-stream read error reconnects transparently. Cancel
    ctx to stop.
  • WithReconnectDelay(d) — initial delay (default 3s), overridden by a server
    retry value for subsequent reconnects (EventSource semantics); <= 0
    reconnects immediately.

Background

This was the reconnection layer deferred from the SSE subsystem. The reconnect
wait is behind an unexported test seam so the retry-backoff wiring is verified
deterministically (no real sleeps); cancellation is checked before the wait so a
canceled context never triggers a spurious reconnect.

Behavior changes

None — additive to the sse package.

Test plan

  • go build ./...
  • go vet ./...
  • gofmt -l . (clean)
  • go test -race ./... (all packages pass; SSE run repeated with -count=3, no flakes)
  • Covered: reconnect-and-flatten across connections; Last-Event-ID replayed to
    the next connect; mid-stream read error reconnects transparently (error not
    surfaced); connect error is terminal; cancellation stops with no further
    connect; consumer break closes the reader; a server retry overrides the delay
    (verified via the injected wait recorder).

🤖 Generated with Claude Code

OmarAlJarrah and others added 5 commits June 16, 2026 21:20
Stream over a caller ConnectFunc with Last-Event-ID replay, server-retry backoff,
and deterministic cancel/reconnect via an injectable wait seam.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@OmarAlJarrah OmarAlJarrah merged commit 5529a40 into main Jun 16, 2026
1 of 2 checks passed
@OmarAlJarrah OmarAlJarrah deleted the feat/sse-reconnect branch June 16, 2026 18:31
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