-
Notifications
You must be signed in to change notification settings - Fork 0
/
loadBalancer.js
47 lines (40 loc) · 1.14 KB
/
loadBalancer.js
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
const express = require('express');
const axios = require('axios')
const httpProxy = require('http-proxy')
const app = express();
const PORT = 8080;
const servers = [
{ url: 'http://localhost:3000', alive: true },
{ url: 'http://localhost:4000', alive: true },
{ url: 'http://localhost:5000', alive: true }
]
const checkServerHealth = async (server) => {
try {
await axios.get(`${server.url}/health`);
server.alive = true;
} catch (error) {
server.alive = false;
}
}
const healthCheck = () => {
servers.forEach( server => checkServerHealth(server))
}
setInterval(healthCheck, 5000)
let currentServer = 0, server
const proxy = httpProxy.createProxyServer()
app.use((req, res) => {
server = servers[currentServer]
currentServer === servers.length -1? currentServer = 0 : ++currentServer
if(server.alive){
proxy.web(req, res, { target: server.url }, (e) => {
if(e){
res.status(500).send('Server error')
console.log(e);
}
})
return;
}
})
app.listen(PORT, () => {
console.log(`Load balancer running on port ${PORT}`)
})