-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcluster-example.ts
More file actions
76 lines (70 loc) · 1.63 KB
/
cluster-example.ts
File metadata and controls
76 lines (70 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { BunGateway, BunGateLogger } from '../src'
import { cpus } from 'os'
const logger = new BunGateLogger({
level: 'info',
transport: {
target: 'pino-pretty',
options: {
colorize: true,
translateTime: 'SYS:standard',
ignore: 'pid,hostname',
},
},
})
const gateway = new BunGateway({
server: {
port: 3000,
development: false,
},
logger,
cluster: {
enabled: true,
workers: cpus().length,
restartWorkers: true,
maxRestarts: 10,
restartDelay: 1000,
shutdownTimeout: 30000,
},
})
// Add a simple route for testing
gateway.addRoute({
pattern: '/health',
handler: async (req) => {
return new Response(
JSON.stringify({
status: 'healthy',
worker: process.env.CLUSTER_WORKER_ID || 'master',
timestamp: new Date().toISOString(),
}),
{
headers: { 'Content-Type': 'application/json' },
},
)
},
})
// Add a load balancing route
gateway.addRoute({
pattern: '/api/*',
loadBalancer: {
targets: [
{ url: 'http://localhost:8080', weight: 1 },
{ url: 'http://localhost:8081', weight: 1 },
],
strategy: 'round-robin',
healthCheck: {
enabled: true,
interval: 10000,
timeout: 5000,
path: '/health',
},
},
})
// Start the gateway
await gateway.listen(3000)
console.log('BunGate cluster example started')
console.log(`Master process: ${process.pid}`)
console.log(
`Cluster mode: ${gateway.getConfig().cluster?.enabled ? 'enabled' : 'disabled'}`,
)
console.log(`Workers: ${gateway.getConfig().cluster?.workers || 1}`)
console.log('Visit http://localhost:3000/health to test')