internal static RedisClient GetRedisClientInternal(string server, long db)
        {
            if (string.IsNullOrEmpty(server))
            {
                throw new ArgumentNullException("server");
            }
            RedisClientPool redisClientPool;

            if (!clientPools.TryGetValue((server + db), out redisClientPool))
            {
                lock (clientPools)
                {
                    if (!clientPools.TryGetValue((server + db), out redisClientPool))
                    {
                        string   host  = server;
                        int      port  = 6379;
                        string[] array = server.Split(new char[]
                        {
                            ':'
                        });
                        if (array.Length > 1)
                        {
                            host = array[0];
                            if (!int.TryParse(array[1], out port))
                            {
                                port = 6379;
                            }
                        }
                        redisClientPool = new RedisClientPool(host, port, db);
                        clientPools.Add((server + db), redisClientPool);
                    }
                }
            }
            return(redisClientPool.GetRedisClient());
        }
示例#2
0
        internal static RedisClient GetRedisClientInternal(string server)
        {
            if (string.IsNullOrEmpty(server))
            {
                throw new ArgumentNullException("server");
            }
            RedisClientPool clientPool;

            if (!clientPools.TryGetValue(server, out clientPool))
            {
                lock (clientPools) {
                    if (!clientPools.TryGetValue(server, out clientPool))
                    {
                        var host = server;
                        var port = 6379;
                        var s    = server.Split(':');
                        if (s.Length > 1)
                        {
                            host = s[0];
                            if (!int.TryParse(s[1], out port))
                            {
                                port = 6379;
                            }
                        }
                        clientPool = new RedisClientPool(host, port);
                        clientPools.Add(server, clientPool);
                    }
                }
            }
            return(clientPool.GetRedisClient());
        }
示例#3
0
 public override TValue AdapterCall <TValue>(CommandPacket cmd, Func <RedisResult, TValue> parse)
 {
     return(TopOwner.LogCall(cmd, () =>
     {
         RedisResult rt = null;
         RedisClientPool pool = null;
         Exception ioex = null;
         using (var rds = GetRedisSocket(cmd))
         {
             pool = (rds as DefaultRedisSocket.TempProxyRedisSocket)._pool;
             try
             {
                 rds.Write(cmd);
                 rt = rds.Read(cmd._flagReadbytes);
             }
             catch (Exception ex)
             {
                 ioex = ex;
             }
         }
         if (ioex != null)
         {
             if (pool?.SetUnavailable(ioex) == true)
             {
             }
             throw ioex;
         }
         return parse(rt);
     }));
 }
示例#4
0
            long GetOrAddClusterTrackingRedirectId(string host, RedisClientPool pool)
            {
                var poolkey = pool.Key;

                //return _sub.RedisSocket.ClientId;
                if (_cli.Adapter.UseType != RedisClient.UseType.Cluster)
                {
                    return(_sub.RedisSocket.ClientId);
                }

                ClusterTrackingInfo tracking = null;

                lock (_clusterTrackingsLock)
                {
                    if (_clusterTrackings.TryGetValue(poolkey, out tracking) == false)
                    {
                        tracking = new ClusterTrackingInfo
                        {
                            Client = new RedisClient(new ConnectionStringBuilder
                            {
                                Host           = host,
                                MaxPoolSize    = 1,
                                Password       = pool._policy._connectionStringBuilder.Password,
                                ClientName     = "client_tracking_redirect",
                                ConnectTimeout = pool._policy._connectionStringBuilder.ConnectTimeout,
                                IdleTimeout    = pool._policy._connectionStringBuilder.IdleTimeout,
                                ReceiveTimeout = pool._policy._connectionStringBuilder.ReceiveTimeout,
                                SendTimeout    = pool._policy._connectionStringBuilder.SendTimeout,
                                Ssl            = pool._policy._connectionStringBuilder.Ssl,
                                User           = pool._policy._connectionStringBuilder.User,
                            })
                        };
                        tracking.Client.Unavailable += (_, e) =>
                        {
                            lock (_dictLock) _dictSort.Clear();
                            _dict.Clear();
                            lock (_clusterTrackingsLock)
                            {
                                if (_clusterTrackings.TryGetValue(e.Pool.Key, out var localTracking))
                                {
                                    _clusterTrackings.Remove(e.Pool.Key);
                                    localTracking.Client.Dispose();
                                }
                            }
                        };
                        tracking.PubSub = tracking.Client.Subscribe("__redis__:invalidate", InValidate) as IPubSubSubscriber;
                        _clusterTrackings.Add(poolkey, tracking);
                    }
                }
                return(tracking.PubSub.RedisSocket.ClientId);
            }
