Turdus
Turdus is a client side HTTP Round-robin and Weighted round-robin load balancing library.
It provides following features:
- Client side Load balancing
- Fault tolerance
- Normalized Response on request failure.
- Mutiple apps supported.
Principle
For simple Round-robin, it is basically array loop.
For weighted round robin algorithm, on each peer selection we increase current_weight of each eligible peer by its weight, select peer with greatest current_weight and reduce its current_weight by total number of weight points distributed among peers.
see also: nginx implementation
[Deprecated] For weighted round robin algorithm (gcd version):
- calculate GCD of all endpoints' weight
- choose the max weighted endpoint then minus GCD
- if largest weight belongs to more than one endpoint, choose the small index one.
- set to original weight until no weight bigger than zero.
- repeat the procedures above.
Usage
install turdus by npm.
npm install turdus
Provide or fetch your callee apps' IP/domain from written configs
or subscribe event by eureka client and fetchRegistry from Eureka
- simple polling each server.
const Turdus = ;// initialize multiple appconst trudus = ; { await turdus;} ;;;// ... // will touch known resources one by one.//// curl http://127.0.0.1/cat-books// curl http://127.0.0.2/cat-books// curl http://127.0.0.3/cat-books// curl http://127.0.0.1/cat-books// curl http://127.0.0.2/cat-books// curl http://127.0.0.3/cat-books// ...
- weighted polling each servers;
const Turdus = ;const turdus = ; { await turdus;} ;;;// ... // will touch known resources by their weight.//// curl http://127.0.0.2/cat-books// curl http://127.0.0.1/cat-books// curl http://127.0.0.3/cat-books// curl http://127.0.0.2/cat-books// curl http://127.0.0.2/cat-books// curl http://127.0.0.1/cat-books// curl http://127.0.0.2/cat-books// curl http://127.0.0.3/cat-books// curl http://127.0.0.2/cat-books// curl http://127.0.0.1/cat-books// curl http://127.0.0.2/cat-books// curl http://127.0.0.3/cat-books// curl http://127.0.0.2/cat-books// curl http://127.0.0.1/cat-books// curl http://127.0.0.2/cat-books// curl http://127.0.0.2/cat-books// curl http://127.0.0.3/cat-books// curl http://127.0.0.1/cat-books// curl http://127.0.0.2/cat-books// ...
- fault tolerance and nomalize error response.
const turdus = ;turdus
- update exist applications' endpoints or add new applications.
const endpoints = kitten: server: '127.0.0.1' weight: 0 server: '127.0.0.2' weight: 0 doggy: server: '192.168.0.1' weight: 0 server: '192.168.0.2' weight: 0 ;const turdus = ;turdus; // the doggy application's endpoints will be changed.// also it's balance type got changed from raw to weighted.
NOTE: this function may reset related applications' current polling order to original status.
License
MIT