|
| 1 | +// |
| 2 | +// SubscriptionStateMachine.swift |
| 3 | +// Supabase |
| 4 | +// |
| 5 | +// Created by Guilherme Souza on 18/11/25. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | +import Helpers |
| 10 | + |
| 11 | +actor SubscriptionStateMachine { |
| 12 | + /// Represents the possible states of a channel subscription |
| 13 | + enum State: Sendable { |
| 14 | + case unsubscribed |
| 15 | + case subscribing(Task<Void, any Error>) |
| 16 | + case subscribed(joinRef: String) |
| 17 | + case unsubscribing(Task<Void, Never>) |
| 18 | + } |
| 19 | + |
| 20 | + // MARK: - Properties |
| 21 | + |
| 22 | + private(set) var state: State = .unsubscribed |
| 23 | + |
| 24 | + private let topic: String |
| 25 | + private let maxRetryAttempts: Int |
| 26 | + private let timeoutInterval: TimeInterval |
| 27 | + private let logger: (any SupabaseLogger)? |
| 28 | + |
| 29 | + // MARK: - Initialization |
| 30 | + |
| 31 | + init( |
| 32 | + topic: String, |
| 33 | + maxRetryAttempts: Int, |
| 34 | + timeoutInterval: TimeInterval, |
| 35 | + logger: (any SupabaseLogger)? |
| 36 | + ) { |
| 37 | + self.topic = topic |
| 38 | + self.maxRetryAttempts = maxRetryAttempts |
| 39 | + self.timeoutInterval = timeoutInterval |
| 40 | + self.logger = logger |
| 41 | + } |
| 42 | + |
| 43 | + // MARK: - Public API |
| 44 | + |
| 45 | + /// Subscribe to the channel. Returns immediately if already subscribed. |
| 46 | + /// |
| 47 | + /// This method is safe to call multiple times - it will reuse an existing subscription |
| 48 | + /// or wait for an in-progress subscription attempt to complete. |
| 49 | + /// |
| 50 | + /// - Parameter performSubscription: Closure that performs the actual subscription |
| 51 | + /// - Returns: The join reference for the subscription |
| 52 | + /// - Throws: Subscription errors or timeout |
| 53 | + func subscribe( |
| 54 | + performSubscription: @escaping @Sendable () async throws -> String |
| 55 | + ) async throws -> String { |
| 56 | + switch state { |
| 57 | + case .subscribed(let joinRef): |
| 58 | + logger?.debug("Already subscribed to channel '\(topic)'") |
| 59 | + return joinRef |
| 60 | + |
| 61 | + case .subscribing(let task): |
| 62 | + logger?.debug("Subscription already in progress for '\(topic)', waiting...") |
| 63 | + try await task.value |
| 64 | + // Recursively call to get the join ref after task completes |
| 65 | + return try await subscribe(performSubscription: performSubscription) |
| 66 | + |
| 67 | + case .unsubscribing(let task): |
| 68 | + logger?.debug("Unsubscription in progress for '\(topic)', waiting...") |
| 69 | + await task.value |
| 70 | + return try await subscribe(performSubscription: performSubscription) |
| 71 | + |
| 72 | + case .unsubscribed: |
| 73 | + logger?.debug("Initiating subscription to channel '\(topic)'") |
| 74 | + return try await performSubscriptionWithRetry(performSubscription: performSubscription) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /// Unsubscribe from the channel. |
| 79 | + /// |
| 80 | + /// - Parameter performUnsubscription: Closure that performs the actual unsubscription |
| 81 | + func unsubscribe( |
| 82 | + performUnsubscription: @escaping @Sendable () async -> Void |
| 83 | + ) async { |
| 84 | + switch state { |
| 85 | + case .unsubscribed: |
| 86 | + logger?.debug("Already unsubscribed from channel '\(topic)'") |
| 87 | + return |
| 88 | + |
| 89 | + case .unsubscribing(let task): |
| 90 | + logger?.debug("Unsubscription already in progress for '\(topic)', waiting...") |
| 91 | + await task.value |
| 92 | + return |
| 93 | + |
| 94 | + case .subscribing(let task): |
| 95 | + logger?.debug("Cancelling subscription attempt for '\(topic)'") |
| 96 | + task.cancel() |
| 97 | + state = .unsubscribed |
| 98 | + return |
| 99 | + |
| 100 | + case .subscribed: |
| 101 | + logger?.debug("Unsubscribing from channel '\(topic)'") |
| 102 | + let unsubscribeTask = Task<Void, Never> { |
| 103 | + await performUnsubscription() |
| 104 | + state = .unsubscribed |
| 105 | + } |
| 106 | + |
| 107 | + state = .unsubscribing(unsubscribeTask) |
| 108 | + await unsubscribeTask.value |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + /// Mark the subscription as successfully subscribed. |
| 113 | + /// |
| 114 | + /// - Parameter joinRef: The join reference received from the server |
| 115 | + func didSubscribe(joinRef: String) { |
| 116 | + guard case .subscribing = state else { |
| 117 | + logger?.debug("Ignoring didSubscribe in non-subscribing state") |
| 118 | + return |
| 119 | + } |
| 120 | + |
| 121 | + logger?.debug("Successfully subscribed to channel '\(topic)'") |
| 122 | + state = .subscribed(joinRef: joinRef) |
| 123 | + } |
| 124 | + |
| 125 | + /// Handle subscription error. |
| 126 | + func handleError(_ error: any Error) { |
| 127 | + guard case .subscribing = state else { |
| 128 | + logger?.debug("Ignoring subscription error in non-subscribing state: \(error)") |
| 129 | + return |
| 130 | + } |
| 131 | + |
| 132 | + logger?.error("Subscription error for channel '\(topic)': \(error.localizedDescription)") |
| 133 | + state = .unsubscribed |
| 134 | + } |
| 135 | + |
| 136 | + /// Get current join reference if subscribed, nil otherwise. |
| 137 | + var joinRef: String? { |
| 138 | + if case .subscribed(let joinRef) = state { |
| 139 | + return joinRef |
| 140 | + } |
| 141 | + return nil |
| 142 | + } |
| 143 | + |
| 144 | + /// Check if currently subscribed. |
| 145 | + var isSubscribed: Bool { |
| 146 | + if case .subscribed = state { |
| 147 | + return true |
| 148 | + } |
| 149 | + return false |
| 150 | + } |
| 151 | + |
| 152 | + /// Check if currently subscribing. |
| 153 | + var isSubscribing: Bool { |
| 154 | + if case .subscribing = state { |
| 155 | + return true |
| 156 | + } |
| 157 | + return false |
| 158 | + } |
| 159 | + |
| 160 | + // MARK: - Private Helpers |
| 161 | + |
| 162 | + private func performSubscriptionWithRetry( |
| 163 | + performSubscription: @escaping @Sendable () async throws -> String |
| 164 | + ) async throws -> String { |
| 165 | + logger?.debug( |
| 166 | + "Starting subscription to channel '\(topic)' (max attempts: \(maxRetryAttempts))" |
| 167 | + ) |
| 168 | + |
| 169 | + let subscriptionTask = Task<Void, any Error> { |
| 170 | + var attempts = 0 |
| 171 | + |
| 172 | + while attempts < maxRetryAttempts { |
| 173 | + attempts += 1 |
| 174 | + |
| 175 | + do { |
| 176 | + logger?.debug( |
| 177 | + "Attempting to subscribe to channel '\(topic)' (attempt \(attempts)/\(maxRetryAttempts))" |
| 178 | + ) |
| 179 | + |
| 180 | + let joinRef: String? = try await withTimeout(interval: timeoutInterval) { |
| 181 | + do { |
| 182 | + return try await performSubscription() |
| 183 | + } catch { |
| 184 | + self.logger?.error("Error when perfoming subscription: \(error.localizedDescription)") |
| 185 | + return nil |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + if let joinRef { |
| 190 | + state = .subscribed(joinRef: joinRef) |
| 191 | + logger?.debug("Successfully subscribed to channel '\(topic)'") |
| 192 | + } else { |
| 193 | + state = .unsubscribed |
| 194 | + logger?.error("Failed to subscribe to channel '\(topic)', no join ref received") |
| 195 | + } |
| 196 | + return |
| 197 | + |
| 198 | + } catch is TimeoutError { |
| 199 | + logger?.debug( |
| 200 | + "Subscribe timed out for channel '\(topic)' (attempt \(attempts)/\(maxRetryAttempts))" |
| 201 | + ) |
| 202 | + |
| 203 | + if attempts < maxRetryAttempts { |
| 204 | + let delay = calculateRetryDelay(for: attempts) |
| 205 | + logger?.debug( |
| 206 | + "Retrying subscription to channel '\(topic)' in \(String(format: "%.2f", delay)) seconds..." |
| 207 | + ) |
| 208 | + |
| 209 | + do { |
| 210 | + try await _clock.sleep(for: delay) |
| 211 | + |
| 212 | + if Task.isCancelled { |
| 213 | + logger?.debug("Subscription retry cancelled for channel '\(topic)'") |
| 214 | + throw CancellationError() |
| 215 | + } |
| 216 | + } catch { |
| 217 | + logger?.debug("Subscription retry cancelled for channel '\(topic)'") |
| 218 | + throw CancellationError() |
| 219 | + } |
| 220 | + } else { |
| 221 | + logger?.error( |
| 222 | + "Failed to subscribe to channel '\(topic)' after \(maxRetryAttempts) attempts due to timeout" |
| 223 | + ) |
| 224 | + } |
| 225 | + } catch is CancellationError { |
| 226 | + logger?.debug("Subscription cancelled for channel '\(topic)'") |
| 227 | + throw CancellationError() |
| 228 | + } catch { |
| 229 | + logger?.error("Subscription failed for channel '\(topic)': \(error.localizedDescription)") |
| 230 | + throw error |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + logger?.error("Subscription to channel '\(topic)' failed after \(attempts) attempts") |
| 235 | + throw RealtimeError.maxRetryAttemptsReached |
| 236 | + } |
| 237 | + |
| 238 | + state = .subscribing(subscriptionTask) |
| 239 | + |
| 240 | + do { |
| 241 | + try await subscriptionTask.value |
| 242 | + |
| 243 | + // Get the join ref that was just set |
| 244 | + guard case .subscribed(let joinRef) = state else { |
| 245 | + throw RealtimeError("Subscription succeeded but state is invalid") |
| 246 | + } |
| 247 | + |
| 248 | + return joinRef |
| 249 | + } catch { |
| 250 | + state = .unsubscribed |
| 251 | + throw error |
| 252 | + } |
| 253 | + } |
| 254 | + |
| 255 | + /// Calculates retry delay with exponential backoff and jitter |
| 256 | + private func calculateRetryDelay(for attempt: Int) -> TimeInterval { |
| 257 | + let baseDelay: TimeInterval = 1.0 |
| 258 | + let maxDelay: TimeInterval = 30.0 |
| 259 | + let backoffMultiplier: Double = 2.0 |
| 260 | + |
| 261 | + let exponentialDelay = baseDelay * pow(backoffMultiplier, Double(attempt - 1)) |
| 262 | + let cappedDelay = min(exponentialDelay, maxDelay) |
| 263 | + |
| 264 | + // Add jitter (±25% random variation) to prevent thundering herd |
| 265 | + let jitterRange = cappedDelay * 0.25 |
| 266 | + let jitter = Double.random(in: -jitterRange...jitterRange) |
| 267 | + |
| 268 | + return max(0.1, cappedDelay + jitter) |
| 269 | + } |
| 270 | +} |
0 commit comments