@@ -64,8 +64,8 @@ public final class ValkeyClusterClient: Sendable {
64
64
65
65
@usableFromInline
66
66
typealias StateMachine = ValkeyClusterClientStateMachine <
67
- ValkeyClient ,
68
- ValkeyClientFactory ,
67
+ ValkeyNodeClient ,
68
+ ValkeyNodeClientFactory ,
69
69
ContinuousClock ,
70
70
CheckedContinuation < Void , any Error > ,
71
71
AsyncStream < Void > . Continuation
@@ -82,7 +82,7 @@ public final class ValkeyClusterClient: Sendable {
82
82
83
83
private enum RunAction {
84
84
case runClusterDiscovery( runNodeDiscovery: Bool )
85
- case runClient( ValkeyClient )
85
+ case runClient( ValkeyNodeClient )
86
86
case runTimer( ValkeyClusterTimer )
87
87
}
88
88
@@ -113,7 +113,7 @@ public final class ValkeyClusterClient: Sendable {
113
113
self . actionStream = stream
114
114
self . actionStreamContinuation = continuation
115
115
116
- let factory = ValkeyClientFactory (
116
+ let factory = ValkeyNodeClientFactory (
117
117
logger: logger,
118
118
configuration: clientConfiguration,
119
119
connectionFactory: ValkeyConnectionFactory (
@@ -153,8 +153,8 @@ public final class ValkeyClusterClient: Sendable {
153
153
@inlinable
154
154
public func send< Command: ValkeyCommand > ( command: Command ) async throws -> Command . Response {
155
155
let hashSlots = command. keysAffected. map { HashSlot ( key: $0) }
156
- var clientSelector : ( ) async throws -> ValkeyClient = {
157
- try await self . client ( for: hashSlots)
156
+ var clientSelector : ( ) async throws -> ValkeyNodeClient = {
157
+ try await self . nodeClient ( for: hashSlots)
158
158
}
159
159
160
160
while !Task. isCancelled {
@@ -168,7 +168,7 @@ public final class ValkeyClusterClient: Sendable {
168
168
throw error
169
169
}
170
170
self . logger. trace ( " Received move error " , metadata: [ " error " : " \( movedError) " ] )
171
- clientSelector = { try await self . client ( for: movedError) }
171
+ clientSelector = { try await self . nodeClient ( for: movedError) }
172
172
}
173
173
}
174
174
throw CancellationError ( )
@@ -188,8 +188,8 @@ public final class ValkeyClusterClient: Sendable {
188
188
operation: ( ValkeyConnection ) async throws -> sending Value
189
189
) async throws -> Value {
190
190
let hashSlots = keys. map { HashSlot ( key: $0) }
191
- let client = try await self . client ( for: hashSlots)
192
- return try await client . withConnection ( isolation: isolation, operation: operation)
191
+ let node = try await self . nodeClient ( for: hashSlots)
192
+ return try await node . withConnection ( isolation: isolation, operation: operation)
193
193
}
194
194
195
195
/// Starts running the cluster client.
@@ -361,13 +361,13 @@ public final class ValkeyClusterClient: Sendable {
361
361
/// MOVED responses from Valkey nodes.
362
362
///
363
363
/// - Parameter moveError: The MOVED error response from a Valkey node.
364
- /// - Returns: A client connected to the node that can handle the request.
364
+ /// - Returns: A ``ValkeyNode`` connected to the node that can handle the request.
365
365
/// - Throws:
366
366
/// - `ValkeyClusterError.waitedForDiscoveryAfterMovedErrorThreeTimes` if unable to resolve
367
367
/// the MOVED error after multiple attempts
368
368
/// - `ValkeyClusterError.clientRequestCancelled` if the request is cancelled
369
369
@usableFromInline
370
- /* private */ func client ( for moveError: ValkeyMovedError ) async throws -> ValkeyClient {
370
+ /* private */ func nodeClient ( for moveError: ValkeyMovedError ) async throws -> ValkeyNodeClient {
371
371
var counter = 0
372
372
while counter < 3 {
373
373
defer { counter += 1 }
@@ -376,8 +376,8 @@ public final class ValkeyClusterClient: Sendable {
376
376
}
377
377
378
378
switch action {
379
- case . connectionPool( let client ) :
380
- return client
379
+ case . connectionPool( let node ) :
380
+ return node
381
381
382
382
case . waitForDiscovery:
383
383
break
@@ -414,25 +414,25 @@ public final class ValkeyClusterClient: Sendable {
414
414
throw ValkeyClusterError . waitedForDiscoveryAfterMovedErrorThreeTimes
415
415
}
416
416
417
- /// Retrieves a client for communicating with nodes that manage the given hash slots.
417
+ /// Retrieves a ``ValkeyNode`` for communicating with nodes that manage the given hash slots.
418
418
///
419
419
/// This is a lower-level method that can be used when you need direct access to a
420
- /// specific `ValkeyClient ` instance for nodes managing particular hash slots. Most users
420
+ /// specific `ValkeyNode ` instance for nodes managing particular hash slots. Most users
421
421
/// should prefer the higher-level `send(command:)` method.
422
422
///
423
423
/// - Parameter slots: The collection of hash slots to determine which node to connect to.
424
- /// - Returns: A `ValkeyClient ` instance connected to the appropriate node.
424
+ /// - Returns: A `ValkeyNode ` instance connected to the appropriate node.
425
425
/// - Throws:
426
426
/// - `ValkeyClusterError.clusterIsUnavailable` if no healthy nodes are available
427
427
/// - `ValkeyClusterError.clusterIsMissingSlotAssignment` if the slot assignment cannot be determined
428
428
@inlinable
429
- func client ( for slots: some ( Collection < HashSlot > & Sendable ) ) async throws -> ValkeyClient {
429
+ func nodeClient ( for slots: some ( Collection < HashSlot > & Sendable ) ) async throws -> ValkeyNodeClient {
430
430
var retries = 0
431
431
while retries < 3 {
432
432
defer { retries += 1 }
433
433
434
434
do {
435
- return try self . stateLock. withLock { state -> ValkeyClient in
435
+ return try self . stateLock. withLock { state -> ValkeyNodeClient in
436
436
try state. poolFastPath ( for: slots)
437
437
}
438
438
} catch ValkeyClusterError . clusterIsUnavailable {
@@ -539,7 +539,7 @@ public final class ValkeyClusterClient: Sendable {
539
539
///
540
540
/// - Returns: A list of voters that can participate in cluster topology election.
541
541
/// - Throws: Any error encountered during node discovery.
542
- private func runNodeDiscovery( ) async throws -> [ ValkeyClusterVoter < ValkeyClient > ] {
542
+ private func runNodeDiscovery( ) async throws -> [ ValkeyClusterVoter < ValkeyNodeClient > ] {
543
543
do {
544
544
self . logger. trace ( " Running node discovery " )
545
545
let nodes = try await self . nodeDiscovery. lookupNodes ( )
@@ -577,11 +577,11 @@ public final class ValkeyClusterClient: Sendable {
577
577
/// - Parameter voters: The list of nodes that can vote on cluster topology.
578
578
/// - Returns: The agreed-upon cluster description.
579
579
/// - Throws: `ValkeyClusterError.clusterIsUnavailable` if consensus cannot be reached.
580
- private func runClusterDiscoveryFindingConsensus( voters: [ ValkeyClusterVoter < ValkeyClient > ] ) async throws -> ValkeyClusterDescription {
580
+ private func runClusterDiscoveryFindingConsensus( voters: [ ValkeyClusterVoter < ValkeyNodeClient > ] ) async throws -> ValkeyClusterDescription {
581
581
try await withThrowingTaskGroup ( of: ( ValkeyClusterDescription, ValkeyNodeID) . self) { taskGroup in
582
582
for voter in voters {
583
583
taskGroup. addTask {
584
- ( try await voter. client. clusterShards ( ) , voter. nodeID)
584
+ ( try await voter. client. send ( command : CLUSTER . SHARDS ( ) ) , voter. nodeID)
585
585
}
586
586
}
587
587
@@ -625,7 +625,7 @@ public final class ValkeyClusterClient: Sendable {
625
625
626
626
for voter in actions. voters {
627
627
taskGroup. addTask {
628
- ( try await voter. client. clusterShards ( ) , voter. nodeID)
628
+ ( try await voter. client. send ( command : CLUSTER . SHARDS ( ) ) , voter. nodeID)
629
629
}
630
630
}
631
631
@@ -650,80 +650,3 @@ public final class ValkeyClusterClient: Sendable {
650
650
/// This allows the cluster client to be used anywhere a `ValkeyClientProtocol` is expected.
651
651
@available ( valkeySwift 1 . 0 , * )
652
652
extension ValkeyClusterClient : ValkeyClientProtocol { }
653
-
654
- /// Extension that makes ``ValkeyClient`` conform to ``ValkeyNodeConnectionPool``.
655
- ///
656
- /// This enables the ``ValkeyClusterClient`` to manage individual ``ValkeyClient`` instances.
657
- @available ( valkeySwift 1 . 0 , * )
658
- extension ValkeyClient : ValkeyNodeConnectionPool {
659
- /// Initiates a graceful shutdown of the client.
660
- ///
661
- /// This method attempts to cleanly shut down the client's connections.
662
- /// If not implemented, it falls back to force shutdown.
663
- @usableFromInline
664
- package func triggerGracefulShutdown( ) {
665
- // TODO: Implement graceful shutdown
666
- self . triggerForceShutdown ( )
667
- }
668
- }
669
-
670
- /// A factory for creating ``ValkeyClient`` instances to connect to specific nodes.
671
- ///
672
- /// This factory is used by the ``ValkeyClusterClient`` to create client instances
673
- /// for each node in the cluster as needed.
674
- @available ( valkeySwift 1 . 0 , * )
675
- @usableFromInline
676
- package struct ValkeyClientFactory : ValkeyNodeConnectionPoolFactory {
677
- @usableFromInline
678
- package typealias ConnectionPool = ValkeyClient
679
-
680
- var logger : Logger
681
- var configuration : ValkeyClientConfiguration
682
- var eventLoopGroup : any EventLoopGroup
683
- let connectionIDGenerator = ConnectionIDGenerator ( )
684
- let connectionFactory : ValkeyConnectionFactory
685
-
686
- /// Creates a new `ValkeyClientFactory` instance.
687
- ///
688
- /// - Parameters:
689
- /// - logger: The logger used for diagnostic information.
690
- /// - configuration: Configuration for the Valkey clients created by this factory.
691
- /// - eventLoopGroup: The event loop group to use for client connections.
692
- package init (
693
- logger: Logger ,
694
- configuration: ValkeyClientConfiguration ,
695
- connectionFactory: ValkeyConnectionFactory ,
696
- eventLoopGroup: any EventLoopGroup
697
- ) {
698
- self . logger = logger
699
- self . configuration = configuration
700
- self . connectionFactory = connectionFactory
701
- self . eventLoopGroup = eventLoopGroup
702
- }
703
-
704
- /// Creates a connection pool (client) for a specific node in the cluster.
705
- ///
706
- /// - Parameter nodeDescription: Description of the node to connect to.
707
- /// - Returns: A configured `ValkeyClient` instance ready to connect to the specified node.
708
- @usableFromInline
709
- package func makeConnectionPool( nodeDescription: ValkeyNodeDescription ) -> ValkeyClient {
710
- let serverAddress = ValkeyServerAddress . hostname (
711
- nodeDescription. endpoint,
712
- port: nodeDescription. port
713
- )
714
-
715
- var clientConfiguration = self . configuration
716
- if !nodeDescription. useTLS {
717
- // TODO: Should this throw? What about the other way around?
718
- clientConfiguration. tls = . disable
719
- }
720
-
721
- return ValkeyClient (
722
- serverAddress,
723
- connectionIDGenerator: self . connectionIDGenerator,
724
- connectionFactory: self . connectionFactory,
725
- eventLoopGroup: self . eventLoopGroup,
726
- logger: self . logger
727
- )
728
- }
729
- }
0 commit comments