commit 80ea37a102cdb9cd29a9fafd77d12251a29dabb2
parent 91fc0039eb44c11954775ae83e3e7b8f9c5a2e0f
Author: William Casarin <jb55@jb55.com>
Date: Wed, 3 May 2023 06:36:33 -0700
Don't spam reconnects on error
Diffstat:
1 file changed, 10 insertions(+), 1 deletion(-)
diff --git a/damus/Nostr/RelayConnection.swift b/damus/Nostr/RelayConnection.swift
@@ -42,6 +42,7 @@ final class RelayConnection {
private(set) var isConnecting = false
private(set) var last_connection_attempt: TimeInterval = 0
+ private(set) var backoff: TimeInterval = 1.0
private lazy var socket = WebSocket(url.url)
private var subscriptionToken: AnyCancellable?
@@ -96,6 +97,7 @@ final class RelayConnection {
private func receive(event: WebSocketEvent) {
switch event {
case .connected:
+ backoff = 1.0
self.isConnected = true
self.isConnecting = false
case .message(let message):
@@ -111,7 +113,8 @@ final class RelayConnection {
print("⚠️ Warning: RelayConnection (\(self.url)) error: \(error)")
isConnected = false
isConnecting = false
- reconnect()
+ backoff *= 1.5
+ reconnect_in(after: backoff)
}
self.handleEvent(.ws_event(event))
}
@@ -124,6 +127,12 @@ final class RelayConnection {
connect()
}
+ func reconnect_in(after: TimeInterval) {
+ DispatchQueue.main.asyncAfter(deadline: .now() + after) {
+ self.reconnect()
+ }
+ }
+
private func receive(message: URLSessionWebSocketTask.Message) {
switch message {
case .string(let messageString):