mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 17:28:16 +08:00
* #363 added a test to prove rr lb works, this doesnt have a lock so it isnt perfect, not sure what the tradeoff is between a lock and a bit of randomness, can change to have a lock anytie * #363 had a look at other oss roudn robin lbs and they all use a lock so imlemented a lock
This commit is contained in:
@ -10,6 +10,7 @@ namespace Ocelot.LoadBalancer.LoadBalancers
|
||||
public class RoundRobin : ILoadBalancer
|
||||
{
|
||||
private readonly Func<Task<List<Service>>> _services;
|
||||
private readonly object _lock = new object();
|
||||
|
||||
private int _last;
|
||||
|
||||
@ -21,14 +22,17 @@ namespace Ocelot.LoadBalancer.LoadBalancers
|
||||
public async Task<Response<ServiceHostAndPort>> Lease(DownstreamContext downstreamContext)
|
||||
{
|
||||
var services = await _services();
|
||||
if (_last >= services.Count)
|
||||
lock(_lock)
|
||||
{
|
||||
_last = 0;
|
||||
}
|
||||
if (_last >= services.Count)
|
||||
{
|
||||
_last = 0;
|
||||
}
|
||||
|
||||
var next = services[_last];
|
||||
_last++;
|
||||
return new OkResponse<ServiceHostAndPort>(next.HostAndPort);
|
||||
var next = services[_last];
|
||||
_last++;
|
||||
return new OkResponse<ServiceHostAndPort>(next.HostAndPort);
|
||||
}
|
||||
}
|
||||
|
||||
public void Release(ServiceHostAndPort hostAndPort)
|
||||
|
Reference in New Issue
Block a user