Ejemplo n.º 1
0
        public static async Task<INode> ConnectAsync(string host, int port, ConnectionPoolConfiguration poolConfiguration, ConnectionConfiguration connectionConfiguration)
        {
            var masterPool = await ConnectionPool.ConnectAsync(host, port, poolConfiguration, connectionConfiguration);
            var infoCommand = Command.From("INFO");
            var response = await masterPool.SendAsync(infoCommand);

            if (response.IsError)
                throw ExceptionBecause.Node.GotErrorResponseFromNodeInfo(host, port);

            if (response.Value.IsArray)
                throw ExceptionBecause.MalformedData.InNodeInfo(host, port, response);

            var slavePoolTasks = response.Value.ToString()
                .Split(new [] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries)
                .Where(s => s.StartsWith("slave"))
                .Select(slaveInfo =>
                {
                    var components = slaveInfo.Split(',');

                    if (components.Length != 5)
                        throw ExceptionBecause.MalformedData.InMasterSlaveInfo(host, port, slaveInfo);

                    var nodeHost = components[2];
                    var nodePort = int.Parse(components[3]);

                    return ConnectionPool.ConnectAsync(nodeHost, nodePort, poolConfiguration, connectionConfiguration);
                });

            var slavePools = await Task.WhenAll(slavePoolTasks);

            return new Node(host, port, masterPool, slavePools);
        }
Ejemplo n.º 2
0
        private ConnectionPool(ConnectionPoolConfiguration poolConfiguration, IEnumerable<IConnection> connections)
        {
            _poolConfiguration = poolConfiguration;

            foreach (var connection in connections)
                _connections.Enqueue(connection);

            _semaphore = new SemaphoreSlim(_connections.Count);
        }
Ejemplo n.º 3
0
        public static async Task<IConnectionPool> ConnectAsync(string host, int port, ConnectionPoolConfiguration poolConfiguration, ConnectionConfiguration connectionConfiguration)
        {
            var tasks = new List<Task<IConnection>>();

            for (var i = 0; i < poolConfiguration.ConnectionsPerPool; i++)
                tasks.Add(Connection.ConnectAsync(host, port, connectionConfiguration));

            var connections = await Task.WhenAll(tasks);

            return new ConnectionPool(poolConfiguration, connections);
        }