-
Notifications
You must be signed in to change notification settings - Fork 18
Adopt NonisolatedNonsendingByDefault
#272
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
e57fb03
5b23941
ecd0889
82129b7
e7c1311
c517bcb
19cb5fd
0f603a6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,15 +18,13 @@ extension ValkeyConnection { | |
| /// | ||
| /// - Parameters: | ||
| /// - shardchannels: list of shard channels to subscribe to | ||
| /// - isolation: Actor isolation | ||
| /// - process: Closure that is called with subscription async sequence | ||
| /// - Returns: Return value of closure | ||
| @inlinable | ||
| public func ssubscribe<Value>( | ||
| public nonisolated func ssubscribe<Value>( | ||
| to shardchannels: String..., | ||
| isolation: isolated (any Actor)? = #isolation, | ||
| process: (ValkeySubscription) async throws -> sending Value | ||
| ) async throws -> sending Value { | ||
| process: (ValkeySubscription) async throws -> Value | ||
| ) async throws -> Value { | ||
| try await self.ssubscribe(to: shardchannels, process: process) | ||
| } | ||
|
|
||
|
|
@@ -39,18 +37,15 @@ extension ValkeyConnection { | |
| /// | ||
| /// - Parameters: | ||
| /// - shardchannels: list of shard channels to subscribe to | ||
| /// - isolation: Actor isolation | ||
| /// - process: Closure that is called with subscription async sequence | ||
| /// - Returns: Return value of closure | ||
| @inlinable | ||
| public func ssubscribe<Value>( | ||
| public nonisolated func ssubscribe<Value>( | ||
| to shardchannels: [String], | ||
| isolation: isolated (any Actor)? = #isolation, | ||
| process: (ValkeySubscription) async throws -> sending Value | ||
| ) async throws -> sending Value { | ||
| process: (ValkeySubscription) async throws -> Value | ||
| ) async throws -> Value { | ||
| try await self._subscribe( | ||
| command: SSUBSCRIBE(shardchannels: shardchannels), | ||
| isolation: isolation, | ||
| process: process | ||
| ) | ||
| } | ||
|
|
@@ -65,33 +60,31 @@ extension ValkeyConnection { | |
| /// channel | ||
| /// | ||
| /// - Parameters: | ||
| /// - isolation: Actor isolation | ||
| /// - process: Closure that is called with async sequence of key invalidations | ||
| /// - Returns: Return value of closure | ||
| @inlinable | ||
| public func subscribeKeyInvalidations<Value>( | ||
| isolation: isolated (any Actor)? = #isolation, | ||
| process: (AsyncMapSequence<ValkeySubscription, ValkeyKey>) async throws -> sending Value | ||
| ) async throws -> sending Value { | ||
| public nonisolated func subscribeKeyInvalidations<Value>( | ||
| process: (AsyncMapSequence<ValkeySubscription, ValkeyKey>) async throws -> Value | ||
| ) async throws -> Value { | ||
| try await self.subscribe(to: [ValkeySubscriptions.invalidateChannel]) { subscription in | ||
| let keys = subscription.map { ValkeyKey($0.message) } | ||
| return try await process(keys) | ||
| } | ||
| } | ||
|
|
||
| #if compiler(>=6.2) | ||
| /// Execute subscribe command and run closure using related ``ValkeySubscription`` | ||
| /// AsyncSequence | ||
| /// | ||
| /// This should not be called directly, used the related commands | ||
| /// ``ValkeyConnection/subscribe(to:isolation:process:)`` or | ||
| /// ``ValkeyConnection/psubscribe(to:isolation:process:)`` | ||
| /// ``ValkeyConnection/subscribe(to:process:)`` or | ||
| /// ``ValkeyConnection/psubscribe(to:process:)`` | ||
| @inlinable | ||
| public func _subscribe<Value>( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of using a later swift-format which the majority of people aren't using yet can you add this instead // swift-format-ignore
@inlinable
public nonisolated(nonsending) func _subscribe<Value>(
command: some ValkeySubscribeCommand,
process: nonisolated(nonsending) (ValkeySubscription) async throws -> Value
) async throws -> Value { |
||
| public nonisolated(nonsending) func _subscribe<Value>( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this |
||
| command: some ValkeySubscribeCommand, | ||
| isolation: isolated (any Actor)? = #isolation, | ||
| process: (ValkeySubscription) async throws -> sending Value | ||
| ) async throws -> sending Value { | ||
| let (id, stream) = try await subscribe(command: command, filters: command.filters) | ||
| process: nonisolated(nonsending) (ValkeySubscription) async throws -> Value | ||
| ) async throws -> Value { | ||
| let (id, stream) = try await self.subscribe(command: command, filters: command.filters) | ||
| let value: Value | ||
| do { | ||
| value = try await process(stream) | ||
|
|
@@ -109,6 +102,37 @@ extension ValkeyConnection { | |
| }.value | ||
| return value | ||
| } | ||
| #else | ||
| /// Execute subscribe command and run closure using related ``ValkeySubscription`` | ||
| /// AsyncSequence | ||
| /// | ||
| /// This should not be called directly, used the related commands | ||
| /// ``ValkeyConnection/subscribe(to:process:)`` or | ||
| /// ``ValkeyConnection/psubscribe(to:process:)`` | ||
| @inlinable | ||
| public nonisolated func _subscribe<Value>( | ||
| command: some ValkeySubscribeCommand, | ||
| process: (ValkeySubscription) async throws -> Value | ||
| ) async throws -> Value { | ||
| let (id, stream) = try await self.subscribe(command: command, filters: command.filters) | ||
| let value: Value | ||
| do { | ||
| value = try await process(stream) | ||
| try Task.checkCancellation() | ||
| } catch { | ||
| // call unsubscribe in unstructured Task to avoid it being cancelled | ||
| _ = await Task { | ||
| try await unsubscribe(id: id) | ||
| }.result | ||
| throw error | ||
| } | ||
| // call unsubscribe in unstructured Task to avoid it being cancelled | ||
| _ = try await Task { | ||
| try await unsubscribe(id: id) | ||
| }.value | ||
| return value | ||
| } | ||
| #endif | ||
|
|
||
| @usableFromInline | ||
| func subscribe( | ||
|
|
@@ -120,7 +144,7 @@ extension ValkeyConnection { | |
| if Task.isCancelled { | ||
| throw ValkeyClientError(.cancelled) | ||
| } | ||
| let subscriptionID: Int = try await withCheckedThrowingContinuation { continuation in | ||
| let subscriptionID: Int = try await withCheckedThrowingContinuation(isolation: self) { continuation in | ||
| self.channelHandler.subscribe( | ||
| command: command, | ||
| streamContinuation: streamContinuation, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -51,8 +51,19 @@ public struct ValkeySubscription: AsyncSequence, Sendable { | |
| public struct AsyncIterator: AsyncIteratorProtocol { | ||
| var base: BaseAsyncSequence.AsyncIterator | ||
|
|
||
| #if compiler(>=6.2) | ||
| @concurrent | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could this be public mutating func next() async throws -> Element? {
try await self.base.next(isolation: #isolation)
}Instead of adding |
||
| public mutating func next() async throws -> Element? { | ||
| try await self.base.next() | ||
| } | ||
| #else | ||
| public mutating func next() async throws -> Element? { | ||
| try await self.base.next() | ||
| } | ||
| #endif | ||
|
|
||
| public mutating func next(isolation actor: isolated (any Actor)?) async throws(any Error) -> ValkeySubscriptionMessage? { | ||
| try await self.base.next(isolation: actor) | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use
// swift-format-ignoreto get around swift-format not understandingnonisolated(nonsending)