-
Notifications
You must be signed in to change notification settings - Fork 546
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(preset): adding bun-cluster preset
- Loading branch information
Showing
6 changed files
with
191 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { spawn, type Subprocess } from 'bun'; | ||
import { fileURLToPath } from 'mlly'; | ||
import { | ||
getGracefulShutdownConfig, | ||
trapUnhandledNodeErrors, | ||
} from 'nitropack/runtime/internal'; | ||
import { join, dirname } from 'pathe'; | ||
|
||
const numberOfWorkers = | ||
Number.parseInt(process.env.NITRO_CLUSTER_WORKERS || "") || | ||
navigator.hardwareConcurrency; | ||
|
||
const workers: Subprocess[] = Array.from({ length: numberOfWorkers }) | ||
const dirName = dirname(fileURLToPath(import.meta.url)) | ||
const workerFile = join(dirName, "./bun-worker") | ||
|
||
for (let i = 0; i < numberOfWorkers; i++) { | ||
workers[i] = createWorker(i) | ||
} | ||
|
||
let isShuttingDown = false; | ||
let isTimedOut = false | ||
|
||
const shutdownConfig = getGracefulShutdownConfig(); | ||
|
||
if (!shutdownConfig.disabled) { | ||
for (const signal of shutdownConfig.signals) { | ||
process.once(signal, onShutdown); | ||
} | ||
} | ||
|
||
trapUnhandledNodeErrors(); | ||
|
||
function createWorker(workerIndex: number): Subprocess { | ||
return spawn({ | ||
cmd: ["bun", "--bun", workerFile], | ||
stdout: "inherit", | ||
stderr: "inherit", | ||
stdin: "inherit", | ||
onExit: () => { | ||
if (!isShuttingDown) | ||
workers[workerIndex] = createWorker(workerIndex) | ||
} | ||
}); | ||
} | ||
|
||
async function onShutdown() { | ||
if (isShuttingDown) { | ||
return; | ||
} | ||
isShuttingDown = true; | ||
|
||
killWorkers() | ||
|
||
const workersKilled = Promise.all(workers.map(i => i.exited)) | ||
|
||
await Promise.any([setTimeout(), workersKilled]) | ||
|
||
if (isTimedOut) { | ||
console.warn( | ||
"[nitro] [cluster] Timeout reached for graceful shutdown. Forcing exit." | ||
); | ||
} | ||
|
||
if (shutdownConfig.forceExit) { | ||
process.exit(0); | ||
} | ||
} | ||
|
||
async function setTimeout() { | ||
isTimedOut = false | ||
await Bun.sleep(shutdownConfig.timeout) | ||
isTimedOut = true | ||
} | ||
|
||
function killWorkers() { | ||
for (const bun of workers) { | ||
bun.kill(); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import "#nitro-internal-pollyfills"; | ||
import { useNitroApp } from "nitropack/runtime"; | ||
import { startScheduleRunner } from "nitropack/runtime/internal"; | ||
|
||
import wsAdapter from "crossws/adapters/bun"; | ||
|
||
const nitroApp = useNitroApp(); | ||
|
||
const ws = import.meta._websocket | ||
? wsAdapter(nitroApp.h3App.websocket) | ||
: undefined; | ||
|
||
const server = Bun.serve({ | ||
reusePort: true, | ||
port: process.env.NITRO_PORT || process.env.PORT || 3000, | ||
websocket: import.meta._websocket ? ws!.websocket : (undefined as any), | ||
async fetch(req: Request, server: any) { | ||
// https://crossws.unjs.io/adapters/bun | ||
if (import.meta._websocket && req.headers.get("upgrade") === "websocket") { | ||
return ws!.handleUpgrade(req, server); | ||
} | ||
|
||
const url = new URL(req.url); | ||
|
||
let body; | ||
if (req.body) { | ||
body = await req.arrayBuffer(); | ||
} | ||
|
||
return nitroApp.localFetch(url.pathname + url.search, { | ||
host: url.hostname, | ||
protocol: url.protocol, | ||
headers: req.headers, | ||
method: req.method, | ||
redirect: req.redirect, | ||
body, | ||
}); | ||
}, | ||
}); | ||
|
||
console.log(`Listening on http://localhost:${server.port}...`); | ||
|
||
// Scheduled tasks | ||
if (import.meta._tasks) { | ||
startScheduleRunner(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters