mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-22 14:02:49 +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
|
||||
{
|
||||
private readonly int _expiryInMs;
|
||||
private readonly int _keyExpiryInMs;
|
||||
private readonly string _key;
|
||||
private readonly ILoadBalancer _loadBalancer;
|
||||
private readonly ConcurrentDictionary<string, StickySession> _stored;
|
||||
private readonly Timer _timer;
|
||||
private bool _expiring;
|
||||
|
||||
public CookieStickySessions(ILoadBalancer loadBalancer, string key, int expiryInMs)
|
||||
public CookieStickySessions(ILoadBalancer loadBalancer, string key, int keyExpiryInMs, int expiryPeriodInMs)
|
||||
{
|
||||
_key = key;
|
||||
_expiryInMs = expiryInMs;
|
||||
_keyExpiryInMs = keyExpiryInMs;
|
||||
_loadBalancer = loadBalancer;
|
||||
_stored = new ConcurrentDictionary<string, StickySession>();
|
||||
_timer = new Timer(x =>
|
||||
@ -37,7 +37,7 @@ namespace Ocelot.LoadBalancer.LoadBalancers
|
||||
Expire();
|
||||
|
||||
_expiring = false;
|
||||
}, null, 0, 50);
|
||||
}, null, 0, expiryPeriodInMs);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
@ -53,7 +53,7 @@ namespace Ocelot.LoadBalancer.LoadBalancers
|
||||
{
|
||||
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;
|
||||
|
||||
@ -69,7 +69,7 @@ namespace Ocelot.LoadBalancer.LoadBalancers
|
||||
|
||||
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);
|
||||
|
@ -7,6 +7,7 @@ namespace Ocelot.LoadBalancer.LoadBalancers
|
||||
public class LoadBalancerFactory : ILoadBalancerFactory
|
||||
{
|
||||
private readonly IServiceDiscoveryProviderFactory _serviceProviderFactory;
|
||||
private const int ExpiryPeriodInMs = 50;
|
||||
|
||||
public LoadBalancerFactory(IServiceDiscoveryProviderFactory serviceProviderFactory)
|
||||
{
|
||||
@ -25,7 +26,7 @@ namespace Ocelot.LoadBalancer.LoadBalancers
|
||||
return new LeastConnection(async () => await serviceProvider.Get(), reRoute.ServiceName);
|
||||
case nameof(CookieStickySessions):
|
||||
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:
|
||||
return new NoLoadBalancer(await serviceProvider.Get());
|
||||
}
|
||||
|
@ -28,7 +28,7 @@ namespace Ocelot.UnitTests.LoadBalancer
|
||||
{
|
||||
_loadBalancer = new Mock<ILoadBalancer>();
|
||||
const int defaultExpiryInMs = 100;
|
||||
_stickySessions = new CookieStickySessions(_loadBalancer.Object, "sessionid", defaultExpiryInMs);
|
||||
_stickySessions = new CookieStickySessions(_loadBalancer.Object, "sessionid", defaultExpiryInMs, 1);
|
||||
_downstreamContext = new DownstreamContext(new DefaultHttpContext());
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user