mirror of
				https://github.com/nsnail/Ocelot.git
				synced 2025-11-04 10:15:27 +08:00 
			
		
		
		
	@@ -29,7 +29,7 @@ namespace Ocelot.LoadBalancer.LoadBalancers
 | 
				
			|||||||
                    var bus = new InMemoryBus<StickySession>();
 | 
					                    var bus = new InMemoryBus<StickySession>();
 | 
				
			||||||
                    return new CookieStickySessions(loadBalancer, reRoute.LoadBalancerOptions.Key, reRoute.LoadBalancerOptions.ExpiryInMs, bus);
 | 
					                    return new CookieStickySessions(loadBalancer, reRoute.LoadBalancerOptions.Key, reRoute.LoadBalancerOptions.ExpiryInMs, bus);
 | 
				
			||||||
                default:
 | 
					                default:
 | 
				
			||||||
                    return new NoLoadBalancer(await serviceProvider.Get());
 | 
					                    return new NoLoadBalancer(async () => await serviceProvider.Get());
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,4 +1,5 @@
 | 
				
			|||||||
using System.Collections.Generic;
 | 
					using System;
 | 
				
			||||||
 | 
					using System.Collections.Generic;
 | 
				
			||||||
using System.Linq;
 | 
					using System.Linq;
 | 
				
			||||||
using System.Threading.Tasks;
 | 
					using System.Threading.Tasks;
 | 
				
			||||||
using Ocelot.Middleware;
 | 
					using Ocelot.Middleware;
 | 
				
			||||||
@@ -9,22 +10,23 @@ namespace Ocelot.LoadBalancer.LoadBalancers
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
    public class NoLoadBalancer : ILoadBalancer
 | 
					    public class NoLoadBalancer : ILoadBalancer
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        private readonly List<Service> _services;
 | 
					        private readonly Func<Task<List<Service>>> _services;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        public NoLoadBalancer(List<Service> services)
 | 
					        public NoLoadBalancer(Func<Task<List<Service>>> services)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            _services = services;
 | 
					            _services = services;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        public async Task<Response<ServiceHostAndPort>> Lease(DownstreamContext downstreamContext)
 | 
					        public async Task<Response<ServiceHostAndPort>> Lease(DownstreamContext downstreamContext)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
 | 
					            var services = await _services();
 | 
				
			||||||
            //todo first or default could be null..
 | 
					            //todo first or default could be null..
 | 
				
			||||||
            if (_services == null || _services.Count == 0)
 | 
					            if (services == null || services.Count == 0)
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                return new ErrorResponse<ServiceHostAndPort>(new ServicesAreEmptyError("There were no services in NoLoadBalancer"));
 | 
					                return new ErrorResponse<ServiceHostAndPort>(new ServicesAreEmptyError("There were no services in NoLoadBalancer"));
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            var service = await Task.FromResult(_services.FirstOrDefault());
 | 
					            var service = await Task.FromResult(services.FirstOrDefault());
 | 
				
			||||||
            return new OkResponse<ServiceHostAndPort>(service.HostAndPort);
 | 
					            return new OkResponse<ServiceHostAndPort>(service.HostAndPort);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -20,13 +20,13 @@ namespace Ocelot.LoadBalancer.LoadBalancers
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        public async Task<Response<ServiceHostAndPort>> Lease(DownstreamContext downstreamContext)
 | 
					        public async Task<Response<ServiceHostAndPort>> Lease(DownstreamContext downstreamContext)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            var services = await _services.Invoke();
 | 
					            var services = await _services();
 | 
				
			||||||
            if (_last >= services.Count)
 | 
					            if (_last >= services.Count)
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                _last = 0;
 | 
					                _last = 0;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            var next = await Task.FromResult(services[_last]);
 | 
					            var next = services[_last];
 | 
				
			||||||
            _last++;
 | 
					            _last++;
 | 
				
			||||||
            return new OkResponse<ServiceHostAndPort>(next.HostAndPort);
 | 
					            return new OkResponse<ServiceHostAndPort>(next.HostAndPort);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,4 +1,6 @@
 | 
				
			|||||||
 | 
					using System;
 | 
				
			||||||
using System.Collections.Generic;
 | 
					using System.Collections.Generic;
 | 
				
			||||||
 | 
					using System.Threading.Tasks;
 | 
				
			||||||
using Microsoft.AspNetCore.Http;
 | 
					using Microsoft.AspNetCore.Http;
 | 
				
			||||||
using Ocelot.LoadBalancer.LoadBalancers;
 | 
					using Ocelot.LoadBalancer.LoadBalancers;
 | 
				
			||||||
using Ocelot.Middleware;
 | 
					using Ocelot.Middleware;
 | 
				
			||||||
