diff --git a/README.md b/README.md index c8cb0e5..099b6f9 100644 --- a/README.md +++ b/README.md @@ -387,19 +387,24 @@ export const runServer = mo(import.meta, async (...args: ListenArgs): Promise server.close()); { await using run = workers(4); using threads = serverThreads(run.workers, server); - await run(threads.map(([w, args]) => assign(w, runServer(...args)))); + const fanout = threads.map(([w, args]) => assign(w, runServer(...args))); + void run(fanout); + + await once(server.listen(3000), 'listening'); + console.log('Listening on :3000'); + await once(process, 'SIGINT'); + server.close(); } ``` diff --git a/examples/server-threads/main.ts b/examples/server-threads/main.ts index 62b3441..42c2a8b 100644 --- a/examples/server-threads/main.ts +++ b/examples/server-threads/main.ts @@ -4,21 +4,23 @@ // Run: node examples/server-threads/main.ts // Test: curl http://localhost:3000 +import { once } from 'node:events'; import { createServer } from 'node:net'; import { workers, assign } from '../../src/index.ts'; import { serverThreads } from '../../src/serve/index.ts'; import { runServer } from './server.ts'; const server = createServer(); -server.listen(3000); - -process.on('SIGINT', () => server.close()); -process.on('SIGTERM', () => server.close()); - -console.log('Listening on http://localhost:3000 with 4 worker threads'); { + // Run server threads await using run = workers(4); using threads = serverThreads(run.workers, server); - await run(threads.map(([w, args]) => assign(w, runServer(...args)))); + const fanout = threads.map(([w, args]) => assign(w, runServer(...args))); + void run(fanout); + // Lifecycle + await once(server.listen(3000), 'listening'); + console.log('Listening on http://localhost:3000 with 4 worker threads'); + await once(process, 'SIGINT'); + server.close(); }