mirror of
https://github.com/Dimillian/IceCubesApp.git
synced 2024-12-24 14:02:53 +01:00
Streaming: exponential backoff on reconnect #1438
This commit is contained in:
parent
831ff08082
commit
a740fe8ca8
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user