From a740fe8ca896e7cfcbbf3dbb14a82eb8f9db8835 Mon Sep 17 00:00:00 2001 From: Thomas Ricouard Date: Fri, 23 Jun 2023 07:38:21 +0200 Subject: [PATCH] Streaming: exponential backoff on reconnect #1438 --- Packages/Env/Sources/Env/StreamWatcher.swift | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Packages/Env/Sources/Env/StreamWatcher.swift b/Packages/Env/Sources/Env/StreamWatcher.swift index c5fedefb..f87d305e 100644 --- a/Packages/Env/Sources/Env/StreamWatcher.swift +++ b/Packages/Env/Sources/Env/StreamWatcher.swift @@ -12,6 +12,8 @@ public class StreamWatcher: ObservableObject { private let decoder = JSONDecoder() private let encoder = JSONEncoder() + + private var retryDelay: Int = 10 public enum Stream: String { case publicTimeline = "public" @@ -22,6 +24,7 @@ public class StreamWatcher: ObservableObject { @Published public var events: [any StreamEvent] = [] @Published public var unreadNotificationsCount: Int = 0 @Published public var latestEvent: (any StreamEvent)? + public init() { decoder.keyDecodingStrategy = .convertFromSnakeCase @@ -69,7 +72,8 @@ public class StreamWatcher: ObservableObject { } private func receiveMessage() { - task?.receive(completionHandler: { result in + task?.receive(completionHandler: { [weak self] result in + guard let self = self else { return } switch result { case let .success(message): switch message { @@ -100,8 +104,8 @@ public class StreamWatcher: ObservableObject { self.receiveMessage() case .failure: - DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(10)) { [weak self] in - guard let self = self else { return } + DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(self.retryDelay)) { + self.retryDelay += 30 self.stopWatching() self.connect() self.watch(streams: self.watchedStreams)