mirror of
				https://github.com/nsnail/Ocelot.git
				synced 2025-11-04 09:15:27 +08:00 
			
		
		
		
	RoundRobin loadblancer cause config error https://github.com/TomPallister/Ocelot/issues/103
This commit is contained in:
		@@ -19,7 +19,7 @@ namespace Ocelot.LoadBalancer.LoadBalancers
 | 
			
		||||
            switch (reRoute.LoadBalancer)
 | 
			
		||||
            {
 | 
			
		||||
                case "RoundRobin":
 | 
			
		||||
                    return new RoundRobinLoadBalancer(await serviceProvider.Get());
 | 
			
		||||
                    return new RoundRobinLoadBalancer(async () => await serviceProvider.Get());
 | 
			
		||||
                case "LeastConnection":
 | 
			
		||||
                    return new LeastConnectionLoadBalancer(async () => await serviceProvider.Get(), reRoute.ServiceProviderConfiguraion.ServiceName);
 | 
			
		||||
                default:
 | 
			
		||||
 
 | 
			
		||||
@@ -2,27 +2,31 @@
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
using Ocelot.Responses;
 | 
			
		||||
using Ocelot.Values;
 | 
			
		||||
using System;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.LoadBalancer.LoadBalancers
 | 
			
		||||
{
 | 
			
		||||
    public class RoundRobinLoadBalancer : ILoadBalancer
 | 
			
		||||
    {
 | 
			
		||||
        private readonly List<Service> _services;
 | 
			
		||||
        private readonly Func<Task<List<Service>>> _services;
 | 
			
		||||
 | 
			
		||||
        private int _last;
 | 
			
		||||
 | 
			
		||||
        public RoundRobinLoadBalancer(List<Service> services)
 | 
			
		||||
        public RoundRobinLoadBalancer(Func<Task<List<Service>>> services)
 | 
			
		||||
        {
 | 
			
		||||
            _services = services;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        public async Task<Response<HostAndPort>> Lease()
 | 
			
		||||
        {
 | 
			
		||||
            if (_last >= _services.Count)
 | 
			
		||||
            var services = await _services.Invoke();
 | 
			
		||||
            if (_last >= services.Count)
 | 
			
		||||
            {
 | 
			
		||||
                _last = 0;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            var next = await Task.FromResult(_services[_last]);
 | 
			
		||||
            var next = await Task.FromResult(services[_last]);
 | 
			
		||||
            _last++;
 | 
			
		||||
            return new OkResponse<HostAndPort>(next.HostAndPort);
 | 
			
		||||
        }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user