Skip to content

Commit

Permalink
连续5次keep alive失败时,重试连接,若还是失败,则退出
Browse files Browse the repository at this point in the history
  • Loading branch information
bangbang93 committed Jul 13, 2020
1 parent 58f1ca3 commit b459319
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export class Cluster {
public isEnabled = false
public keepAliveInterval: Timeout

private keepAliveError = 0
private readonly prefixUrl = process.env.CLUSTER_BMCLAPI || 'https://openbmclapi.bangbang93.com'
private readonly cacheDir = join(__dirname, '..', 'cache')
private readonly host: string
Expand Down Expand Up @@ -166,6 +167,7 @@ export class Cluster {
public async disable(): Promise<void> {
return new Promise((resolve, reject) => {
this.io.emit('disable', null, (ack) => {
this.isEnabled = false
if (ack !== true) return reject(ack)
this.io.disconnect()
resolve()
Expand Down Expand Up @@ -220,9 +222,23 @@ export class Cluster {
console.log('kicked by server')
process.exit(1)
}
this.keepAliveError = 0
} catch (e) {
this.keepAliveError++
console.error('keep alive error')
console.error(e)
if (this.keepAliveError >= 5) {
console.error('exit')
await Bluebird.try(async () => {
await this.disable()
await this.enable()
})
.timeout(ms('30s'))
.catch((e) => {
console.error(e, 'restart failed')
process.exit(1)
})
}
} finally {
this.keepAliveInterval = setTimeout(this._keepAlive.bind(this), ms('1m'))
}
Expand Down

0 comments on commit b459319

Please sign in to comment.