Пример #1
0
        public void should_serve_from_service_with_least_connections()
        {
            var serviceName = "products";

            var availableServices = new List <Service>
            {
                new Service(serviceName, new HostAndPort("127.0.0.1", 80), string.Empty, string.Empty, new string[0]),
                new Service(serviceName, new HostAndPort("127.0.0.2", 80), string.Empty, string.Empty, new string[0]),
                new Service(serviceName, new HostAndPort("127.0.0.3", 80), string.Empty, string.Empty, new string[0])
            };

            _services        = availableServices;
            _leastConnection = new LeastConnectionLoadBalancer(() => Task.FromResult(_services), serviceName);

            var response = _leastConnection.Lease().Result;

            response.Data.DownstreamHost.ShouldBe(availableServices[0].HostAndPort.DownstreamHost);

            response = _leastConnection.Lease().Result;

            response.Data.DownstreamHost.ShouldBe(availableServices[1].HostAndPort.DownstreamHost);

            response = _leastConnection.Lease().Result;

            response.Data.DownstreamHost.ShouldBe(availableServices[2].HostAndPort.DownstreamHost);
        }
Пример #2
0
        private async Task LeaseDelayAndRelease()
        {
            var hostAndPort = await _leastConnection.Lease();

            await Task.Delay(_random.Next(1, 100));

            _leastConnection.Release(hostAndPort.Data);
        }
Пример #3
0
        public void should_release_connection()
        {
            var serviceName = "products";

            var availableServices = new List <Service>
            {
                new Service(serviceName, new HostAndPort("127.0.0.1", 80), string.Empty, string.Empty, new string[0]),
                new Service(serviceName, new HostAndPort("127.0.0.2", 80), string.Empty, string.Empty, new string[0]),
            };

            _services        = availableServices;
            _leastConnection = new LeastConnectionLoadBalancer(() => Task.FromResult(_services), serviceName);

            var response = _leastConnection.Lease().Result;

            response.Data.DownstreamHost.ShouldBe(availableServices[0].HostAndPort.DownstreamHost);

            response = _leastConnection.Lease().Result;

            response.Data.DownstreamHost.ShouldBe(availableServices[1].HostAndPort.DownstreamHost);

            response = _leastConnection.Lease().Result;

            response.Data.DownstreamHost.ShouldBe(availableServices[0].HostAndPort.DownstreamHost);

            response = _leastConnection.Lease().Result;

            response.Data.DownstreamHost.ShouldBe(availableServices[1].HostAndPort.DownstreamHost);

            //release this so 2 should have 1 connection and we should get 2 back as our next host and port
            _leastConnection.Release(availableServices[1].HostAndPort);

            response = _leastConnection.Lease().Result;

            response.Data.DownstreamHost.ShouldBe(availableServices[1].HostAndPort.DownstreamHost);
        }