relays: keep in sync with user relay list

Still need to figure out how to ensure new bootstrap relays are added...

Changelog-Changed: Ensure contact relay list is kept in sync with internal relay pool
This commit is contained in:
William Casarin
2022-12-29 15:23:34 -08:00
parent 2867da61ca
commit 3824e95f29
8 changed files with 87 additions and 49 deletions

View File

@@ -28,12 +28,14 @@ class Relay: Identifiable {
let descriptor: RelayDescriptor
let connection: RelayConnection
var last_pong: UInt32
var flags: Int
init(descriptor: RelayDescriptor, connection: RelayConnection) {
self.flags = 0
self.descriptor = descriptor
self.connection = connection
self.last_pong = 0
}
func mark_broken() {

View File

@@ -179,8 +179,23 @@ class RelayPool {
return nil
}
func record_last_pong(relay_id: String, event: NostrConnectionEvent) {
if case .ws_event(let ws_event) = event {
if case .pong = ws_event {
for relay in relays {
if relay.id == relay_id {
relay.last_pong = UInt32(Date.now.timeIntervalSince1970)
return
}
}
}
}
}
func handle_event(relay_id: String, event: NostrConnectionEvent) {
record_last_pong(relay_id: relay_id, event: event)
// handle reconnect logic, etc?
for handler in handlers {
handler.callback(relay_id, event)
@@ -193,3 +208,4 @@ func add_rw_relay(_ pool: RelayPool, _ url: String) {
try? pool.add_relay(url_, info: RelayInfo.rw)
}