From 6639c002ed8a2209cdd31c671ba6f4321d7a9bd6 Mon Sep 17 00:00:00 2001 From: William Casarin Date: Thu, 19 Sep 2024 12:41:31 -0700 Subject: [PATCH] relay: don't reconnect when we don't have to We are reconnecting multiple times for two separate reasons: 1. On a cancellation "error" which does not warrant a reconnect 2. In our reconnection backoff it doesn't check If we are already connecting or connected. We check this so we don't reconnect multiple times. This fixes many reconnection issues and makes Damus feel wayyy snappier. Signed-off-by: William Casarin --- damus/Nostr/RelayConnection.swift | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/damus/Nostr/RelayConnection.swift b/damus/Nostr/RelayConnection.swift index f8a2e3e2..1df94875 100644 --- a/damus/Nostr/RelayConnection.swift +++ b/damus/Nostr/RelayConnection.swift @@ -141,6 +141,10 @@ final class RelayConnection: ObservableObject { // ignore socket not connected? return } + if nserr.domain == NSURLErrorDomain && nserr.code == -999 { + // these aren't real error, it just means task was cancelled + return + } DispatchQueue.main.async { self.isConnected = false self.isConnecting = false @@ -157,14 +161,21 @@ final class RelayConnection: ObservableObject { } func reconnect_with_backoff() { - self.backoff *= 1.5 + self.backoff *= 2.0 self.reconnect_in(after: self.backoff) } func reconnect() { guard !isConnecting && !isDisabled else { + self.log?.add("Cancelling reconnect, already connecting") return // we're already trying to connect or we're disabled } + + guard !self.isConnected else { + self.log?.add("Cancelling reconnect, already connected") + return + } + disconnect() connect() log?.add("Reconnecting...")