Skip to content

Commit d16658d

Browse files
committed
Fix up ValkeyClusterClient.transaction after merge
Signed-off-by: Adam Fowler <adamfowler71@gmail.com>
1 parent 83c2cbd commit d16658d

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

Sources/Valkey/Cluster/ValkeyClusterClient.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,9 +353,15 @@ public final class ValkeyClusterClient: Sendable {
353353
_ commands: Commands
354354
) async throws -> [Result<RESPToken, Error>] where Commands.Element == any ValkeyCommand {
355355
let hashSlot = try self.hashSlot(for: commands.flatMap { $0.keysAffected })
356-
356+
let readOnlyCommand = commands.reduce(true) { $0 && $1.isReadOnly }
357+
let nodeSelection =
358+
if readOnlyCommand {
359+
self.clientConfiguration.readOnlyReplicaSelection.clusterNodeSelection
360+
} else {
361+
ValkeyClusterNodeSelection.primary
362+
}
357363
var clientSelector: () async throws -> ValkeyNodeClient = {
358-
try await self.nodeClient(for: hashSlot.map { [$0] } ?? [])
364+
try await self.nodeClient(for: hashSlot.map { [$0] } ?? [], nodeSelection: nodeSelection)
359365
}
360366

361367
var asking = false

Sources/Valkey/Cluster/ValkeyClusterNodeSelection.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ package enum ValkeyClusterNodeSelection: Sendable {
1111
case primary
1212
case cycleReplicas(Int)
1313

14+
/// Select node from node ids
15+
/// - Parameter nodeIDs: Primary and replica nodes
16+
/// - Returns: ID of selected node
1417
@usableFromInline
1518
func select(nodeIDs: ValkeyShardNodeIDs) -> ValkeyNodeID {
1619
switch self {
@@ -22,7 +25,10 @@ package enum ValkeyClusterNodeSelection: Sendable {
2225
}
2326
}
2427
}
28+
29+
@available(valkeySwift 1.0, *)
2530
extension ValkeyClientConfiguration.ReadOnlyReplicaSelection {
31+
/// Convert from read only replica selection to node selection
2632
@usableFromInline
2733
var clusterNodeSelection: ValkeyClusterNodeSelection {
2834
switch self.value {

0 commit comments

Comments
 (0)