9
9
import NIOSSL
10
10
import _ValkeyConnectionPool
11
11
12
+ #if canImport(Darwin)
13
+ import Darwin
14
+ #elseif canImport(Glibc)
15
+ import Glibc
16
+ #elseif canImport(Musl)
17
+ import Musl
18
+ #elseif canImport(WinSDK)
19
+ import WinSDK
20
+ #elseif canImport(Bionic)
21
+ import Bionic
22
+ #else
23
+ #error("Unsupported platform")
24
+ #endif
25
+
12
26
/// Configuration for the Valkey client.
13
27
@available ( valkeySwift 1 . 0 , * )
14
28
public struct ValkeyClientConfiguration : Sendable {
@@ -64,6 +78,43 @@ public struct ValkeyClientConfiguration: Sendable {
64
78
}
65
79
}
66
80
81
+ /// Retry parameters for when a client needs to retry a command
82
+ public struct RetryParameters : Sendable {
83
+ let exponentBase : Double
84
+ let factor : Double
85
+ let minWaitTime : Double
86
+ let maxWaitTime : Double
87
+
88
+ /// Initialize RetryParameters
89
+ /// - Parameters:
90
+ /// - exponentBase: Exponent base number
91
+ /// - factor: Duration to multiple exponent by get base wait value
92
+ /// - minWaitTime: Minimum wait time
93
+ /// - maxWaitTime: Maximum wait time
94
+ public init (
95
+ exponentBase: Double = 2 ,
96
+ factor: Duration = . milliseconds( 10.0 ) ,
97
+ minWaitTime: Duration = . seconds( 1.28 ) ,
98
+ maxWaitTime: Duration = . seconds( 655.36 )
99
+ ) {
100
+ self . exponentBase = exponentBase
101
+ self . factor = factor / . milliseconds( 1 )
102
+ self . minWaitTime = minWaitTime / . milliseconds( 1 )
103
+ self . maxWaitTime = maxWaitTime / . milliseconds( 1 )
104
+ }
105
+
106
+ /// Calculate wait time for retry number
107
+ ///
108
+ /// This code is a copy from the `RetryParam` type in cluster_clients.rs of valkey-glide,
109
+ @usableFromInline
110
+ func calculateWaitTime( retry: Int ) -> Duration {
111
+ let baseWait = pow ( self . exponentBase, Double ( retry) ) * self . factor
112
+ let clampedWait = max ( min ( baseWait, self . maxWaitTime) , self . minWaitTime)
113
+ let jitteredWait = Double . random ( in: minWaitTime... clampedWait)
114
+ return . milliseconds( jitteredWait)
115
+ }
116
+ }
117
+
67
118
/// The connection pool definition for Valkey connections.
68
119
public struct ConnectionPool : Hashable , Sendable {
69
120
/// The minimum number of connections to preserve in the pool.
@@ -108,6 +159,8 @@ public struct ValkeyClientConfiguration: Sendable {
108
159
public var connectionPool : ConnectionPool
109
160
/// The keep alive behavior for the connection.
110
161
public var keepAliveBehavior : KeepAliveBehavior
162
+ /// Retry parameters for when a client needs to retry a command
163
+ public var retryParameters : RetryParameters
111
164
/// The timeout the client uses to determine if a connection is considered dead.
112
165
///
113
166
/// The connection is considered dead if a response isn't received within this time.
@@ -130,20 +183,23 @@ public struct ValkeyClientConfiguration: Sendable {
130
183
/// - authentication: The authentication credentials.
131
184
/// - connectionPool: The connection pool configuration.
132
185
/// - keepAliveBehavior: The connection keep alive behavior.
186
+ /// - retryParameters: Retry parameters for when client returns an error that requires a retry
133
187
/// - commandTimeout: The timeout for a connection response.
134
188
/// - blockingCommandTimeout: The timeout for a blocking command response.
135
189
/// - tls: The TLS configuration.
136
190
public init (
137
191
authentication: Authentication ? = nil ,
138
192
connectionPool: ConnectionPool = . init( ) ,
139
193
keepAliveBehavior: KeepAliveBehavior = . init( ) ,
194
+ retryParameters: RetryParameters = . init( ) ,
140
195
commandTimeout: Duration = . seconds( 30 ) ,
141
196
blockingCommandTimeout: Duration = . seconds( 120 ) ,
142
197
tls: TLS = . disable
143
198
) {
144
199
self . authentication = authentication
145
200
self . connectionPool = connectionPool
146
201
self . keepAliveBehavior = keepAliveBehavior
202
+ self . retryParameters = retryParameters
147
203
self . commandTimeout = commandTimeout
148
204
self . blockingCommandTimeout = blockingCommandTimeout
149
205
self . tls = tls
0 commit comments