Skip to content

feat: add resilient SSE session with reconnection/backoff#43

Open
dogo wants to merge 4 commits intoRecouse:mainfrom
dogo:feature/reconnect
Open

feat: add resilient SSE session with reconnection/backoff#43
dogo wants to merge 4 commits intoRecouse:mainfrom
dogo:feature/reconnect

Conversation

@dogo
Copy link

@dogo dogo commented Oct 1, 2025

• Implement exponential backoff reconnection with configurable limits (maxReconnectAttempts, reconnectInitialDelay, reconnectBackoffFactor)
• Track reconnect attempts and readyState using thread-safe Mutex; reset attempts on successful .open

• Implement exponential backoff reconnection with configurable limits (maxReconnectAttempts, reconnectInitialDelay, reconnectBackoffFactor)
• Track reconnect attempts and readyState using thread-safe Mutex; reset attempts on successful .open
@dogo dogo requested a review from Recouse as a code owner October 1, 2025 11:11
@dogo
Copy link
Author

dogo commented Nov 12, 2025

Hey @Recouse, I’ve resolved the conflicts and the CI has passed. Could you please review the code when you have a moment?

@dogo
Copy link
Author

dogo commented Feb 20, 2026

Hi @Recouse 👋

Before moving forward with this PR, I just wanted to check if there’s still interest in the feature it introduces. If not, no worries at all, I’m happy to close it. Just let me know 🙂

@Recouse
Copy link
Owner

Recouse commented Feb 21, 2026

Hi,
Yes, I’m interested in this feature. It had reconnection support in earlier versions, but I removed it because the implementation was unstable.

Copy link
Owner

@Recouse Recouse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you tested the implementation? At first glance, it looks fine, but there may be some caveats with SessionDelegate and URLSession instances from the initial request.

/// Helper method for reconnection
private func attemptReconnect(stream continuation: AsyncStream<EventType>.Continuation) {
let delay = reconnectInitialDelay * pow(reconnectBackoffFactor, Double(reconnectAttempts - 1))
DispatchQueue.global().asyncAfter(deadline: .now() + delay) { [weak self] in
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it possible to implement this with Task.sleep()?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Done

/// it can be started by iterating event stream returned by ``DataTask/events()``.
final class DataTask: Sendable {
/// Initializes or reinitializes the SSE session
private func startSession(stream continuation: AsyncStream<EventType>.Continuation) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's more of a stylistic request to declare methods after init()

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@dogo
Copy link
Author

dogo commented Feb 23, 2026

Hi, Yes, I’m interested in this feature. It had reconnection support in earlier versions, but I removed it because the implementation was unstable.

Yes, I’ve tested the reconnection flow with:

  • normal connection
  • forced network drop
  • server-side close
  • manual cancellation

The session is properly invalidated before creating a new one, and the delegate is not reused across sessions to avoid unexpected behavior.

Copy link
Owner

@Recouse Recouse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my tests I've noticed that the current logic relies on the URLSession:task:didCompleteWithError: delegate method to complete the request. I've added an error != nil check to properly close the connection when reconnection is not needed.

There's another issue: reconnection happens indefinitely, because on reconnection readyState becomes .connecting, and after the first response it's set to .open and resets reconnectAttempts. As a result, the request never finishes and re-runs indefinitely.

Comment on lines +293 to +294
// Attempts to reconnect if the limit has not been exceeded
if reconnectAttempts < maxReconnectAttempts {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Attempts to reconnect if the limit has not been exceeded
if reconnectAttempts < maxReconnectAttempts {
// If an error occurred, an error object will indicate how the request failed, otherwise nil.
// Attempts to reconnect on error if the limit has not been exceeded.
if error != nil, reconnectAttempts < maxReconnectAttempts {

@Recouse
Copy link
Owner

Recouse commented Feb 24, 2026

Turns out it requires an overhaul of the DataTask implementation. I also wanted to reduce the Mutex count, and either switch to a single state value or make DataTask an actor. I think it will be better to put this PR on hold and return to it later.

@dogo
Copy link
Author

dogo commented Feb 25, 2026

Turns out it requires an overhaul of the DataTask implementation. I also wanted to reduce the Mutex count, and either switch to a single state value or make DataTask an actor. I think it will be better to put this PR on hold and return to it later.

That makes sense. I agree the current approach introduces complexity in state management and reconnection flow.

Switching DataTask to an actor sounds like a much cleaner solution and would eliminate the need for multiple mutexes.

I'm happy to put this PR on hold and revisit it with a redesigned implementation.

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.

2 participants