Extend OcelotBuilder with overloads of the AddCustomLoadBalancer registration helper method

This commit is contained in:
David Lievrouw
2019-07-10 19:36:01 +02:00
parent de012a0794
commit 80ab5f6a91
4 changed files with 103 additions and 10 deletions

View File

@ -3,6 +3,9 @@ using Microsoft.Extensions.DependencyInjection;
using Ocelot.Middleware.Multiplexer;
using System;
using System.Net.Http;
using Ocelot.Configuration;
using Ocelot.LoadBalancer.LoadBalancers;
using Ocelot.ServiceDiscovery.Providers;
namespace Ocelot.DependencyInjection
{
@ -25,6 +28,23 @@ namespace Ocelot.DependencyInjection
IOcelotBuilder AddTransientDefinedAggregator<T>()
where T : class, IDefinedAggregator;
IOcelotBuilder AddCustomLoadBalancer<T>()
where T : ILoadBalancer, new();
IOcelotBuilder AddCustomLoadBalancer<T>(Func<T> loadBalancerFactoryFunc)
where T : ILoadBalancer;
IOcelotBuilder AddCustomLoadBalancer<T>(Func<IServiceProvider, T> loadBalancerFactoryFunc)
where T : ILoadBalancer;
IOcelotBuilder AddCustomLoadBalancer<T>(
Func<DownstreamReRoute, IServiceDiscoveryProvider, T> loadBalancerFactoryFunc)
where T : ILoadBalancer;
IOcelotBuilder AddCustomLoadBalancer<T>(
Func<IServiceProvider, DownstreamReRoute, IServiceDiscoveryProvider, T> loadBalancerFactoryFunc)
where T : ILoadBalancer;
IOcelotBuilder AddConfigPlaceholders();
}
}

View File

@ -1,3 +1,5 @@
using Ocelot.ServiceDiscovery.Providers;
using Ocelot.Configuration.ChangeTracking;
namespace Ocelot.DependencyInjection
@ -173,6 +175,47 @@ namespace Ocelot.DependencyInjection
return this;
}
public IOcelotBuilder AddCustomLoadBalancer<T>()
where T : ILoadBalancer, new()
{
AddCustomLoadBalancer((provider, reRoute, serviceDiscoveryProvider) => new T());
return this;
}
public IOcelotBuilder AddCustomLoadBalancer<T>(Func<T> loadBalancerFactoryFunc)
where T : ILoadBalancer
{
AddCustomLoadBalancer((provider, reRoute, serviceDiscoveryProvider) =>
loadBalancerFactoryFunc());
return this;
}
public IOcelotBuilder AddCustomLoadBalancer<T>(Func<IServiceProvider, T> loadBalancerFactoryFunc)
where T : ILoadBalancer
{
AddCustomLoadBalancer((provider, reRoute, serviceDiscoveryProvider) =>
loadBalancerFactoryFunc(provider));
return this;
}
public IOcelotBuilder AddCustomLoadBalancer<T>(Func<DownstreamReRoute, IServiceDiscoveryProvider, T> loadBalancerFactoryFunc)
where T : ILoadBalancer
{
AddCustomLoadBalancer((provider, reRoute, serviceDiscoveryProvider) =>
loadBalancerFactoryFunc(reRoute, serviceDiscoveryProvider));
return this;
}
public IOcelotBuilder AddCustomLoadBalancer<T>(Func<IServiceProvider, DownstreamReRoute, IServiceDiscoveryProvider, T> loadBalancerFactoryFunc)
where T : ILoadBalancer
{
Services.AddSingleton<ILoadBalancerCreator>(provider =>
new DelegateInvokingLoadBalancerCreator<T>(
(reRoute, serviceDiscoveryProvider) =>
loadBalancerFactoryFunc(provider, reRoute, serviceDiscoveryProvider)));
return this;
}
private void AddSecurity()
{
Services.TryAddSingleton<ISecurityOptionsCreator, SecurityOptionsCreator>();

View File

@ -0,0 +1,25 @@
namespace Ocelot.LoadBalancer.LoadBalancers
{
using System;
using Ocelot.Configuration;
using Ocelot.ServiceDiscovery.Providers;
public class DelegateInvokingLoadBalancerCreator<T> : ILoadBalancerCreator
where T : ILoadBalancer
{
private readonly Func<DownstreamReRoute, IServiceDiscoveryProvider, ILoadBalancer> _creatorFunc;
public DelegateInvokingLoadBalancerCreator(
Func<DownstreamReRoute, IServiceDiscoveryProvider, ILoadBalancer> creatorFunc)
{
_creatorFunc = creatorFunc;
}
public ILoadBalancer Create(DownstreamReRoute reRoute, IServiceDiscoveryProvider serviceProvider)
{
return _creatorFunc(reRoute, serviceProvider);
}
public string Type => typeof(T).Name;
}
}