Skip to content

Commit

Permalink
Wait for server listen retries.
Browse files Browse the repository at this point in the history
  • Loading branch information
saul-jb committed Apr 12, 2024
1 parent 72a5d14 commit dfd21e8
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions packages/rpc/src/rpc-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,29 @@ export class RPCServer {
}

async start (): Promise<void> {
let retryCount = 0
for (let i = 0; i <= this.options.retryCount; i++) {
try {
await new Promise<void>(resolve => this.server.listen(this.path, resolve))
break
} catch (e) {
const code = (e as { code?: string }).code

if (code === 'EADDRINUSE') {
if (i >= this.options.retryCount) {
throw e
}

this.server.on('error', (e: Error & { code?: string }) => {
if (e.code === 'EADDRINUSE') {
if (retryCount > this.options.retryCount) {
throw e
}
this.events.dispatchEvent(new RPCEvent(code))

this.events.dispatchEvent(new RPCEvent(e.code))
await new Promise(resolve => setTimeout(resolve, this.options.retryDelay))

setTimeout(() => {
retryCount++
this.server.close()
this.server.listen(this.path)
}, this.options.retryDelay)
} else {
throw e
}
}
})

await new Promise<void>(resolve => this.server.listen(this.path, resolve))
}

this.rpc.addMethod('rpc-abort', (raw, { id }) => {
const params = z.object({ id: z.number() }).parse(raw)
Expand Down

0 comments on commit dfd21e8

Please sign in to comment.