Skip to content

Commit b049875

Browse files
committed
feat: Add new load balancing strategies including Power of Two Choices, Latency-based, and Weighted Least Connections; update demo routes and logging
1 parent ccc2aec commit b049875

1 file changed

Lines changed: 126 additions & 7 deletions

File tree

examples/lb-example-all-options.ts

Lines changed: 126 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const gateway = new BunGateway({
4242
})
4343

4444
// =============================================================================
45-
// 1. ROUND ROBIN LOAD BALANCER WITH HEALTH CHECKS
45+
// ROUND ROBIN LOAD BALANCER WITH HEALTH CHECKS
4646
// =============================================================================
4747
console.log('\n1️⃣ Round Robin with Health Checks')
4848

@@ -83,7 +83,7 @@ gateway.addRoute({
8383
})
8484

8585
// =============================================================================
86-
// 2. WEIGHTED LOAD BALANCER FOR HIGH-PERFORMANCE SCENARIOS
86+
// WEIGHTED LOAD BALANCER FOR HIGH-PERFORMANCE SCENARIOS
8787
// =============================================================================
8888
console.log('2️⃣ Weighted Load Balancer (Performance Optimized)')
8989

@@ -151,7 +151,7 @@ gateway.addRoute({
151151
})
152152

153153
// =============================================================================
154-
// 4. IP HASH FOR SESSION AFFINITY
154+
// IP HASH FOR SESSION AFFINITY
155155
// =============================================================================
156156
console.log('4️⃣ IP Hash for Session Affinity')
157157

@@ -190,7 +190,7 @@ gateway.addRoute({
190190
})
191191

192192
// =============================================================================
193-
// 5. RANDOM STRATEGY WITH ADVANCED ERROR HANDLING
193+
// RANDOM STRATEGY WITH ADVANCED ERROR HANDLING
194194
// =============================================================================
195195
console.log('5️⃣ Random Strategy with Advanced Error Handling')
196196

@@ -296,7 +296,118 @@ gateway.addRoute({
296296
})
297297

298298
// =============================================================================
299-
// 7. MONITORING AND METRICS ENDPOINT
299+
// POWER OF TWO CHOICES (P2C) STRATEGY
300+
// =============================================================================
301+
console.log('6️⃣➕ Power of Two Choices (P2C) Strategy')
302+
303+
gateway.addRoute({
304+
pattern: '/api/p2c/*',
305+
loadBalancer: {
306+
strategy: 'p2c',
307+
targets: [
308+
{ url: 'http://localhost:8080' },
309+
{ url: 'http://localhost:8081' },
310+
],
311+
healthCheck: {
312+
enabled: true,
313+
interval: 10000,
314+
timeout: 4000,
315+
path: '/',
316+
expectedStatus: 200,
317+
},
318+
},
319+
proxy: {
320+
pathRewrite: (path) => path.replace('/api/p2c', ''),
321+
},
322+
hooks: {
323+
beforeRequest: async (req) => {
324+
logger.debug(`🎲 P2C routing for: ${req.url}`)
325+
},
326+
},
327+
})
328+
329+
// Alias for P2C
330+
gateway.addRoute({
331+
pattern: '/api/power-of-two/*',
332+
loadBalancer: {
333+
strategy: 'power-of-two-choices',
334+
targets: [
335+
{ url: 'http://localhost:8080' },
336+
{ url: 'http://localhost:8081' },
337+
],
338+
healthCheck: {
339+
enabled: true,
340+
interval: 10000,
341+
timeout: 4000,
342+
path: '/',
343+
expectedStatus: 200,
344+
},
345+
},
346+
proxy: {
347+
pathRewrite: (path) => path.replace('/api/power-of-two', ''),
348+
},
349+
})
350+
351+
// =============================================================================
352+
// LATENCY-BASED STRATEGY
353+
// =============================================================================
354+
console.log('6️⃣✅ Latency-based Strategy')
355+
356+
gateway.addRoute({
357+
pattern: '/api/latency/*',
358+
loadBalancer: {
359+
strategy: 'latency',
360+
targets: [
361+
{ url: 'http://localhost:8080' },
362+
{ url: 'http://localhost:8081' },
363+
],
364+
healthCheck: {
365+
enabled: true,
366+
interval: 8000,
367+
timeout: 3000,
368+
path: '/',
369+
expectedStatus: 200,
370+
},
371+
},
372+
proxy: {
373+
pathRewrite: (path) => path.replace('/api/latency', ''),
374+
timeout: 10000,
375+
},
376+
hooks: {
377+
afterResponse: async (req, res) => {
378+
logger.info(`⏱️ Latency strategy served with ${res.status}`)
379+
},
380+
},
381+
})
382+
383+
// =============================================================================
384+
// WEIGHTED LEAST CONNECTIONS
385+
// =============================================================================
386+
console.log('6️⃣🔢 Weighted Least Connections Strategy')
387+
388+
gateway.addRoute({
389+
pattern: '/api/wlc/*',
390+
loadBalancer: {
391+
strategy: 'weighted-least-connections',
392+
targets: [
393+
{ url: 'http://localhost:8080', weight: 3 },
394+
{ url: 'http://localhost:8081', weight: 1 },
395+
],
396+
healthCheck: {
397+
enabled: true,
398+
interval: 12000,
399+
timeout: 4000,
400+
path: '/',
401+
expectedStatus: 200,
402+
},
403+
},
404+
proxy: {
405+
pathRewrite: (path) => path.replace('/api/wlc', ''),
406+
},
407+
})
408+
409+
// =============================================================================
410+
// MONITORING AND METRICS ENDPOINT
300411
// =============================================================================
301412
console.log('7️⃣ Monitoring and Metrics')
302413

@@ -327,7 +438,7 @@ gateway.addRoute({
327438
})
328439

329440
// =============================================================================
330-
// 8. HEALTH CHECK ENDPOINT
441+
// HEALTH CHECK ENDPOINT
331442
// =============================================================================
332443
gateway.addRoute({
333444
pattern: '/health',
@@ -348,7 +459,7 @@ gateway.addRoute({
348459
})
349460

350461
// =============================================================================
351-
// 9. DEMO ENDPOINTS FOR TESTING
462+
// DEMO ENDPOINTS FOR TESTING
352463
// =============================================================================
353464
gateway.addRoute({
354465
pattern: '/demo',
@@ -359,6 +470,10 @@ gateway.addRoute({
359470
'Least Connections': '/api/least-connections/get',
360471
'IP Hash': '/api/ip-hash/get',
361472
Random: '/api/random/get',
473+
'Power of Two Choices (P2C)': '/api/p2c/get',
474+
'P2C Alias (power-of-two-choices)': '/api/power-of-two/get',
475+
'Latency-based': '/api/latency/get',
476+
'Weighted Least Connections': '/api/wlc/get',
362477
'Users Service': '/api/users/1',
363478
'Posts Service': '/api/posts/1',
364479
Metrics: '/metrics',
@@ -430,6 +545,10 @@ try {
430545
console.log(' • Least Connections: /api/least-connections/*')
431546
console.log(' • IP Hash: /api/ip-hash/*')
432547
console.log(' • Random: /api/random/*')
548+
console.log(' • Power of Two Choices (P2C): /api/p2c/*')
549+
console.log(' • P2C Alias (power-of-two-choices): /api/power-of-two/*')
550+
console.log(' • Latency-based: /api/latency/*')
551+
console.log(' • Weighted Least Connections: /api/wlc/*')
433552
console.log(' • Users Service: /api/users/*')
434553
console.log(' • Posts Service: /api/posts/*')
435554

0 commit comments

Comments
 (0)