Skip to content

Commit

Permalink
SocketPool + IdentifiableContinuation
Browse files Browse the repository at this point in the history
  • Loading branch information
swhitty committed Apr 12, 2024
1 parent fa92879 commit 9fc27df
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions FlyingSocks/Sources/SocketPool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,25 +156,34 @@ public final actor SocketPool<Queue: EventQueue>: AsyncSocketPool {
state = .complete
waiting.cancellAll()
waiting = Waiting()
loop?.cancel()
loop?.resume(throwing: CancellationError())
loop = nil
}

typealias Continuation = CancellingContinuation<Void, SocketError>
private var loop: CancellingContinuation<Void, Never>?
private var loop: IdentifiableContinuation<Void, any Swift.Error>?
private var waiting = Waiting() {
didSet {
if !waiting.isEmpty, let continuation = loop {
continuation.resume()
loop = nil
}
}
}

private func suspendUntilContinuationsExist() async throws {
let continuation = CancellingContinuation<Void, Never>()
loop = continuation
defer { loop = nil }
return try await continuation.value
try await withIdentifiableThrowingContinuation(isolation: self) {
loop = $0
} onCancel: { id in
Task { await self.cancelLoopContinuation(with: id) }
}
}

private func cancelLoopContinuation(with id: IdentifiableContinuation<Void, any Swift.Error>.ID) {
if loop?.id == id {
loop?.resume(throwing: CancellationError())
loop = nil
}
}

private func appendContinuation(_ continuation: Continuation,
Expand Down

0 comments on commit 9fc27df

Please sign in to comment.