示例#5
0
            public override TValue AdapterCall <TValue>(CommandPacket cmd, Func <RedisResult, TValue> parse)
            {
                if (cmd._keyIndexes.Count > 1) //Multiple key slot values not equal
                {
                    switch (cmd._command)
                    {
                    case "DEL":
                    case "UNLINK":
                        return(cmd._keyIndexes.Select((_, idx) => AdapterCall(new CommandPacket(cmd._command).InputKey(cmd.GetKey(idx)), parse)).Sum(a => a.ConvertTo <long>()).ConvertTo <TValue>());

                    case "MSET":
                        cmd._keyIndexes.ForEach(idx => AdapterCall(new CommandPacket(cmd._command).InputKey(cmd._input[idx].ToInvariantCultureToString()).InputRaw(cmd._input[idx + 1]), parse));
                        return(default);

                    case "MGET":
                        return(cmd._keyIndexes.Select((_, idx) => AdapterCall(new CommandPacket(cmd._command).InputKey(cmd.GetKey(idx)), parse).ConvertTo <object[]>().First()).ToArray().ConvertTo <TValue>());

                    case "PFCOUNT":
                        return(cmd._keyIndexes.Select((_, idx) => AdapterCall(new CommandPacket(cmd._command).InputKey(cmd.GetKey(idx)), parse)).Sum(a => a.ConvertTo <long>()).ConvertTo <TValue>());
                    }
                }
                return(TopOwner.LogCall(cmd, () =>
                {
                    RedisResult rt = null;
                    RedisClientPool pool = null;
                    using (var rds = GetRedisSocket(cmd))
                    {
                        pool = (rds as DefaultRedisSocket.TempProxyRedisSocket)._pool;
                        try
                        {
                            rds.Write(cmd);
                            rt = rds.Read(cmd);
                        }
                        catch (Exception ex)
                        {
                            if (pool?.SetUnavailable(ex) == true)
                            {
                            }
                            throw ex;
                        }
                    }
                    return parse(rt);
                }));
            }
示例#6
0
            public override TValue AdapterCall <TValue>(CommandPacket cmd, Func <RedisResult, TValue> parse)
            {
                if (cmd._keyIndexes.Count > 1) //Multiple key slot values not equal
                {
                    switch (cmd._command)
                    {
                    case "DEL":
                    case "UNLINK":
                        return(cmd._keyIndexes.Select((_, idx) => AdapterCall(cmd._command.InputKey(cmd.GetKey(idx)), parse)).Sum(a => a.ConvertTo <long>()).ConvertTo <TValue>());

                    case "MSET":
                        cmd._keyIndexes.ForEach(idx => AdapterCall(cmd._command.InputKey(cmd._input[idx].ToInvariantCultureToString()).InputRaw(cmd._input[idx + 1]), parse));
                        return(default);

                    case "MGET":
                        return(cmd._keyIndexes.Select((_, idx) =>
                        {
                            var rt = AdapterCall(cmd._command.InputKey(cmd.GetKey(idx)), parse);
                            return rt.ConvertTo <object[]>().FirstOrDefault();
                        }).ToArray().ConvertTo <TValue>());

                    case "PFCOUNT":
                        return(cmd._keyIndexes.Select((_, idx) => AdapterCall(cmd._command.InputKey(cmd.GetKey(idx)), parse)).Sum(a => a.ConvertTo <long>()).ConvertTo <TValue>());
                    }
                }
                return(TopOwner.LogCall(cmd, () =>
                {
                    RedisResult rt = null;
                    RedisClientPool pool = null;
                    var protocolRetry = false;
                    using (var rds = GetRedisSocket(cmd))
                    {
                        pool = (rds as DefaultRedisSocket.TempProxyRedisSocket)._pool;
                        try
                        {
                            if (cmd._clusterMovedAsking)
                            {
                                cmd._clusterMovedAsking = false;
                                var askingCmd = "ASKING".SubCommand(null).FlagReadbytes(false);
                                rds.Write(askingCmd);
                                rds.Read(askingCmd);
                            }
                            rds.Write(cmd);
                            rt = rds.Read(cmd);
                        }
                        catch (ProtocolViolationException)
                        {
                            rds.ReleaseSocket();
                            cmd._protocolErrorTryCount++;
                            if (cmd._protocolErrorTryCount <= pool._policy._connectionStringBuilder.Retry)
                            {
                                protocolRetry = true;
                            }
                            else
                            {
                                if (cmd.IsReadOnlyCommand() == false || cmd._protocolErrorTryCount > 1)
                                {
                                    throw;
                                }
                                protocolRetry = true;
                            }
                        }
                        catch (Exception ex)
                        {
                            if (pool?.SetUnavailable(ex) == true)
                            {
                            }
                            throw;
                        }
                    }
                    if (protocolRetry)
                    {
                        return AdapterCall(cmd, parse);
                    }
                    if (rt.IsError && pool != null)
                    {
                        var moved = ClusterMoved.ParseSimpleError(rt.SimpleError);
                        if (moved != null && cmd._clusterMovedTryCount < 3)
                        {
                            cmd._clusterMovedTryCount++;

                            if (moved.endpoint.StartsWith("127.0.0.1"))
                            {
                                moved.endpoint = $"{DefaultRedisSocket.SplitHost(pool._policy._connectionStringBuilder.Host).Key}:{moved.endpoint.Substring(10)}";
                            }
                            else if (moved.endpoint.StartsWith("localhost", StringComparison.CurrentCultureIgnoreCase))
                            {
                                moved.endpoint = $"{DefaultRedisSocket.SplitHost(pool._policy._connectionStringBuilder.Host).Key}:{moved.endpoint.Substring(10)}";
                            }

                            ConnectionStringBuilder connectionString = pool._policy._connectionStringBuilder.ToString();
                            connectionString.Host = moved.endpoint;
                            RegisterClusterNode(connectionString);

                            if (moved.ismoved)
                            {
                                _slotCache.AddOrUpdate(moved.slot, connectionString.Host, (k1, v1) => connectionString.Host);
                            }

                            if (moved.isask)
                            {
                                cmd._clusterMovedAsking = true;
                            }

                            TopOwner.OnNotice(null, new NoticeEventArgs(NoticeType.Info, null, $"{(cmd.WriteTarget ?? "Not connected").PadRight(21)} > {cmd}\r\n{rt.SimpleError} ", null));
                            return AdapterCall(cmd, parse);
                        }
                    }
                    return parse(rt);
                }));
            }
