Skip to content

Commit 03fe57a

Browse files
committed
Add transaction(_:) to client protocol
Signed-off-by: Adam Fowler <[email protected]>
1 parent cf72fbb commit 03fe57a

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

Sources/Valkey/Connection/ValkeyConnection.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ public final actor ValkeyConnection: ValkeyClientProtocol, Sendable {
449449
func transactionWithAsk(
450450
_ commands: some Collection<any ValkeyCommand>
451451
) async throws -> [Result<RESPToken, any Error>] {
452-
// this currently allocates a promise for every command. We could collapse this down to one promise
452+
self.logger.trace("transaction asking", metadata: ["commands": .string(Self.concatenateCommandNames(commands))])
453453
var promises: [EventLoopPromise<RESPToken>] = []
454454
promises.reserveCapacity(commands.count)
455455
var valkeyPromises: [ValkeyPromise<RESPToken>] = []

Sources/Valkey/ValkeyClientProtocol.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,24 @@ public protocol ValkeyClientProtocol: Sendable {
2828
/// - Returns: Array holding the RESPToken responses of all the commands
2929
func execute(_ commands: [any ValkeyCommand]) async -> [Result<RESPToken, any Error>]
3030

31+
/// Pipeline a series of commands as a transaction to Valkey connection
32+
///
33+
/// Another client will never be served in the middle of the execution of these
34+
/// commands. See https://valkey.io/topics/transactions/ for more information.
35+
///
36+
/// EXEC and MULTI commands are added to the pipelined commands and the output
37+
/// of the EXEC command is transformed into an array of RESPToken Results, one for
38+
/// each command.
39+
///
40+
/// This is an alternative version of the transaction function ``ValkeyConnection/transaction(_:)->(_,_)``
41+
/// that allows for a collection of ValkeyCommands. It provides more flexibility but the command
42+
/// responses are returned as ``RESPToken`` instead of the response type for the command.
43+
///
44+
/// - Parameter commands: Collection of ValkeyCommands
45+
/// - Returns: Array holding the RESPToken responses of all the commands
46+
/// - Throws: ValkeyTransactionError when EXEC aborts
47+
func transaction(_ commands: [any ValkeyCommand]) async throws -> [Result<RESPToken, any Error>]
48+
3149
/// Execute subscribe command and run closure using related ``ValkeySubscription``
3250
/// AsyncSequence
3351
///

0 commit comments

Comments
 (0)