Closed sixinyiyu closed 4 months ago
I was actually thinking about the similar thing where "targets" in a LoadBalancer can be of any value type rather than just strings. That means you can have lb.next().id as an object containing whatever info you need for that target. Do I get your point correctly? @sixinyiyu
I was actually thinking about the similar thing where "targets" in a LoadBalancer can be of any value type rather than just strings. That means you can have lb.next().id as an object containing whatever info you need for that target. Do I get your point correctly
yes, containing whatever info you need for that target ,not just id。
A complete overhaul of the LoadBalancer API has just been committed with 12e3384ea0d8512aef2458c12e56a8bc08347655, covering the requirement from this issue. The targets to the new LoadBalancer object is an array of values of any type. That means it can be as simple as a string like before, or an object defined by user. For each target, weight and capacity can be specified by using user provided callbacks that take in a target value and return the target's weight and capacity. The new API have added some new features as well, such as the capacity of each target limiting the maximum number of connections to a target.
Here's a sample using the new LoadBalancer API:
var lb = new algo.LoadBalancer(
[
{ addr: 'localhost:8000', w: 0.5 },
{ addr: 'localhost:8001', w: 1.0 },
{ addr: 'localhost:8002', w: 1.0 },
], {
algorithm: 'round-robin',
weight: t => t.w,
}
)
var $conn
pipy.listen(8080, $=>$
.demuxHTTP().to($=>$
.muxHTTP(() => $conn = lb.allocate()).to($=>$
.connect(() => $conn.target.addr)
)
.onEnd(() => $conn.free())
)
)
new Array(3).fill().forEach(
function (_, i) {
pipy.listen(8000 + i, $=>$
.serveHTTP(
new Message(`hi ${i}!\n`)
)
)
}
)
Please feel free to give it a try and give us feedback @sixinyiyu
Discussion
for example:
Improvement example: