Skip to content

Commit

Permalink
fix(satori): reconnect when prepare failed, fix #307
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Aug 15, 2024
1 parent a6bbf1a commit 345a9c6
Showing 1 changed file with 24 additions and 21 deletions.
45 changes: 24 additions & 21 deletions packages/core/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,33 @@ export namespace Adapter {
const logger = this.ctx.logger('adapter')
const { retryTimes, retryInterval, retryLazy } = this.config

const reconnect = async (initial = false) => {
const reconnect = (initial: boolean, message: string) => {
if (!this.getActive()) return

let timeout = retryInterval
if (_retryCount >= retryTimes) {
if (initial) {
return this.setStatus(Status.OFFLINE, new Error(message))
} else {
timeout = retryLazy
}
}

_retryCount++
this.setStatus(Status.RECONNECT)
logger.warn(`${message}, will retry in ${Time.format(timeout)}...`)
setTimeout(() => {
if (this.getActive()) connect()
}, timeout)
}

const connect = async (initial = false) => {
logger.debug('websocket client opening')
let socket: WebSocket
try {
socket = await this.prepare()
} catch (error) {
logger.warn(error)
reconnect(initial, error.toString() || `failed to prepare websocket`)
return
}

Expand All @@ -71,24 +91,7 @@ export namespace Adapter {
socket.addEventListener('close', ({ code, reason }) => {
this.socket = null
logger.debug(`websocket closed with ${code}`)
if (!this.getActive()) return

const message = reason.toString() || `failed to connect to ${url}, code: ${code}`
let timeout = retryInterval
if (_retryCount >= retryTimes) {
if (initial) {
return this.setStatus(Status.OFFLINE, new Error(message))
} else {
timeout = retryLazy
}
}

_retryCount++
this.setStatus(Status.RECONNECT)
logger.warn(`${message}, will retry in ${Time.format(timeout)}...`)
setTimeout(() => {
if (this.getActive()) reconnect()
}, timeout)
reconnect(initial, reason.toString() || `failed to connect to ${url}, code: ${code}`)
})

socket.addEventListener('open', () => {
Expand All @@ -99,7 +102,7 @@ export namespace Adapter {
})
}

reconnect(true)
connect(true)
}

async stop() {
Expand Down

0 comments on commit 345a9c6

Please sign in to comment.