Skip to content

Commit

Permalink
fix: 当token超时后重新连接ws会使用已过期的token
Browse files Browse the repository at this point in the history
  • Loading branch information
bangbang93 committed Apr 19, 2024
1 parent 3dcbf38 commit d57a159
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export async function bootstrap(version: string): Promise<void> {
throw e
}

await cluster.connect()
cluster.connect()
const proto = config.byoc ? 'http' : 'https'
if (proto === 'https') {
logger.info('请求证书')
Expand Down
14 changes: 11 additions & 3 deletions src/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,12 +369,20 @@ export class Cluster {
})
}

public async connect(): Promise<void> {
public connect(): void {
if (this.socket?.connected) return
this.socket = connect(this.prefixUrl, {
transports: ['websocket'],
auth: {
token: await this.tokenManager.getToken(),
auth: (cb) => {
this.tokenManager
.getToken()
.then((token) => {
cb({token})
})
.catch((e) => {
logger.error(e, 'get token error')
this.exit(1)
})
},
})
this.socket.on('error', this.onConnectionError.bind(this, 'error'))
Expand Down
2 changes: 1 addition & 1 deletion src/keepalive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class Keepalive {
if (this.keepAliveError >= 1) {
await Bluebird.try(async () => {
await this.cluster.disable()
await this.cluster.connect()
this.cluster.connect()
await this.cluster.enable()
})
.timeout(ms('10m'), 'restart timeout')
Expand Down

0 comments on commit d57a159

Please sign in to comment.