Skip to content

Commit

Permalink
update:init fib-loadbalancer
Browse files Browse the repository at this point in the history
  • Loading branch information
dingchendong committed Jan 19, 2020
1 parent 00b2563 commit 7e606bb
Show file tree
Hide file tree
Showing 5 changed files with 272 additions and 2 deletions.
46 changes: 44 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,44 @@
# fib-lb
Load balance module for fibxs.
# fib-loadbalancer

## Introduction

Load balance module for fibjs.

## Installation

```shell
npm install fib-loadbalancer
```
or
```shell
fibjs --install fib-loadbalancer
```

## Example

```javascript
...
const http = require('http');
let repeater = require('fib-loadbalancer');

let params = {
urls: ["http://127.0.0.1:8093", "http://127.0.0.1:8092", "http://127.0.0.1:8091"],
health: "health",
response: 'true'
}

var httpServer = new http.Server(8081, [
(req) => {
req.session = {}
}, {
"*": repeater(params)
}
]);
...
```

## Precautions

In params, urls is a required parameter, health and response are optional parameters. Health default is 'health', if the route provided by the user for health check is not 'health', the health parameter of the specified route needs to be filled in. Response default is 'true', if the response provided by the user for health check is not 'true', the response parameter of the specified route needs to be filled in.

[example code](./examples/index.js)
30 changes: 30 additions & 0 deletions example/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const http = require('http');

let repeater = require("../lib/");

let params = {
urls: ["http://127.0.0.1:8093", "http://127.0.0.1:8092", "http://127.0.0.1:8091"],
health: "health",
response: 'true'
}

var httpServer = new http.Server(8081, [
(req) => {
req.session = {}
}, {
"*": repeater(params)
}
]);

setInterval(() => {
console.warn("Server is running", new Date());
}, 1000 * 5);

httpServer.enableCrossOrigin();

let version = process.version.split("-")[0].split('.')[1];
if (Number(version) < 29) {
httpServer.run(() => {});
} else {
httpServer.start();
}
91 changes: 91 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
const http = require('http');
const url = require('url');
const net = require('net');
const coroutine = require('coroutine');

http.timeout = 200;

let temp_urls;

function equar(a, b) {
if (a.length !== b.length) {
return false
} else {
for (let i = 0; i < a.length; i++) {
if (a.indexOf(b[i]) == -1) {
return false
}
}
return true;
}
}

let checkUrlsWorker = (urls, health, response, r) => {

let _urls = [];

coroutine.parallel(urls, function(e) {

let _url = new net.Url(e);
let _rs = _url.resolve(health);

let rs;

try {
rs = http.get(_rs);
} catch (f) {
console.log("URL request timed out : ", e);
return;
}

let data = rs.data || "";

if (rs && rs.statusCode == 200 && data.toString().indexOf(response) != -1) {
_urls.push(e);
} else {
console.log("Unhealthy URL : ", e);
}
})

if (!equar(temp_urls, _urls)) {
temp_urls = _urls;
if (temp_urls.length != 0) {
r.load(temp_urls);
console.log("Healthy URLs have changed : ", temp_urls);
} else {
console.log("No URLs currently available!");
}
}

console.log("CheckUrlsWorker is running, healthy URLs : ", temp_urls);
}

module.exports = params => {

let urls, health, response;

if (!params) {
throw new Error("No Params");
}

if (!params.urls) {
throw new Error("No URLs");
}

urls = params.urls;
temp_urls = params.urls;

response = params.response || 'true';

health = params.health || "health";

let r = new http.Repeater(urls);

checkUrlsWorker(urls, health, response, r)

setInterval(() => {
checkUrlsWorker(urls, health, response, r)
}, 1000 * 5);

return r;
}
11 changes: 11 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "fib-loadbalancer",
"version": "1.0.0",
"description": "Load balance module for fibjs",
"main": "./lib/",
"scripts": {
"test": "fibos ./test/index.js"
},
"author": "",
"license": "ISC"
}
96 changes: 96 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
var test = require('test');
var assert = test.assert;
var http = require('http');
const coroutine = require('coroutine');
var lb = require('../lib/index.js');
test.setup();

function runServer(port) {
return new http.Server(port, {
'/health': (req) => {
console.notice("server health");
req.response.write('true');
}
});
}

describe('fib-lb test', () => {

it('export test', () => {

let params = {};

assert.throws(() => {
lb()
});

assert.throws(() => {
lb(params)
});


params.urls = ["http://127.0.0.1:8093", "http://127.0.0.1:8092", "http://127.0.0.1:8091"];
params.health = "health";
params.response = 'true';

let r = new http.Repeater(params.urls);

assert.typeOf(lb(params), typeof r);

});

it('checkUrlsWorker test', () => {

var svr1 = runServer(8091);
var svr6 = runServer(8096);
var svr3 = new http.Server(8093, {
'/health': (req) => {
console.notice("server health");
req.response.statusCode = 404;
req.response.write('true');
}
});

var svr4 = new http.Server(8094, {
'/health': (req) => {
console.notice("server health");
req.response.write('false');
}
});

svr1.start();
svr6.start();
svr3.start();
svr4.start();

let params = {
urls: ["http://127.0.0.1:8093", "http://127.0.0.1:8091", "http://127.0.0.1:8092", "http://127.0.0.1:8096", "http://127.0.0.1:8095", "http://127.0.0.1:8094"],
health: "health",
response: 'true'
}

let r = lb(params);

assert.deepEqual(r.urls.sort(), ["http://127.0.0.1:8091/", "http://127.0.0.1:8096/"]);

coroutine.sleep(1000 * 3);

svr1.stop();
svr6.stop();

var svr2 = runServer(8092);
var svr5 = runServer(8095);

svr2.start();
svr5.start();

coroutine.sleep(1000 * 3);

assert.deepEqual(r.urls.sort(), ["http://127.0.0.1:8091/", "http://127.0.0.1:8092/", "http://127.0.0.1:8095/", "http://127.0.0.1:8096/"]);

});

});

test.run(console.INFO);
process.exit();

0 comments on commit 7e606bb

Please sign in to comment.