mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-23 00:52:51 +08:00
started adding loadbalancers
This commit is contained in:
parent
044b609ea9
commit
6bf2d4677c
@ -4,7 +4,7 @@ echo Packing Ocelot Version %1
|
||||
nuget pack .\Ocelot.nuspec -version %1
|
||||
|
||||
echo Publishing Ocelot
|
||||
nuget push Ocelot.%1.nupkg -ApiKey %2 -Source https://www.nuget.org/api/v2/package
|
||||
nuget push Ocelot.%1.nupkg -ApiKey %2 -Source https://www.nuget.org/api/v2/package
|
||||
|
||||
|
||||
|
||||
|
@ -1 +1 @@
|
||||
{"ReRoutes":[{"DownstreamTemplate":"http://localhost:41879/","UpstreamTemplate":"/","UpstreamHttpMethod":"Get","AuthenticationOptions":{"Provider":null,"ProviderRootUrl":null,"ScopeName":null,"RequireHttps":false,"AdditionalScopes":[],"ScopeSecret":null},"AddHeadersToRequest":{},"AddClaimsToRequest":{},"RouteClaimsRequirement":{},"AddQueriesToRequest":{},"RequestIdKey":null,"FileCacheOptions":{"TtlSeconds":0},"ReRouteIsCaseSensitive":false}],"GlobalConfiguration":{"RequestIdKey":null}}
|
||||
{"ReRoutes":[{"DownstreamPathTemplate":"/","UpstreamTemplate":"/","UpstreamHttpMethod":"Get","AuthenticationOptions":{"Provider":null,"ProviderRootUrl":null,"ScopeName":null,"RequireHttps":false,"AdditionalScopes":[],"ScopeSecret":null},"AddHeadersToRequest":{},"AddClaimsToRequest":{},"RouteClaimsRequirement":{},"AddQueriesToRequest":{},"RequestIdKey":null,"FileCacheOptions":{"TtlSeconds":0},"ReRouteIsCaseSensitive":false,"ServiceName":null,"DownstreamScheme":"http","DownstreamHost":"localhost","DownstreamPort":41879}],"GlobalConfiguration":{"RequestIdKey":null,"ServiceDiscoveryProvider":{"Provider":null,"Address":null}}}
|
81
test/Ocelot.UnitTests/RoundRobinTests.cs
Normal file
81
test/Ocelot.UnitTests/RoundRobinTests.cs
Normal file
@ -0,0 +1,81 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using Ocelot.Values;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests
|
||||
{
|
||||
public class RoundRobinTests
|
||||
{
|
||||
private readonly RoundRobin _roundRobin;
|
||||
private readonly List<HostAndPort> _hostAndPorts;
|
||||
|
||||
public RoundRobinTests()
|
||||
{
|
||||
_hostAndPorts = new List<HostAndPort>
|
||||
{
|
||||
new HostAndPort("127.0.0.1", 5000),
|
||||
new HostAndPort("127.0.0.1", 5001),
|
||||
new HostAndPort("127.0.0.1", 5001)
|
||||
};
|
||||
|
||||
_roundRobin = new RoundRobin(_hostAndPorts);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_get_next_address()
|
||||
{
|
||||
var address = _roundRobin.Next();
|
||||
address.ShouldBe(_hostAndPorts[0]);
|
||||
address = _roundRobin.Next();
|
||||
address.ShouldBe(_hostAndPorts[1]);
|
||||
address = _roundRobin.Next();
|
||||
address.ShouldBe(_hostAndPorts[2]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_go_back_to_first_address_after_finished_last()
|
||||
{
|
||||
var stopWatch = Stopwatch.StartNew();
|
||||
|
||||
while (stopWatch.ElapsedMilliseconds < 1000)
|
||||
{
|
||||
var address = _roundRobin.Next();
|
||||
address.ShouldBe(_hostAndPorts[0]);
|
||||
address = _roundRobin.Next();
|
||||
address.ShouldBe(_hostAndPorts[1]);
|
||||
address = _roundRobin.Next();
|
||||
address.ShouldBe(_hostAndPorts[2]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public interface ILoadBalancer
|
||||
{
|
||||
HostAndPort Next();
|
||||
}
|
||||
|
||||
public class RoundRobin : ILoadBalancer
|
||||
{
|
||||
private readonly List<HostAndPort> _hostAndPorts;
|
||||
private int _last;
|
||||
|
||||
public RoundRobin(List<HostAndPort> hostAndPorts)
|
||||
{
|
||||
_hostAndPorts = hostAndPorts;
|
||||
}
|
||||
|
||||
public HostAndPort Next()
|
||||
{
|
||||
if (_last >= _hostAndPorts.Count)
|
||||
{
|
||||
_last = 0;
|
||||
}
|
||||
|
||||
var next = _hostAndPorts[_last];
|
||||
_last++;
|
||||
return next;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user