#363 added a test to prove rr lb works, this doesnt have a lock so it… (#365)

* #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:
Tom Pallister
2018-05-21 18:46:39 +01:00
committed by GitHub
parent f96adf9583
commit d01720c349
2 changed files with 62 additions and 8 deletions

View File

@ -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)