示例#7
0
            public override TValue AdapterCall <TValue>(CommandPacket cmd, Func <RedisResult, TValue> parse)
            {
                return(TopOwner.LogCall(cmd, () =>
                {
                    RedisResult rt = null;
                    RedisClientPool pool = null;
                    using (var rds = GetRedisSocket(cmd))
                    {
                        pool = (rds as DefaultRedisSocket.TempProxyRedisSocket)._pool;
                        try
                        {
                            if (cmd._clusterMovedAsking)
                            {
                                cmd._clusterMovedAsking = false;
                                rds.Write("ASKING");
                                rds.Read(false);
                            }
                            rds.Write(cmd);
                            rt = rds.Read(cmd._flagReadbytes);
                        }
                        catch (Exception ex)
                        {
                            if (pool?.SetUnavailable(ex) == true)
                            {
                            }
                            throw ex;
                        }
                    }
                    if (rt.IsError && pool != null)
                    {
                        var moved = ClusterMoved.ParseSimpleError(rt.SimpleError);
                        if (moved != null && cmd._clusterMovedTryCount < 3)
                        {
                            cmd._clusterMovedTryCount++;

                            if (moved.endpoint.StartsWith("127.0.0.1"))
                            {
                                moved.endpoint = $"{DefaultRedisSocket.SplitHost(pool._policy._connectionStringBuilder.Host).Key}:{moved.endpoint.Substring(10)}";
                            }
                            else if (moved.endpoint.StartsWith("localhost", StringComparison.CurrentCultureIgnoreCase))
                            {
                                moved.endpoint = $"{DefaultRedisSocket.SplitHost(pool._policy._connectionStringBuilder.Host).Key}:{moved.endpoint.Substring(10)}";
                            }

                            ConnectionStringBuilder connectionString = pool._policy._connectionStringBuilder.ToString();
                            connectionString.Host = moved.endpoint;
                            RegisterClusterNode(connectionString);

                            if (moved.ismoved)
                            {
                                _slotCache.AddOrUpdate(moved.slot, connectionString.Host, (k1, v1) => connectionString.Host);
                            }

                            if (moved.isask)
                            {
                                cmd._clusterMovedAsking = true;
                            }

                            TopOwner.OnNotice(new NoticeEventArgs(NoticeType.Info, null, $"{(cmd.WriteHost ?? "Not connected")} > {cmd}\r\n{rt.SimpleError} ", null));
                            return AdapterCall(cmd, parse);
                        }
                    }
                    rt.IsErrorThrow = TopOwner._isThrowRedisSimpleError;
                    return parse(rt);
                }));
            }
示例#8
0
 public UnavailableEventArgs(string host, RedisClientPool pool)
 {
     this.Host = host;
     this.Pool = pool;
 }
示例#9
0
 public ConnectedEventArgs(string host, RedisClientPool pool, RedisClient cli)
 {
     this.Host   = host;
     this.Pool   = pool;
     this.Client = cli;
 }
示例#10
0
 public ServiceStatckRedisClient(RedisClientPool clientPool) : base(clientPool.host, clientPool.port)
 {
     this.clientPool = clientPool;
 }