Example #1
0
        public void Compare_to_should_return_positive_value_if_less_connection_than_another(double anotherActiveConnections)
        {
            // Arrange
            var anotherMetric  = new ActiveConnectionMetric(anotherActiveConnections);
            var expectedResult = GetExpectedResult(anotherActiveConnections);

            // Act
            var result = _sut.CompareTo(anotherMetric);

            // Assert
            Assert.Equal(expectedResult, result);
        }
Example #2
0
        public Result <Uri> Execute(IEnumerable <IReadonlyMicroserviceInstance> instances)
        {
            if (instances == null || !instances.Any())
            {
                return(Result <Uri> .Failure(Messages.CanNotExecuteLoadBalancingStrategyForZeroAvailableInstances()));
            }

            IInstanceMetric bestMetric = new ActiveConnectionMetric(double.MaxValue);
            IReadonlyMicroserviceInstance bestInstance = null;

            foreach (var instance in instances)
            {
                if (TryGetActiveConnectionMetric(instance, out var metric))
                {
                    if (bestMetric.CompareTo(metric) >= 0)
                    {
                        bestMetric   = metric;
                        bestInstance = instance;
                    }
                }
            }

            return(bestInstance == null ?
                   Result <Uri> .Failure(Messages.CanNotFindSuitableTarget(instances)) :
                   Result <Uri> .Success(bestInstance.Uri));
        }