mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2026-02-01 06:48:51 +00:00
finally stop reconnecting when the first connection fails once and for all.
This commit is contained in:
@@ -45,7 +45,7 @@ export class AbstractRelay {
|
|||||||
private reconnectTimeoutHandle: ReturnType<typeof setTimeout> | undefined
|
private reconnectTimeoutHandle: ReturnType<typeof setTimeout> | undefined
|
||||||
private pingIntervalHandle: ReturnType<typeof setInterval> | undefined
|
private pingIntervalHandle: ReturnType<typeof setInterval> | undefined
|
||||||
private reconnectAttempts: number = 0
|
private reconnectAttempts: number = 0
|
||||||
private closedIntentionally: boolean = false
|
private skipReconnection: boolean = false
|
||||||
|
|
||||||
private connectionPromise: Promise<void> | undefined
|
private connectionPromise: Promise<void> | undefined
|
||||||
private openCountRequests = new Map<string, CountResolver>()
|
private openCountRequests = new Map<string, CountResolver>()
|
||||||
@@ -120,12 +120,9 @@ export class AbstractRelay {
|
|||||||
this._connected = false
|
this._connected = false
|
||||||
this.connectionPromise = undefined
|
this.connectionPromise = undefined
|
||||||
|
|
||||||
const wasIntentional = this.closedIntentionally
|
|
||||||
this.closedIntentionally = false // reset for next time
|
|
||||||
|
|
||||||
this.onclose?.()
|
this.onclose?.()
|
||||||
|
|
||||||
if (this.enableReconnect && !wasIntentional) {
|
if (this.enableReconnect && !this.skipReconnection) {
|
||||||
this.reconnect()
|
this.reconnect()
|
||||||
} else {
|
} else {
|
||||||
this.closeAllSubscriptions(reason)
|
this.closeAllSubscriptions(reason)
|
||||||
@@ -139,11 +136,13 @@ export class AbstractRelay {
|
|||||||
|
|
||||||
this.challenge = undefined
|
this.challenge = undefined
|
||||||
this.authPromise = undefined
|
this.authPromise = undefined
|
||||||
|
this.skipReconnection = false
|
||||||
this.connectionPromise = new Promise((resolve, reject) => {
|
this.connectionPromise = new Promise((resolve, reject) => {
|
||||||
if (opts?.timeout) {
|
if (opts?.timeout) {
|
||||||
connectionTimeoutHandle = setTimeout(() => {
|
connectionTimeoutHandle = setTimeout(() => {
|
||||||
reject('connection timed out')
|
reject('connection timed out')
|
||||||
this.connectionPromise = undefined
|
this.connectionPromise = undefined
|
||||||
|
this.skipReconnection = true
|
||||||
this.onclose?.()
|
this.onclose?.()
|
||||||
this.handleHardClose('relay connection timed out')
|
this.handleHardClose('relay connection timed out')
|
||||||
}, opts.timeout)
|
}, opts.timeout)
|
||||||
@@ -153,18 +152,8 @@ export class AbstractRelay {
|
|||||||
opts.abort.onabort = reject
|
opts.abort.onabort = reject
|
||||||
}
|
}
|
||||||
|
|
||||||
const connectionFailed = () => {
|
|
||||||
clearTimeout(connectionTimeoutHandle)
|
|
||||||
reject('connection failed')
|
|
||||||
this.connectionPromise = undefined
|
|
||||||
this.closedIntentionally = true // prevent reconnect attempts on initial connection failure
|
|
||||||
this.onclose?.()
|
|
||||||
this.handleHardClose('relay connection failed')
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
this.ws = new this._WebSocket(this.url)
|
this.ws = new this._WebSocket(this.url)
|
||||||
this.ws.addEventListener('error', connectionFailed)
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
clearTimeout(connectionTimeoutHandle)
|
clearTimeout(connectionTimeoutHandle)
|
||||||
reject(err)
|
reject(err)
|
||||||
@@ -172,8 +161,6 @@ export class AbstractRelay {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.ws.onopen = () => {
|
this.ws.onopen = () => {
|
||||||
this.ws?.removeEventListener('error', connectionFailed)
|
|
||||||
|
|
||||||
if (this.reconnectTimeoutHandle) {
|
if (this.reconnectTimeoutHandle) {
|
||||||
clearTimeout(this.reconnectTimeoutHandle)
|
clearTimeout(this.reconnectTimeoutHandle)
|
||||||
this.reconnectTimeoutHandle = undefined
|
this.reconnectTimeoutHandle = undefined
|
||||||
@@ -203,10 +190,13 @@ export class AbstractRelay {
|
|||||||
resolve()
|
resolve()
|
||||||
}
|
}
|
||||||
|
|
||||||
this.ws.onerror = ev => {
|
this.ws.onerror = () => {
|
||||||
clearTimeout(connectionTimeoutHandle)
|
clearTimeout(connectionTimeoutHandle)
|
||||||
reject((ev as any).message || 'websocket error')
|
reject('connection failed')
|
||||||
this.handleHardClose('relay connection errored')
|
this.connectionPromise = undefined
|
||||||
|
this.skipReconnection = true
|
||||||
|
this.onclose?.()
|
||||||
|
this.handleHardClose('relay connection failed')
|
||||||
}
|
}
|
||||||
|
|
||||||
this.ws.onclose = ev => {
|
this.ws.onclose = ev => {
|
||||||
@@ -478,7 +468,7 @@ export class AbstractRelay {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public close() {
|
public close() {
|
||||||
this.closedIntentionally = true
|
this.skipReconnection = true
|
||||||
if (this.reconnectTimeoutHandle) {
|
if (this.reconnectTimeoutHandle) {
|
||||||
clearTimeout(this.reconnectTimeoutHandle)
|
clearTimeout(this.reconnectTimeoutHandle)
|
||||||
this.reconnectTimeoutHandle = undefined
|
this.reconnectTimeoutHandle = undefined
|
||||||
|
|||||||
Reference in New Issue
Block a user