mirror of
				https://github.com/nsnail/Ocelot.git
				synced 2025-11-04 09:15:27 +08:00 
			
		
		
		
	wip fake consul provider
This commit is contained in:
		@@ -13,6 +13,7 @@ namespace Ocelot.LoadBalancer.LoadBalancers
 | 
			
		||||
        private readonly Func<Task<List<Service>>> _services;
 | 
			
		||||
        private readonly List<Lease> _leases;
 | 
			
		||||
        private readonly string _serviceName;
 | 
			
		||||
        private static readonly object _syncLock = new object();
 | 
			
		||||
 | 
			
		||||
        public LeastConnectionLoadBalancer(Func<Task<List<Service>>> services, string serviceName)
 | 
			
		||||
        {
 | 
			
		||||
@@ -35,32 +36,38 @@ namespace Ocelot.LoadBalancer.LoadBalancers
 | 
			
		||||
                return new ErrorResponse<HostAndPort>(new List<Error>() { new ServicesAreEmptyError($"services were empty for {_serviceName}") });
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            //todo - maybe this should be moved somewhere else...? Maybe on a repeater on seperate thread? loop every second and update or something?
 | 
			
		||||
            UpdateServices(services);
 | 
			
		||||
            lock(_syncLock)
 | 
			
		||||
            {        
 | 
			
		||||
                //todo - maybe this should be moved somewhere else...? Maybe on a repeater on seperate thread? loop every second and update or something?
 | 
			
		||||
                UpdateServices(services);
 | 
			
		||||
 | 
			
		||||
            var leaseWithLeastConnections = GetLeaseWithLeastConnections();
 | 
			
		||||
                var leaseWithLeastConnections = GetLeaseWithLeastConnections();
 | 
			
		||||
 | 
			
		||||
            _leases.Remove(leaseWithLeastConnections);
 | 
			
		||||
                _leases.Remove(leaseWithLeastConnections);
 | 
			
		||||
 | 
			
		||||
            leaseWithLeastConnections = AddConnection(leaseWithLeastConnections);
 | 
			
		||||
                leaseWithLeastConnections = AddConnection(leaseWithLeastConnections);
 | 
			
		||||
 | 
			
		||||
            _leases.Add(leaseWithLeastConnections);
 | 
			
		||||
 | 
			
		||||
            return new OkResponse<HostAndPort>(new HostAndPort(leaseWithLeastConnections.HostAndPort.DownstreamHost, leaseWithLeastConnections.HostAndPort.DownstreamPort));
 | 
			
		||||
                _leases.Add(leaseWithLeastConnections);
 | 
			
		||||
            
 | 
			
		||||
                return new OkResponse<HostAndPort>(new HostAndPort(leaseWithLeastConnections.HostAndPort.DownstreamHost, leaseWithLeastConnections.HostAndPort.DownstreamPort));
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public Response Release(HostAndPort hostAndPort)
 | 
			
		||||
        {
 | 
			
		||||
            var matchingLease = _leases.FirstOrDefault(l => l.HostAndPort.DownstreamHost == hostAndPort.DownstreamHost
 | 
			
		||||
                && l.HostAndPort.DownstreamPort == hostAndPort.DownstreamPort);
 | 
			
		||||
 | 
			
		||||
            if (matchingLease != null)
 | 
			
		||||
            lock(_syncLock)
 | 
			
		||||
            {
 | 
			
		||||
                var replacementLease = new Lease(hostAndPort, matchingLease.Connections - 1);
 | 
			
		||||
                var matchingLease = _leases.FirstOrDefault(l => l.HostAndPort.DownstreamHost == hostAndPort.DownstreamHost
 | 
			
		||||
                    && l.HostAndPort.DownstreamPort == hostAndPort.DownstreamPort);
 | 
			
		||||
 | 
			
		||||
                _leases.Remove(matchingLease);
 | 
			
		||||
                if (matchingLease != null)
 | 
			
		||||
                {
 | 
			
		||||
                    var replacementLease = new Lease(hostAndPort, matchingLease.Connections - 1);
 | 
			
		||||
 | 
			
		||||
                _leases.Add(replacementLease);
 | 
			
		||||
                    _leases.Remove(matchingLease);
 | 
			
		||||
 | 
			
		||||
                    _leases.Add(replacementLease);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            return new OkResponse();
 | 
			
		||||
 
 | 
			
		||||
@@ -32,8 +32,16 @@ namespace Ocelot.LoadBalancer.LoadBalancers
 | 
			
		||||
 | 
			
		||||
        public Response Add(string key, ILoadBalancer loadBalancer)
 | 
			
		||||
        {
 | 
			
		||||
            _loadBalancers[key] = loadBalancer;
 | 
			
		||||
            return new OkResponse();
 | 
			
		||||
            try
 | 
			
		||||
            {
 | 
			
		||||
                _loadBalancers.Add(key, loadBalancer);
 | 
			
		||||
                return new OkResponse();
 | 
			
		||||
            }
 | 
			
		||||
            catch (System.Exception exception)
 | 
			
		||||
            {
 | 
			
		||||
                Console.WriteLine(exception.StackTrace);
 | 
			
		||||
                throw;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -18,6 +18,7 @@ namespace Ocelot.Middleware
 | 
			
		||||
    using System.Threading.Tasks;
 | 
			
		||||
    using Authorisation.Middleware;
 | 
			
		||||
    using Microsoft.AspNetCore.Http;
 | 
			
		||||
    using Ocelot.Configuration.Provider;
 | 
			
		||||
    using Ocelot.LoadBalancer.Middleware;
 | 
			
		||||
 | 
			
		||||
    public static class OcelotMiddlewareExtensions
 | 
			
		||||
@@ -29,6 +30,7 @@ namespace Ocelot.Middleware
 | 
			
		||||
        /// <returns></returns>
 | 
			
		||||
        public static IApplicationBuilder UseOcelot(this IApplicationBuilder builder)
 | 
			
		||||
        {
 | 
			
		||||
            CreateConfiguration(builder);
 | 
			
		||||
            builder.UseOcelot(new OcelotMiddlewareConfiguration());
 | 
			
		||||
            return builder;
 | 
			
		||||
        }
 | 
			
		||||
@@ -41,6 +43,8 @@ namespace Ocelot.Middleware
 | 
			
		||||
        /// <returns></returns>
 | 
			
		||||
        public static IApplicationBuilder UseOcelot(this IApplicationBuilder builder, OcelotMiddlewareConfiguration middlewareConfiguration)
 | 
			
		||||
        {
 | 
			
		||||
            CreateConfiguration(builder);
 | 
			
		||||
            
 | 
			
		||||
            // This is registered to catch any global exceptions that are not handled
 | 
			
		||||
            builder.UseExceptionHandlerMiddleware();
 | 
			
		||||
 | 
			
		||||
@@ -118,6 +122,18 @@ namespace Ocelot.Middleware
 | 
			
		||||
            return builder;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private static void CreateConfiguration(IApplicationBuilder builder)
 | 
			
		||||
        {
 | 
			
		||||
            var configProvider = (IOcelotConfigurationProvider)builder.ApplicationServices.GetService(typeof(IOcelotConfigurationProvider));
 | 
			
		||||
            
 | 
			
		||||
            var config = configProvider.Get();
 | 
			
		||||
            
 | 
			
		||||
            if(config == null)
 | 
			
		||||
            {
 | 
			
		||||
                throw new Exception("Unable to start Ocelot: configuration was null");
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private static void UseIfNotNull(this IApplicationBuilder builder, Func<HttpContext, Func<Task>, Task> middleware)
 | 
			
		||||
        {
 | 
			
		||||
            if (middleware != null)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user