@@ -258,9 +258,16 @@ public final class ValkeyClusterClient: Sendable {
258258 _ commands: [ any ValkeyCommand ]
259259 ) async -> [ Result < RESPToken , any Error > ] {
260260 guard commands. count > 0 else { return [ ] }
261+ let readOnlyCommand = commands. reduce ( true ) { $0 && $1. isReadOnly }
262+ let nodeSelection =
263+ if readOnlyCommand {
264+ self . clientConfiguration. readOnlyReplicaSelection. clusterNodeSelection
265+ } else {
266+ ValkeyClusterNodeSelection . primary
267+ }
261268 // get a list of nodes and the commands that should be run on them
262269 do {
263- let nodes = try await self . splitCommandsAcrossNodes ( commands: commands)
270+ let nodes = try await self . splitCommandsAcrossNodes ( commands: commands, nodeSelection : nodeSelection )
264271 // if this list has one element, then just run the pipeline on that single node
265272 if nodes. count == 1 {
266273 do {
@@ -570,7 +577,10 @@ public final class ValkeyClusterClient: Sendable {
570577 /// These array of indices are then used to create collections of commands to
571578 /// run on each node
572579 @usableFromInline
573- func splitCommandsAcrossNodes( commands: [ any ValkeyCommand ] ) async throws -> [ ValkeyServerAddress : NodeAndCommands ] . Values {
580+ func splitCommandsAcrossNodes(
581+ commands: [ any ValkeyCommand ] ,
582+ nodeSelection: ValkeyClusterNodeSelection
583+ ) async throws -> [ ValkeyServerAddress : NodeAndCommands ] . Values {
574584 var nodeMap : [ ValkeyServerAddress : NodeAndCommands ] = [ : ]
575585 var index = commands. startIndex
576586 var prevAddress : ValkeyServerAddress ? = nil
@@ -583,7 +593,7 @@ public final class ValkeyClusterClient: Sendable {
583593 // Get hash slot for key and add all the commands you have iterated through so far to the
584594 // node associated with that key and break out of loop
585595 let hashSlot = try self . hashSlot ( for: keysAffected)
586- let node = try await self . nodeClient ( for: hashSlot. map { [ $0] } ?? [ ] , nodeSelection: . primary )
596+ let node = try await self . nodeClient ( for: hashSlot. map { [ $0] } ?? [ ] , nodeSelection: nodeSelection )
587597 let address = node. serverAddress
588598 let nodeAndCommands = NodeAndCommands ( node: node, commandIndices: . init( commands. startIndex..< index) )
589599 nodeMap [ address] = nodeAndCommands
@@ -599,7 +609,7 @@ public final class ValkeyClusterClient: Sendable {
599609 if keysAffected. count > 0 {
600610 // If command affects a key get hash slot for key and add command to the node associated with that key
601611 let hashSlot = try self . hashSlot ( for: keysAffected)
602- let node = try await self . nodeClient ( for: hashSlot. map { [ $0] } ?? [ ] , nodeSelection: . primary )
612+ let node = try await self . nodeClient ( for: hashSlot. map { [ $0] } ?? [ ] , nodeSelection: nodeSelection )
603613 prevAddress = node. serverAddress
604614 nodeMap [ prevAddress, default: . init( node: node, commandIndices: [ ] ) ] . commandIndices. append ( index)
605615 } else {
@@ -610,7 +620,7 @@ public final class ValkeyClusterClient: Sendable {
610620 }
611621 } else {
612622 // if none of the commands affect any keys then choose a random node
613- let node = try await self . nodeClient ( for: [ ] , nodeSelection: . primary )
623+ let node = try await self . nodeClient ( for: [ ] , nodeSelection: nodeSelection )
614624 let address = node. serverAddress
615625 let nodeAndCommands = NodeAndCommands ( node: node, commandIndices: . init( commands. startIndex..< index) )
616626 nodeMap [ address] = nodeAndCommands
0 commit comments