pool: hooks to notify when a relay fails to connect, then ask whether a connection should be attempted.

This commit is contained in:
fiatjaf
2026-01-30 15:21:25 -03:00
parent b3d314643a
commit b624ad4059
4 changed files with 42 additions and 4 deletions

View File

@@ -145,7 +145,7 @@ export class AbstractRelay {
reject('connection timed out')
this.connectionPromise = undefined
this.onclose?.()
this.closeAllSubscriptions('relay connection timed out')
this.handleHardClose('relay connection timed out')
}, opts.timeout)
}
@@ -153,8 +153,17 @@ export class AbstractRelay {
opts.abort.onabort = reject
}
const connectionFailed = () => {
clearTimeout(connectionTimeoutHandle)
reject('connection failed')
this.connectionPromise = undefined
this.onclose?.()
this.handleHardClose('relay connection failed')
}
try {
this.ws = new this._WebSocket(this.url)
this.ws.addEventListener('error', connectionFailed)
} catch (err) {
clearTimeout(connectionTimeoutHandle)
reject(err)
@@ -162,6 +171,8 @@ export class AbstractRelay {
}
this.ws.onopen = () => {
this.ws?.removeEventListener('error', connectionFailed)
if (this.reconnectTimeoutHandle) {
clearTimeout(this.reconnectTimeoutHandle)
this.reconnectTimeoutHandle = undefined