@@ -16,6 +18,12 @@ namespace Ocelot.UnitTests.LoadBalancer
 | 
				
			|||||||
        private NoLoadBalancer _loadBalancer;
 | 
					        private NoLoadBalancer _loadBalancer;
 | 
				
			||||||
        private Response<ServiceHostAndPort> _result;
 | 
					        private Response<ServiceHostAndPort> _result;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        public NoLoadBalancerTests()
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            _services = new List<Service>();
 | 
				
			||||||
 | 
					            _loadBalancer = new NoLoadBalancer(() => Task.FromResult(_services));
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        [Fact]
 | 
					        [Fact]
 | 
				
			||||||
        public void should_return_host_and_port()
 | 
					        public void should_return_host_and_port()
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
@@ -25,6 +33,7 @@ namespace Ocelot.UnitTests.LoadBalancer
 | 
				
			|||||||
            {
 | 
					            {
 | 
				
			||||||
                new Service("product", hostAndPort, string.Empty, string.Empty, new string[0])
 | 
					                new Service("product", hostAndPort, string.Empty, string.Empty, new string[0])
 | 
				
			||||||
            };
 | 
					            };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            this.Given(x => x.GivenServices(services))
 | 
					            this.Given(x => x.GivenServices(services))
 | 
				
			||||||
                .When(x => x.WhenIGetTheNextHostAndPort())
 | 
					                .When(x => x.WhenIGetTheNextHostAndPort())
 | 
				
			||||||
                .Then(x => x.ThenTheHostAndPortIs(hostAndPort))
 | 
					                .Then(x => x.ThenTheHostAndPortIs(hostAndPort))
 | 
				
			||||||
@@ -34,25 +43,43 @@ namespace Ocelot.UnitTests.LoadBalancer
 | 
				
			|||||||
        [Fact]
 | 
					        [Fact]
 | 
				
			||||||
        public void should_return_error_if_no_services()
 | 
					        public void should_return_error_if_no_services()
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            var services = new List<Service>();
 | 
					            this.When(x => x.WhenIGetTheNextHostAndPort())
 | 
				
			||||||
 | 
					 | 
				
			||||||
            this.Given(x => x.GivenServices(services))
 | 
					 | 
				
			||||||
                .When(x => x.WhenIGetTheNextHostAndPort())
 | 
					 | 
				
			||||||
                .Then(x => x.ThenThereIsAnError())
 | 
					                .Then(x => x.ThenThereIsAnError())
 | 
				
			||||||
                .BDDfy();
 | 
					                .BDDfy();
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        [Fact]
 | 
				
			||||||
 | 
					        public void should_return_error_if_no_services_then_when_services_available_return_host_and_port()
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            var hostAndPort = new ServiceHostAndPort("127.0.0.1", 80);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            var services = new List<Service>
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                new Service("product", hostAndPort, string.Empty, string.Empty, new string[0])
 | 
				
			||||||
 | 
					            };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            this.Given(_ => WhenIGetTheNextHostAndPort())
 | 
				
			||||||
 | 
					                .And(_ => ThenThereIsAnError())
 | 
				
			||||||
 | 
					                .And(_ => GivenServices(services))
 | 
				
			||||||
 | 
					                .When(_ => WhenIGetTheNextHostAndPort())
 | 
				
			||||||
 | 
					                .Then(_ => ThenTheHostAndPortIs(hostAndPort))
 | 
				
			||||||
 | 
					                .BDDfy();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        [Fact]
 | 
					        [Fact]
 | 
				
			||||||
        public void should_return_error_if_null_services()
 | 
					        public void should_return_error_if_null_services()
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            List<Service> services = null;
 | 
					            this.Given(x => x.GivenServicesAreNull())
 | 
				
			||||||
 | 
					 | 
				
			||||||
            this.Given(x => x.GivenServices(services))
 | 
					 | 
				
			||||||
                .When(x => x.WhenIGetTheNextHostAndPort())
 | 
					                .When(x => x.WhenIGetTheNextHostAndPort())
 | 
				
			||||||
                .Then(x => x.ThenThereIsAnError())
 | 
					                .Then(x => x.ThenThereIsAnError())
 | 
				
			||||||
                .BDDfy();
 | 
					                .BDDfy();
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        private void GivenServicesAreNull()
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            _loadBalancer = new NoLoadBalancer(() => Task.FromResult((List<Service>)null));
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        private void ThenThereIsAnError()
 | 
					        private void ThenThereIsAnError()
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            _result.IsError.ShouldBeTrue();
 | 
					            _result.IsError.ShouldBeTrue();
 | 
				
			||||||
@@ -60,12 +87,11 @@ namespace Ocelot.UnitTests.LoadBalancer
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        private void GivenServices(List<Service> services)
 | 
					        private void GivenServices(List<Service> services)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            _services = services;
 | 
					            _services.AddRange(services);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        private void WhenIGetTheNextHostAndPort()
 | 
					        private void WhenIGetTheNextHostAndPort()
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            _loadBalancer = new NoLoadBalancer(_services);
 | 
					 | 
				
			||||||
            _result = _loadBalancer.Lease(new DownstreamContext(new DefaultHttpContext())).Result;
 | 
					            _result = _loadBalancer.Lease(new DownstreamContext(new DefaultHttpContext())).Result;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user