mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-22 06:22:50 +08:00
pass expiry period into cookie thing to make this more testable
This commit is contained in:
parent
003fff8b24
commit
2f8d857731
@ -12,17 +12,17 @@ namespace Ocelot.LoadBalancer.LoadBalancers
|
|||||||
|
|
||||||
public class CookieStickySessions : ILoadBalancer, IDisposable
|
public class CookieStickySessions : ILoadBalancer, IDisposable
|
||||||
{
|
{
|
||||||
private readonly int _expiryInMs;
|
private readonly int _keyExpiryInMs;
|
||||||
private readonly string _key;
|
private readonly string _key;
|
||||||
private readonly ILoadBalancer _loadBalancer;
|
private readonly ILoadBalancer _loadBalancer;
|
||||||
private readonly ConcurrentDictionary<string, StickySession> _stored;
|
private readonly ConcurrentDictionary<string, StickySession> _stored;
|
||||||
private readonly Timer _timer;
|
private readonly Timer _timer;
|
||||||
private bool _expiring;
|
private bool _expiring;
|
||||||
|
|
||||||
public CookieStickySessions(ILoadBalancer loadBalancer, string key, int expiryInMs)
|
public CookieStickySessions(ILoadBalancer loadBalancer, string key, int keyExpiryInMs, int expiryPeriodInMs)
|
||||||
{
|
{
|
||||||
_key = key;
|
_key = key;
|
||||||
_expiryInMs = expiryInMs;
|
_keyExpiryInMs = keyExpiryInMs;
|
||||||
_loadBalancer = loadBalancer;
|
_loadBalancer = loadBalancer;
|
||||||
_stored = new ConcurrentDictionary<string, StickySession>();
|
_stored = new ConcurrentDictionary<string, StickySession>();
|
||||||
_timer = new Timer(x =>
|
_timer = new Timer(x =>
|
||||||
@ -37,7 +37,7 @@ namespace Ocelot.LoadBalancer.LoadBalancers
|
|||||||
Expire();
|
Expire();
|
||||||
|
|
||||||
_expiring = false;
|
_expiring = false;
|
||||||
}, null, 0, 50);
|
}, null, 0, expiryPeriodInMs);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
@ -53,7 +53,7 @@ namespace Ocelot.LoadBalancer.LoadBalancers
|
|||||||
{
|
{
|
||||||
var cached = _stored[value];
|
var cached = _stored[value];
|
||||||
|
|
||||||
var updated = new StickySession(cached.HostAndPort, DateTime.UtcNow.AddMilliseconds(_expiryInMs));
|
var updated = new StickySession(cached.HostAndPort, DateTime.UtcNow.AddMilliseconds(_keyExpiryInMs));
|
||||||
|
|
||||||
_stored[value] = updated;
|
_stored[value] = updated;
|
||||||
|
|
||||||
@ -69,7 +69,7 @@ namespace Ocelot.LoadBalancer.LoadBalancers
|
|||||||
|
|
||||||
if (!string.IsNullOrEmpty(value) && !_stored.ContainsKey(value))
|
if (!string.IsNullOrEmpty(value) && !_stored.ContainsKey(value))
|
||||||
{
|
{
|
||||||
_stored[value] = new StickySession(next.Data, DateTime.UtcNow.AddMilliseconds(_expiryInMs));
|
_stored[value] = new StickySession(next.Data, DateTime.UtcNow.AddMilliseconds(_keyExpiryInMs));
|
||||||
}
|
}
|
||||||
|
|
||||||
return new OkResponse<ServiceHostAndPort>(next.Data);
|
return new OkResponse<ServiceHostAndPort>(next.Data);
|
||||||
|
@ -7,6 +7,7 @@ namespace Ocelot.LoadBalancer.LoadBalancers
|
|||||||
public class LoadBalancerFactory : ILoadBalancerFactory
|
public class LoadBalancerFactory : ILoadBalancerFactory
|
||||||
{
|
{
|
||||||
private readonly IServiceDiscoveryProviderFactory _serviceProviderFactory;
|
private readonly IServiceDiscoveryProviderFactory _serviceProviderFactory;
|
||||||
|
private const int ExpiryPeriodInMs = 50;
|
||||||
|
|
||||||
public LoadBalancerFactory(IServiceDiscoveryProviderFactory serviceProviderFactory)
|
public LoadBalancerFactory(IServiceDiscoveryProviderFactory serviceProviderFactory)
|
||||||
{
|
{
|
||||||
@ -25,7 +26,7 @@ namespace Ocelot.LoadBalancer.LoadBalancers
|
|||||||
return new LeastConnection(async () => await serviceProvider.Get(), reRoute.ServiceName);
|
return new LeastConnection(async () => await serviceProvider.Get(), reRoute.ServiceName);
|
||||||
case nameof(CookieStickySessions):
|
case nameof(CookieStickySessions):
|
||||||
var loadBalancer = new RoundRobin(async () => await serviceProvider.Get());
|
var loadBalancer = new RoundRobin(async () => await serviceProvider.Get());
|
||||||
return new CookieStickySessions(loadBalancer, reRoute.LoadBalancerOptions.Key, reRoute.LoadBalancerOptions.ExpiryInMs);
|
return new CookieStickySessions(loadBalancer, reRoute.LoadBalancerOptions.Key, reRoute.LoadBalancerOptions.ExpiryInMs, ExpiryPeriodInMs);
|
||||||
default:
|
default:
|
||||||
return new NoLoadBalancer(await serviceProvider.Get());
|
return new NoLoadBalancer(await serviceProvider.Get());
|
||||||
}
|
}
|
||||||
|
@ -28,7 +28,7 @@ namespace Ocelot.UnitTests.LoadBalancer
|
|||||||
{
|
{
|
||||||
_loadBalancer = new Mock<ILoadBalancer>();
|
_loadBalancer = new Mock<ILoadBalancer>();
|
||||||
const int defaultExpiryInMs = 100;
|
const int defaultExpiryInMs = 100;
|
||||||
_stickySessions = new CookieStickySessions(_loadBalancer.Object, "sessionid", defaultExpiryInMs);
|
_stickySessions = new CookieStickySessions(_loadBalancer.Object, "sessionid", defaultExpiryInMs, 1);
|
||||||
_downstreamContext = new DownstreamContext(new DefaultHttpContext());
|
_downstreamContext = new DownstreamContext(new DefaultHttpContext());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user