-
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(realtime): handle timeout when subscribing to channel (#349)
* fix(realtime): handle timeout when subscribing to channel * test: add test for channel subscribe timeout * Fix typo * test: add test for subscribe with timeout * remove duplicated test
- Loading branch information
Showing
5 changed files
with
272 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// | ||
// Task+withTimeout.swift | ||
// | ||
// | ||
// Created by Guilherme Souza on 19/04/24. | ||
// | ||
|
||
import Foundation | ||
|
||
@discardableResult | ||
package func withTimeout<R: Sendable>( | ||
interval: TimeInterval, | ||
@_inheritActorContext operation: @escaping @Sendable () async throws -> R | ||
) async throws -> R { | ||
try await withThrowingTaskGroup(of: R.self) { group in | ||
defer { | ||
group.cancelAll() | ||
} | ||
|
||
let deadline = Date(timeIntervalSinceNow: interval) | ||
|
||
group.addTask { | ||
try await operation() | ||
} | ||
|
||
group.addTask { | ||
let interval = deadline.timeIntervalSinceNow | ||
if interval > 0 { | ||
try await Task.sleep(nanoseconds: NSEC_PER_SEC * UInt64(interval)) | ||
} | ||
try Task.checkCancellation() | ||
throw TimeoutError() | ||
} | ||
|
||
return try await group.next()! | ||
} | ||
} | ||
|
||
package struct TimeoutError: Error, Hashable {} |
Oops, something went wrong.