Skip to content
This repository has been archived by the owner on Aug 28, 2020. It is now read-only.

Commit

Permalink
Load barancer
Browse files Browse the repository at this point in the history
  • Loading branch information
ukatama committed May 23, 2016
1 parent 9eac222 commit f7abae2
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
### Added
- Load barancer

## [1.2.1] - 2016-05-21
### Fixed
- Error on Login page renderer
Expand Down
24 changes: 15 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,15 @@ Specify proxy routes by array in descending order of priority.

Each of route is specified in an object consisting of the following keys:

| key | type | description |
|---------|-----------------|-----------------------------------------------------------------------------------------------|
| host | string | Host name matcher |
| url | string (regexp) | URL matcher. optional. e.g. `"^/foo/bar[0-9]+$"` |
| methods | array of string | Arrowed methods |
| app | string | Application ID |
| etcd | string | Key of the target address in etcd. Specified by `--env APP=<vlalue>` in [Connect](#Connect). |
| public | boolean | Set `true` to disable authentication. (Default: `false`) |
| key | type | description |
|----------|-----------------|-----------------------------------------------------------------------------------------------|
| host | string | Host name matcher |
| url | string (regexp) | URL matcher. optional. e.g. `"^/foo/bar[0-9]+$"` |
| methods | array of string | Arrowed methods |
| app | string | Application ID |
| etcd | string | Key of the target address in etcd. Specified by `--env APP=<vlalue>` in [Connect](#Connect). |
| public | boolean | Set `true` to disable authentication. (Default: `false`) |
| backends | number | Set number of backend instances to enable load barancing. Set null or 0 to disable LB. |

Route configurations can be write under `route` key of [configuration file](#Configuration).

Expand All @@ -171,6 +172,10 @@ You have to register JSON value of route configurations into etcd to Live-Config
$ docker exec -i etcd /etcdctl set /routes "$(cat /path/to/routes.json)"
```

#### Load barancing
Backend applications must be named `app-N` and registerd into etcd.
N is an integral number from 1 on up.

#### Example of `routes.json`
```json
[
Expand All @@ -190,7 +195,8 @@ $ docker exec -i etcd /etcdctl set /routes "$(cat /path/to/routes.json)"
{
"host": "bar.b.example.com",
"app": "b",
"etcd": "bar"
"etcd": "bar",
"backends": 3
}
]
```
22 changes: 22 additions & 0 deletions src/server/__tests__/test-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ describe('Router', () => {
host: 'app2.example.com',
etcd: 'app2',
},
{
app: 'app4',
host: 'app4.example.com',
etcd: 'app4',
backends: 3,
},
];

let etcd, router;
Expand Down Expand Up @@ -118,4 +124,20 @@ describe('Router', () => {
expect(route).toEqual(routes[1]);
});
});

pit('routes as loadbarancer', () => {
Math.random = jest.fn().mockReturnValue((3.2 - 1) / 3);

etcd.get.mockReturnValueOnce(
Promise.resolve({ value: 'http://etcd-target-app4.example.com' })
);

return router.route('app4.example.com', '/', 'GET').then((route) => {
expect(route).toEqual({
...routes[4],
target: 'http://etcd-target-app4.example.com',
});
expect(etcd.get).toBeCalledWith('backends/app4-3', true);
});
});
});
6 changes: 5 additions & 1 deletion src/server/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ export class Router {
return route;
}

return this.etcd.get(`backends/${route.etcd}`, true)
const n = route.backends &&
Math.floor(Math.random() * route.backends) + 1;
const backend = n ? `${route.etcd}-${n}` : route.etcd;

return this.etcd.get(`backends/${backend}`, true)
.then((node) => ({
...route,
target: node.value,
Expand Down

0 comments on commit f7abae2

Please sign in to comment.