Skip to content

Commit

Permalink
hotfix port discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
ermalkaleci committed Mar 25, 2024
1 parent e399e60 commit 3acbd8b
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 18 deletions.
2 changes: 1 addition & 1 deletion executor/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@acala-network/chopsticks-executor",
"description": "Chopsticks executor",
"version": "0.9.11",
"version": "0.9.12",
"license": "Apache-2.0",
"type": "module",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/chopsticks/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@acala-network/chopsticks",
"version": "0.9.11",
"version": "0.9.12",
"author": "Acala Developers <[email protected]>",
"license": "Apache-2.0",
"bin": "./chopsticks.cjs",
Expand Down
44 changes: 33 additions & 11 deletions packages/chopsticks/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,27 @@ const respond = (res: http.ServerResponse, data?: any) => {
res.end()
}

const portInUse = async (port: number) => {
const server = http.createServer()
const inUse = await new Promise<boolean>((resolve) => {
server.once('error', (e: any) => {
if (e.code === 'EADDRINUSE') {
resolve(true)
} else {
resolve(false)
}
})
server.once('listening', () => {
server.close()
resolve(false)
})
server.listen(port)
})
server.removeAllListeners()
server.unref()
return inUse
}

export const createServer = async (handler: Handler, port: number) => {
let wss: WebSocketServer | undefined
let listenPort: number | undefined
Expand Down Expand Up @@ -118,24 +139,25 @@ export const createServer = async (handler: Handler, port: number) => {
})

for (let i = 0; i < 10; i++) {
if (port && (await portInUse(port + i))) {
continue
}
const preferPort = port ? port + i : undefined
wsLogger.debug('Try starting on port %d', preferPort)
const success = await new Promise<boolean>((resolve) => {
server.once('error', (e: any) => {
if (e.code === 'EADDRINUSE') {
server.close()
resolve(false)
}
})
await new Promise<void>((resolve, reject) => {
const onError = (e: Error) => {
server.close()
reject(e)
}
server.once('error', onError)
server.listen(preferPort, () => {
wss = new WebSocketServer({ server, maxPayload: 1024 * 1024 * 100 })
listenPort = (server.address() as AddressInfo).port
resolve(true)
server.removeListener('error', onError)
resolve()
})
})
if (success) {
break
}
break
}

if (!wss || !listenPort) {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@acala-network/chopsticks-core",
"version": "0.9.11",
"version": "0.9.12",
"author": "Acala Developers <[email protected]>",
"license": "Apache-2.0",
"type": "module",
Expand Down
2 changes: 1 addition & 1 deletion packages/db/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@acala-network/chopsticks-db",
"version": "0.9.11",
"version": "0.9.12",
"author": "Acala Developers <[email protected]>",
"license": "Apache-2.0",
"type": "module",
Expand Down
2 changes: 1 addition & 1 deletion packages/e2e/src/genesis-provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe.each([
['Asset Hub Kusama', new URL('../blobs/asset-hub-kusama.json', import.meta.url).pathname],
])(`genesis provider works %s`, async (name, genesis) => {
const { chain, dev, api, teardown } = await setupContextWithConfig({
port: 0,
port: 1234,
genesis,
'build-block-mode': BuildBlockMode.Manual,
})
Expand Down
2 changes: 1 addition & 1 deletion packages/testing/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@acala-network/chopsticks-testing",
"version": "0.9.11",
"version": "0.9.12",
"author": "Acala Developers <[email protected]>",
"license": "Apache-2.0",
"type": "module",
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@acala-network/chopsticks-utils",
"version": "0.9.11",
"version": "0.9.12",
"author": "Acala Developers <[email protected]>",
"license": "Apache-2.0",
"type": "module",
Expand Down

0 comments on commit 3acbd8b

Please sign in to comment.