|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the valkey-swift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2025 the valkey-swift project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of valkey-swift project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import NIOCore |
| 16 | + |
| 17 | +@available(valkeySwift 1.0, *) |
| 18 | +extension ValkeyClient { |
| 19 | + /// Subscribe to list of channels and run closure with subscription |
| 20 | + /// |
| 21 | + /// When the closure is exited the channels are automatically unsubscribed from. It is |
| 22 | + /// possible to have multiple subscriptions running on the same connection and unsubscribe |
| 23 | + /// commands will only be sent to Valkey when there are no subscriptions active for that |
| 24 | + /// channel |
| 25 | + /// |
| 26 | + /// - Parameters: |
| 27 | + /// - channels: list of channels to subscribe to |
| 28 | + /// - isolation: Actor isolation |
| 29 | + /// - process: Closure that is called with subscription async sequence |
| 30 | + /// - Returns: Return value of closure |
| 31 | + @inlinable |
| 32 | + public func subscribe<Value>( |
| 33 | + to channels: String..., |
| 34 | + process: (ValkeySubscription) async throws -> sending Value |
| 35 | + ) async throws -> Value { |
| 36 | + try await self.subscribe(to: channels, process: process) |
| 37 | + } |
| 38 | + |
| 39 | + @inlinable |
| 40 | + /// Subscribe to list of channels and run closure with subscription |
| 41 | + /// |
| 42 | + /// When the closure is exited the channels are automatically unsubscribed from. It is |
| 43 | + /// possible to have multiple subscriptions running on the same connection and unsubscribe |
| 44 | + /// commands will only be sent to Valkey when there are no subscriptions active for that |
| 45 | + /// channel |
| 46 | + /// |
| 47 | + /// - Parameters: |
| 48 | + /// - channels: list of channels to subscribe to |
| 49 | + /// - isolation: Actor isolation |
| 50 | + /// - process: Closure that is called with subscription async sequence |
| 51 | + /// - Returns: Return value of closure |
| 52 | + public func subscribe<Value>( |
| 53 | + to channels: [String], |
| 54 | + process: (ValkeySubscription) async throws -> sending Value |
| 55 | + ) async throws -> Value { |
| 56 | + try await self.subscribe( |
| 57 | + command: SUBSCRIBE(channel: channels), |
| 58 | + filters: channels.map { .channel($0) }, |
| 59 | + process: process |
| 60 | + ) |
| 61 | + } |
| 62 | + |
| 63 | + /// Subscribe to list of channel patterns and run closure with subscription |
| 64 | + /// |
| 65 | + /// When the closure is exited the patterns are automatically unsubscribed from. It is |
| 66 | + /// possible to have multiple subscriptions running on the same connection and unsubscribe |
| 67 | + /// commands will only be sent to Valkey when there are no subscriptions active for that |
| 68 | + /// pattern |
| 69 | + /// |
| 70 | + /// - Parameters: |
| 71 | + /// - patterns: list of channel patterns to subscribe to |
| 72 | + /// - isolation: Actor isolation |
| 73 | + /// - process: Closure that is called with subscription async sequence |
| 74 | + /// - Returns: Return value of closure |
| 75 | + @inlinable |
| 76 | + public func psubscribe<Value>( |
| 77 | + to patterns: String..., |
| 78 | + process: (ValkeySubscription) async throws -> sending Value |
| 79 | + ) async throws -> Value { |
| 80 | + try await self.psubscribe(to: patterns, process: process) |
| 81 | + } |
| 82 | + |
| 83 | + /// Subscribe to list of pattern matching channels and run closure with subscription |
| 84 | + /// |
| 85 | + /// When the closure is exited the patterns are automatically unsubscribed from. It is |
| 86 | + /// possible to have multiple subscriptions running on the same connection and unsubscribe |
| 87 | + /// commands will only be sent to Valkey when there are no subscriptions active for that |
| 88 | + /// pattern |
| 89 | + /// |
| 90 | + /// - Parameters: |
| 91 | + /// - patterns: list of channel patterns to subscribe to |
| 92 | + /// - isolation: Actor isolation |
| 93 | + /// - process: Closure that is called with subscription async sequence |
| 94 | + /// - Returns: Return value of closure |
| 95 | + @inlinable |
| 96 | + public func psubscribe<Value>( |
| 97 | + to patterns: [String], |
| 98 | + process: (ValkeySubscription) async throws -> sending Value |
| 99 | + ) async throws -> Value { |
| 100 | + try await self.subscribe( |
| 101 | + command: PSUBSCRIBE(pattern: patterns), |
| 102 | + filters: patterns.map { .pattern($0) }, |
| 103 | + process: process |
| 104 | + ) |
| 105 | + } |
| 106 | + |
| 107 | + /// Subscribe to list of shard channels and run closure with subscription |
| 108 | + /// |
| 109 | + /// When the closure is exited the shard channels are automatically unsubscribed from. It is |
| 110 | + /// possible to have multiple subscriptions running on the same connection and unsubscribe |
| 111 | + /// commands will only be sent to Valkey when there are no subscriptions active for that |
| 112 | + /// pattern |
| 113 | + /// |
| 114 | + /// - Parameters: |
| 115 | + /// - shardchannel: list of shard channels to subscribe to |
| 116 | + /// - isolation: Actor isolation |
| 117 | + /// - process: Closure that is called with subscription async sequence |
| 118 | + /// - Returns: Return value of closure |
| 119 | + @inlinable |
| 120 | + public func ssubscribe<Value>( |
| 121 | + to shardchannel: String..., |
| 122 | + process: (ValkeySubscription) async throws -> sending Value |
| 123 | + ) async throws -> Value { |
| 124 | + try await self.ssubscribe(to: shardchannel, process: process) |
| 125 | + } |
| 126 | + |
| 127 | + /// Subscribe to list of shard channels and run closure with subscription |
| 128 | + /// |
| 129 | + /// When the closure is exited the shard channels are automatically unsubscribed from. It is |
| 130 | + /// possible to have multiple subscriptions running on the same connection and unsubscribe |
| 131 | + /// commands will only be sent to Valkey when there are no subscriptions active for that |
| 132 | + /// pattern |
| 133 | + /// |
| 134 | + /// - Parameters: |
| 135 | + /// - shardchannel: list of shard channels to subscribe to |
| 136 | + /// - isolation: Actor isolation |
| 137 | + /// - process: Closure that is called with subscription async sequence |
| 138 | + /// - Returns: Return value of closure |
| 139 | + @inlinable |
| 140 | + public func ssubscribe<Value>( |
| 141 | + to shardchannel: [String], |
| 142 | + process: (ValkeySubscription) async throws -> sending Value |
| 143 | + ) async throws -> Value { |
| 144 | + try await self.subscribe( |
| 145 | + command: SSUBSCRIBE(shardchannel: shardchannel), |
| 146 | + filters: shardchannel.map { .shardChannel($0) }, |
| 147 | + process: process |
| 148 | + ) |
| 149 | + } |
| 150 | + |
| 151 | + /// Subscribe to key invalidation channel required for client-side caching |
| 152 | + /// |
| 153 | + /// See https://valkey.io/topics/client-side-caching/ for more details |
| 154 | + /// |
| 155 | + /// When the closure is exited the channel is automatically unsubscribed from. It is |
| 156 | + /// possible to have multiple subscriptions running on the same connection and unsubscribe |
| 157 | + /// commands will only be sent to Valkey when there are no subscriptions active for that |
| 158 | + /// channel |
| 159 | + /// |
| 160 | + /// - Parameters: |
| 161 | + /// - process: Closure that is called with async sequence of key invalidations |
| 162 | + /// - Returns: Return value of closure |
| 163 | + @inlinable |
| 164 | + public func subscribeKeyInvalidations<Value>( |
| 165 | + process: (AsyncMapSequence<ValkeySubscription, ValkeyKey>) async throws -> sending Value |
| 166 | + ) async throws -> Value { |
| 167 | + try await self.subscribe(to: [ValkeySubscriptions.invalidateChannel]) { subscription in |
| 168 | + let keys = subscription.map { ValkeyKey($0.message) } |
| 169 | + return try await process(keys) |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + @inlinable |
| 174 | + func subscribe<Value>( |
| 175 | + command: some ValkeyCommand, |
| 176 | + filters: [ValkeySubscriptionFilter], |
| 177 | + isolation: isolated (any Actor)? = #isolation, |
| 178 | + process: (ValkeySubscription) async throws -> sending Value |
| 179 | + ) async throws -> Value { |
| 180 | + try await withThrowingTaskGroup(of: Void.self, isolation: isolation) { group in |
| 181 | + let (stream, cont) = ValkeySubscription.makeStream() |
| 182 | + group.addTask { |
| 183 | + while true { |
| 184 | + do { |
| 185 | + try Task.checkCancellation() |
| 186 | + return try await self.withConnection { connection in |
| 187 | + try await connection.subscribe(command: command, filters: filters) { subscription in |
| 188 | + for try await message in subscription { |
| 189 | + cont.yield(message) |
| 190 | + } |
| 191 | + } |
| 192 | + cont.finish() |
| 193 | + } |
| 194 | + } catch let error as ValkeyClientError { |
| 195 | + switch error.errorCode { |
| 196 | + case .connectionClosed, .connectionClosedDueToCancellation, .connectionClosing: |
| 197 | + break |
| 198 | + default: |
| 199 | + cont.finish(throwing: error) |
| 200 | + return |
| 201 | + } |
| 202 | + } catch { |
| 203 | + cont.finish(throwing: error) |
| 204 | + return |
| 205 | + } |
| 206 | + } |
| 207 | + } |
| 208 | + let value = try await process(stream) |
| 209 | + group.cancelAll() |
| 210 | + return value |
| 211 | + } |
| 212 | + } |
| 213 | +} |
0 commit comments