Skip to content

Commit

Permalink
allow disconnect() to cancel reconnection
Browse files Browse the repository at this point in the history
  • Loading branch information
lhoward committed Nov 28, 2024
1 parent cfad7e6 commit fb50252
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion Sources/SwiftOCA/OCP.1/Ocp1Connection+Connect.swift
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,11 @@ extension Ocp1Connection {
}

public func disconnect() async throws {
await removeSubscriptions()
// explicitly checking for isConnected allows disconnect() to also
// asynchronously cancel a reconnection
if isConnected {
await removeSubscriptions()
}

_updateConnectionState(.notConnected)

Expand Down Expand Up @@ -273,6 +277,11 @@ extension Ocp1Connection {
}
logger.trace("reconnection failed with \(error), sleeping for \(backoff)")
try await Task.sleep(for: backoff)
// check for asynchronous explicit disconnection and break
if _connectionState.value == .notConnected {
logger.debug("reconnection cancelled")
throw CancellationError()
}
}
}

Expand Down

0 comments on commit fb50252

Please sign in to comment.