Skip to content

Commit

Permalink
perf: speed up server close
Browse files Browse the repository at this point in the history
Waiting `waitUntil` and closing the server can be done in parallel.
  • Loading branch information
Kikobeats committed Apr 30, 2024
1 parent e57ef11 commit 352af90
Showing 1 changed file with 3 additions and 10 deletions.
13 changes: 3 additions & 10 deletions packages/runtime/src/server/run-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { EdgeContext } from '@edge-runtime/vm'
import listen from 'async-listen'
import http from 'http'
import type { ListenOptions } from 'net'
import { promisify } from 'util'

interface ServerOptions<T extends EdgeContext> extends Options<T> {}

Expand Down Expand Up @@ -33,18 +34,10 @@ export async function runServer<T extends EdgeContext>(
const { handler, waitUntil } = createHandler(options)
const server = http.createServer(handler)
const url = await listen(server, options)

const closeServer = promisify(server.close.bind(server))
return {
url: String(url),
close: async () => {
await waitUntil()
await new Promise<void>((resolve, reject) => {
return server.close((err) => {
if (err) reject(err)
resolve()
})
})
},
close: () => Promise.all([waitUntil(), closeServer()]).then(() => void 0),
waitUntil,
}
}

0 comments on commit 352af90

Please sign in to comment.