diff --git a/package/src/server/index.ts b/package/src/server/index.ts index 3ae53c8..89e6242 100644 --- a/package/src/server/index.ts +++ b/package/src/server/index.ts @@ -1,6 +1,8 @@ /// import { App } from 'astro/app'; +import cluster from 'node:cluster'; +import os from 'node:os'; import { extractHostname, serveStaticFile } from '~/server/utils.ts'; @@ -24,33 +26,44 @@ export function createExports(manifest: SSRManifest, options: Options): CreateEx let _server: Server | null = null; export function start(manifest: SSRManifest, options: Options) { - const app = new App(manifest); - const logger = app.getAdapterLogger(); - const hostname = process.env.HOST ?? extractHostname(options.host); const port = process.env.PORT ? Number.parseInt(process.env.PORT) : options.port; - _server = Bun.serve({ - development: import.meta.env.DEV, - error: (error) => - new Response(`
${error}\n${error.stack}
`, { - headers: { 'Content-Type': 'text/html' }, - }), - fetch: handler(manifest, options), - hostname, - port, - }); - - function exit() { - if (_server) _server.stop(); - process.exit(); - } + if (cluster.isPrimary && options.cluster) { + const numCPUs = os.cpus().length; + for (let i = 0; i < numCPUs; i++) { + cluster.fork(); + } + cluster.on('exit', (worker, code, signal) => { + console.log(`Worker ${worker.process.pid} died`); + cluster.fork(); + }); + } else { + const app = new App(manifest); + const logger = app.getAdapterLogger(); + + _server = Bun.serve({ + development: import.meta.env.DEV, + error: (error) => + new Response(`
${error}\n${error.stack}
`, { + headers: { 'Content-Type': 'text/html' }, + }), + fetch: handler(manifest, options), + hostname, + port, + }); + + function exit() { + if (_server) _server.stop(); + process.exit(); + } - process.on('SIGINT', exit); - process.on('SIGTERM', exit); - process.on('exit', exit); + process.on('SIGINT', exit); + process.on('SIGTERM', exit); + process.on('exit', exit); - logger.info(`Server listening on ${_server.url.href}`); + logger.info(`Server listening on ${_server.url.href}`); + } } function handler( diff --git a/package/src/validators.ts b/package/src/validators.ts index 033c51f..0e56504 100644 --- a/package/src/validators.ts +++ b/package/src/validators.ts @@ -18,5 +18,11 @@ export const OptionsSchema = z port: z.coerce.number().default(4321), /** TODO(@nurodev): Undocumented */ server: z.string(), + /** + * Enable clustering for the server. (Only linux!) + * + * @default false + */ + cluster: z.boolean().default(false), }) .partial();