Пример #1
0
        // methods
        /// <inheritdoc/>
        public IEnumerable <ServerDescription> SelectServers(ClusterDescription cluster, IEnumerable <ServerDescription> servers)
        {
            var list = servers.ToList();

            switch (list.Count)
            {
            case 0:
            case 1:
                return(list);

            default:
            {
                // Follow the "Power of Two Choices" approach
                // https://web.archive.org/web/20191212194243/https://www.nginx.com/blog/nginx-power-of-two-choices-load-balancing-algorithm/
                var index1 = ThreadStaticRandom.Next(list.Count);
                var index2 = (index1 + 1 + ThreadStaticRandom.Next(list.Count - 1)) % list.Count;

                var endpoint1 = list[index1].EndPoint;
                var endpoint2 = list[index2].EndPoint;
                var server1   = _clusterableServers.First(s => EndPointHelper.Equals(s.Description.EndPoint, endpoint1));
                var server2   = _clusterableServers.First(s => EndPointHelper.Equals(s.Description.EndPoint, endpoint2));

                var selectedServer = server1.OutstandingOperationsCount < server2.OutstandingOperationsCount ? server1 : server2;

                return(new[] { selectedServer.Description });
            }
            }
        }
        private PooledConnection ChooseAvailableConnection()
        {
            // if the pool is not full return null (so a new connection will be created)
            if (_connections.Count < _settings.MaxConnections)
            {
                return(null);
            }

            // distribute load randomly among the connections
            var index = ThreadStaticRandom.Next(_connections.Count);

            return(_connections[index]);
        }
Пример #3
0
        // methods
        /// <inheritdoc/>
        public IEnumerable <ServerDescription> SelectServers(ClusterDescription cluster, IEnumerable <ServerDescription> servers)
        {
            var list = servers.ToList();

            switch (list.Count)
            {
            case 0:
            case 1:
                return(list);

            default:
                var index = ThreadStaticRandom.Next(list.Count);
                return(new[] { list[index] });
            }
        }
Пример #4
0
 public Random() : this(ThreadStaticRandom.Next())
 {
 }