Ejemplo n.º 1
0
        public void ReleaseConnection(RedisConnection2 conn)
        {
            lock (_lock)
                FreeConnections.Enqueue(conn);

            bool isAsync = false;

            if (GetConnectionAsyncQueue.Count > 0)
            {
                TaskCompletionSource <RedisConnection2> tcs = null;
                lock (_lock_GetConnectionQueue)
                    if (GetConnectionAsyncQueue.Count > 0)
                    {
                        tcs = GetConnectionAsyncQueue.Dequeue();
                    }
                if (isAsync = (tcs != null))
                {
                    tcs.SetResult(GetConnectionAsync().Result);
                }
            }
            if (isAsync == false && GetConnectionQueue.Count > 0)
            {
                ManualResetEventSlim wait = null;
                lock (_lock_GetConnectionQueue)
                    if (GetConnectionQueue.Count > 0)
                    {
                        wait = GetConnectionQueue.Dequeue();
                    }
                if (wait != null)
                {
                    wait.Set();
                }
            }
        }
Ejemplo n.º 2
0
        private RedisConnection2 GetFreeConnection()
        {
            RedisConnection2 conn = null;

            if (FreeConnections.Count > 0)
            {
                lock (_lock)
                    if (FreeConnections.Count > 0)
                    {
                        conn = FreeConnections.Dequeue();
                    }
            }
            if (conn == null && AllConnections.Count < _poolsize)
            {
                lock (_lock)
                    if (AllConnections.Count < _poolsize)
                    {
                        conn = new RedisConnection2();
                        AllConnections.Add(conn);
                    }
                if (conn != null)
                {
                    conn.Pool = this;
                    var ips = Dns.GetHostAddresses(_ip);
                    if (ips.Length == 0)
                    {
                        throw new Exception($"无法解析“{_ip}”");
                    }
                    conn.Client            = new RedisClient(new IPEndPoint(ips[0], _port), _ssl, 1000, _writebuffer);
                    conn.Client.Connected += Connected;
                }
            }
            return(conn);
        }