Skip to content

Commit 4906875

Browse files
committed
add configurable option for maxRetryAttempts
1 parent d4ad495 commit 4906875

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

Sources/Realtime/RealtimeChannelV2.swift

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ public final class RealtimeChannelV2: Sendable {
4040

4141
let logger: (any SupabaseLogger)?
4242
let socket: RealtimeClientV2
43-
let maxRetryAttempt = 5
4443

4544
@MainActor var joinRef: String? { mutableState.joinRef }
4645

@@ -86,7 +85,7 @@ public final class RealtimeChannelV2: Sendable {
8685

8786
/// Subscribes to the channel.
8887
public func subscribeWithError() async throws {
89-
logger?.debug("Starting subscription to channel '\(topic)' (attempt 1/\(maxRetryAttempt))")
88+
logger?.debug("Starting subscription to channel '\(topic)' (attempt 1/\(socket.options.maxRetryAttempts))")
9089

9190
status = .subscribing
9291

@@ -100,12 +99,12 @@ public final class RealtimeChannelV2: Sendable {
10099

101100
var attempts = 0
102101

103-
while attempts < maxRetryAttempt {
102+
while attempts < socket.options.maxRetryAttempts {
104103
attempts += 1
105104

106105
do {
107106
logger?.debug(
108-
"Attempting to subscribe to channel '\(topic)' (attempt \(attempts)/\(maxRetryAttempt))"
107+
"Attempting to subscribe to channel '\(topic)' (attempt \(attempts)/\(socket.options.maxRetryAttempts))"
109108
)
110109

111110
try await withTimeout(interval: socket.options.timeoutInterval) { [self] in
@@ -117,10 +116,10 @@ public final class RealtimeChannelV2: Sendable {
117116

118117
} catch is TimeoutError {
119118
logger?.debug(
120-
"Subscribe timed out for channel '\(topic)' (attempt \(attempts)/\(maxRetryAttempt))"
119+
"Subscribe timed out for channel '\(topic)' (attempt \(attempts)/\(socket.options.maxRetryAttempts))"
121120
)
122121

123-
if attempts < maxRetryAttempt {
122+
if attempts < socket.options.maxRetryAttempts {
124123
// Add exponential backoff with jitter
125124
let delay = calculateRetryDelay(for: attempts)
126125
logger?.debug(
@@ -136,7 +135,7 @@ public final class RealtimeChannelV2: Sendable {
136135
}
137136
} else {
138137
logger?.error(
139-
"Failed to subscribe to channel '\(topic)' after \(maxRetryAttempt) attempts due to timeout"
138+
"Failed to subscribe to channel '\(topic)' after \(socket.options.maxRetryAttempts) attempts due to timeout"
140139
)
141140
}
142141
} catch is CancellationError {

Sources/Realtime/Types.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public struct RealtimeClientOptions: Sendable {
2020
var timeoutInterval: TimeInterval
2121
var disconnectOnSessionLoss: Bool
2222
var connectOnSubscribe: Bool
23+
var maxRetryAttempts: Int
2324

2425
/// Sets the log level for Realtime
2526
var logLevel: LogLevel?
@@ -32,6 +33,7 @@ public struct RealtimeClientOptions: Sendable {
3233
public static let defaultTimeoutInterval: TimeInterval = 10
3334
public static let defaultDisconnectOnSessionLoss = true
3435
public static let defaultConnectOnSubscribe: Bool = true
36+
public static let defaultMaxRetryAttempts: Int = 5
3537

3638
public init(
3739
headers: [String: String] = [:],
@@ -40,6 +42,7 @@ public struct RealtimeClientOptions: Sendable {
4042
timeoutInterval: TimeInterval = Self.defaultTimeoutInterval,
4143
disconnectOnSessionLoss: Bool = Self.defaultDisconnectOnSessionLoss,
4244
connectOnSubscribe: Bool = Self.defaultConnectOnSubscribe,
45+
maxRetryAttempts: Int = Self.defaultMaxRetryAttempts,
4346
logLevel: LogLevel? = nil,
4447
fetch: (@Sendable (_ request: URLRequest) async throws -> (Data, URLResponse))? = nil,
4548
accessToken: (@Sendable () async throws -> String?)? = nil,
@@ -51,6 +54,7 @@ public struct RealtimeClientOptions: Sendable {
5154
self.timeoutInterval = timeoutInterval
5255
self.disconnectOnSessionLoss = disconnectOnSessionLoss
5356
self.connectOnSubscribe = connectOnSubscribe
57+
self.maxRetryAttempts = maxRetryAttempts
5458
self.logLevel = logLevel
5559
self.fetch = fetch
5660
self.accessToken = accessToken

0 commit comments

Comments
 (0)