commit 38bfe8d76b399789a7030b03bdb9e24e8625697f parent 300b5fda9827a797c5f4a57f73c4d33b93fae09e Author: William Casarin <jb55@jb55.com> Date: Sun, 16 Oct 2022 11:09:46 -0700 Add the ability to mark relays as broken not currently used Signed-off-by: William Casarin <jb55@jb55.com> Diffstat:
M | damus/Nostr/Relay.swift | | | 25 | +++++++++++++++++++++++-- |
M | damus/Nostr/RelayPool.swift | | | 14 | ++++++++++++-- |
2 files changed, 35 insertions(+), 4 deletions(-)
diff --git a/damus/Nostr/Relay.swift b/damus/Nostr/Relay.swift @@ -19,10 +19,31 @@ struct RelayDescriptor: Codable { let info: RelayInfo } -struct Relay: Identifiable { +enum RelayFlags: Int { + case none = 0 + case broken = 1 +} + +class Relay: Identifiable { let descriptor: RelayDescriptor let connection: RelayConnection - + + var flags: Int + + init(descriptor: RelayDescriptor, connection: RelayConnection) { + self.flags = 0 + self.descriptor = descriptor + self.connection = connection + } + + func mark_broken() { + flags |= RelayFlags.broken.rawValue + } + + var is_broken: Bool { + return (flags & RelayFlags.broken.rawValue) == RelayFlags.broken.rawValue + } + var id: String { return get_relay_id(descriptor.url) } diff --git a/damus/Nostr/RelayPool.swift b/damus/Nostr/RelayPool.swift @@ -80,18 +80,28 @@ class RelayPool { /// This is used to retry dead connections func connect_to_disconnected() { for relay in relays { - if !relay.connection.isConnected && !relay.connection.isConnecting { - relay.connection.connect() + let c = relay.connection + if relay.is_broken || c.isReconnecting || c.isConnecting || c.isConnected { + continue } + + relay.connection.reconnect() } } func reconnect(to: [String]? = nil) { let relays = to.map{ get_relays($0) } ?? self.relays for relay in relays { + // don't try to reconnect to broken relays relay.connection.reconnect() } } + + func mark_broken(_ relay_id: String) { + for relay in relays { + relay.mark_broken() + } + } func connect(to: [String]? = nil) { let relays = to.map{ get_relays($0) } ?? self.relays