Esempio n. 1
0
        /// <summary>
        /// Returns a Read/Write client (The default) using the hosts defined in ReadWriteHosts
        /// </summary>
        /// <returns></returns>
        public IRedisClient GetClient()
        {
            var nextHost = ReadWriteHosts[readWriteHostsIndex++ % ReadWriteHosts.Count];
            var client   = RedisClientFactory.CreateRedisClient(
                nextHost.Host, nextHost.Port);

            if (this.ConnectTimeout != null)
            {
                client.ConnectTimeout = this.ConnectTimeout.Value;
            }

            //Set database to userSpecified if different
            if (Db != RedisNativeClient.DefaultDb)
            {
                client.Db = Db;
            }

            if (nextHost.RequiresAuth)
            {
                client.Password = nextHost.Password;
            }

            client.ConnectionFilter = ConnectionFilter;

            return(client);
        }
        /// <summary>
        /// Returns a ReadOnly client using the hosts defined in ReadOnlyHosts.
        /// </summary>
        /// <returns></returns>
        public virtual IRedisClient GetReadOnlyClient()
        {
            var nextHost = ReadOnlyHosts[readOnlyHostsIndex++ % ReadOnlyHosts.Count];
            var client   = RedisClientFactory.CreateRedisClient(
                nextHost.Host, nextHost.Port);

            if (this.ConnectTimeout != null)
            {
                client.ConnectTimeout = this.ConnectTimeout.Value;
            }

            //Set database to userSpecified if different
            if (Db != RedisNativeClient.DefaultDb)
            {
                client.ChangeDb(Db);
            }

            if (nextHost.RequiresAuth)
            {
                client.Password = nextHost.Password;
            }

            client.NamespacePrefix  = NamespacePrefix;
            client.ConnectionFilter = ConnectionFilter;

            return(client);
        }
Esempio n. 3
0
        /// <summary>
        /// Called within a lock
        /// </summary>
        /// <returns></returns>
        private RedisClient GetInActiveReadClient()
        {
            for (var i = 0; i < readClients.Length; i++)
            {
                var nextIndex = (ReadPoolIndex + i) % readClients.Length;

                //Initialize if not exists
                var existingClient = readClients[nextIndex];
                if (existingClient == null ||
                    existingClient.HadExceptions)
                {
                    if (existingClient != null)
                    {
                        existingClient.DisposeConnection();
                    }

                    var nextHost = ReadOnlyHosts[nextIndex % ReadOnlyHosts.Count];
                    var client   = RedisClientFactory.CreateRedisClient(
                        nextHost.Host, nextHost.Port);

                    client.ClientManager = this;

                    readClients[nextIndex] = client;

                    return(client);
                }

                //look for free one
                if (!readClients[nextIndex].Active)
                {
                    return(readClients[nextIndex]);
                }
            }
            return(null);
        }
        private RedisClient InitNewClient(RedisEndpoint nextHost)
        {
            var client = RedisClientFactory.CreateRedisClient(nextHost);

            client.Id = Interlocked.Increment(ref RedisClientCounter);
            client.ConnectionFilter = ConnectionFilter;
            if (this.ConnectTimeout != null)
            {
                client.ConnectTimeout = this.ConnectTimeout.Value;
            }
            if (this.SocketSendTimeout.HasValue)
            {
                client.SendTimeout = this.SocketSendTimeout.Value;
            }
            if (this.SocketReceiveTimeout.HasValue)
            {
                client.ReceiveTimeout = this.SocketReceiveTimeout.Value;
            }
            if (this.IdleTimeOutSecs.HasValue)
            {
                client.IdleTimeOutSecs = this.IdleTimeOutSecs.Value;
            }
            if (this.NamespacePrefix != null)
            {
                client.NamespacePrefix = NamespacePrefix;
            }
            if (Db != null && client.Db != Db) //Reset database to default if changed
            {
                client.ChangeDb(Db.Value);
            }

            return(client);
        }
Esempio n. 5
0
        /// <summary>
        /// Called within a lock
        /// </summary>
        /// <returns></returns>
        private RedisClient GetInActiveWriteClient()
        {
            var desiredIndex = WritePoolIndex % writeClients.Length;

            //this will loop through all hosts in readClients once even though there are 2 for loops
            //both loops are used to try to get the prefered host according to the round robin algorithm
            for (int x = 0; x < ReadWriteHosts.Count; x++)
            {
                var nextHostIndex = (desiredIndex + x) % ReadWriteHosts.Count;
                var nextHost      = ReadWriteHosts[nextHostIndex];
                for (var i = nextHostIndex; i < writeClients.Length; i += ReadWriteHosts.Count)
                {
                    if (writeClients[i] != null && !writeClients[i].Active && !writeClients[i].HadExceptions &&
                        (!CheckConnected || writeClients[i].IsSocketConnected()))
                    {
                        return(writeClients[i]);
                    }
                    else if (writeClients[i] == null || writeClients[i].HadExceptions)
                    {
                        if (writeClients[i] != null)
                        {
                            writeClients[i].DisposeConnection();
                        }
                        var client = RedisClientFactory.CreateRedisClient(nextHost.Host, nextHost.Port);

                        if (nextHost.RequiresAuth)
                        {
                            client.Password = nextHost.Password;
                        }

                        client.Id               = RedisClientCounter++;
                        client.ClientManager    = this;
                        client.NamespacePrefix  = NamespacePrefix;
                        client.ConnectionFilter = ConnectionFilter;

                        writeClients[i] = client;

                        return(client);
                    }
                }
            }
            //do something to extends the capacity of the writes client;
            //extends two times of the original size
            int originalLength = writeClients.Length;

            if (originalLength * 2 < RedisMaxConnection)
            {
                int length          = writeClients.Length << 1;
                var newwriteClients = new RedisClient[length];
                Array.ConstrainedCopy(writeClients, 0, newwriteClients, 0, writeClients.Length);
                writeClients = newwriteClients;
                var ret = GetInActiveWriteClient();
                return(ret);
            }
            return(null);
        }
Esempio n. 6
0
        private RedisClient InitNewClient(RedisEndpoint nextHost)
        {
            var client = RedisClientFactory.CreateRedisClient(nextHost);

            client.Id               = Interlocked.Increment(ref RedisClientCounter);
            client.ClientManager    = this;
            client.ConnectionFilter = ConnectionFilter;

            return(client);
        }
Esempio n. 7
0
        private RedisClient InitNewClient(RedisEndpoint nextHost)
        {
            var client = RedisClientFactory.CreateRedisClient(nextHost);

            client.Id               = Interlocked.Increment(ref RedisClientCounter);
            client.ClientManager    = this;
            client.ConnectionFilter = ConnectionFilter;
            if (NamespacePrefix != null)
            {
                client.NamespacePrefix = NamespacePrefix;
            }

            return(client);
        }
Esempio n. 8
0
        /// <summary>
        /// Returns a Read/Write client (The default) using the hosts defined in ReadWriteHosts
        /// </summary>
        /// <returns></returns>
        public IRedisClient GetClient()
        {
            var nextHost = ReadWriteHosts[readWriteHostsIndex++ % ReadWriteHosts.Count];
            var client   = RedisClientFactory.CreateRedisClient(
                nextHost.Host, nextHost.Port);

            //Set database to userSpecified if different
            if (Db != RedisNativeClient.DefaultDb)
            {
                client.Db = Db;
            }

            return(client);
        }
Esempio n. 9
0
        /// <summary>
        /// Returns a ReadOnly client using the hosts defined in ReadOnlyHosts.
        /// </summary>
        /// <returns></returns>
        public virtual IRedisClient GetReadOnlyClient()
        {
            var nextHost = ReadOnlyHosts[readOnlyHostsIndex++ % ReadOnlyHosts.Count];
            var client   = RedisClientFactory.CreateRedisClient(
                nextHost.Host, nextHost.Port);

            //Set database to userSpecified if different
            if (Db != RedisNativeClient.DefaultDb)
            {
                client.Db = Db;
            }

            if (nextHost.RequiresAuth)
            {
                client.Password = nextHost.Password;
            }
            return(client);
        }
Esempio n. 10
0
        /// <summary>
        /// Called within a lock
        /// </summary>
        /// <returns></returns>
        private RedisClient GetInActiveWriteClient()
        {
            var desiredIndex = WritePoolIndex % writeClients.Length;

            //this will loop through all hosts in readClients once even though there are 2 for loops
            //both loops are used to try to get the prefered host according to the round robin algorithm
            for (int x = 0; x < ReadWriteHosts.Count; x++)
            {
                var nextHostIndex = (desiredIndex + x) % ReadWriteHosts.Count;
                var nextHost      = ReadWriteHosts[nextHostIndex];
                for (var i = nextHostIndex; i < writeClients.Length; i += ReadWriteHosts.Count)
                {
                    if (writeClients[i] != null && !writeClients[i].Active && !writeClients[i].HadExceptions &&
                        (!CheckConnected || writeClients[i].IsSocketConnected()))
                    {
                        return(writeClients[i]);
                    }
                    else if (writeClients[i] == null || writeClients[i].HadExceptions)
                    {
                        if (writeClients[i] != null)
                        {
                            writeClients[i].DisposeConnection();
                        }
                        var client = RedisClientFactory.CreateRedisClient(nextHost.Host, nextHost.Port);

                        if (nextHost.RequiresAuth)
                        {
                            client.Password = nextHost.Password;
                        }

                        client.Id               = RedisClientCounter++;
                        client.ClientManager    = this;
                        client.NamespacePrefix  = NamespacePrefix;
                        client.ConnectionFilter = ConnectionFilter;

                        writeClients[i] = client;

                        return(client);
                    }
                }
            }
            return(null);
        }
Esempio n. 11
0
        /// <summary>
        /// Called within a lock
        /// </summary>
        /// <returns></returns>
        private RedisClient GetInActiveWriteClient()
        {
            for (var i = 0; i < writeClients.Length; i++)
            {
                var nextIndex = (WritePoolIndex + i) % writeClients.Length;

                //Initialize if not exists
                var existingClient = writeClients[nextIndex];
                if (existingClient == null ||
                    existingClient.HadExceptions)
                {
                    if (existingClient != null)
                    {
                        existingClient.DisposeConnection();
                    }

                    var nextHost = ReadWriteHosts[nextIndex % ReadWriteHosts.Count];

                    var client = RedisClientFactory.CreateRedisClient(
                        nextHost.Host, nextHost.Port);

                    if (nextHost.RequiresAuth)
                    {
                        client.Password = nextHost.Password;
                    }

                    client.Id               = RedisClientCounter++;
                    client.ClientManager    = this;
                    client.ConnectionFilter = ConnectionFilter;

                    writeClients[nextIndex] = client;

                    return(client);
                }

                //look for free one
                if (!writeClients[nextIndex].Active)
                {
                    return(writeClients[nextIndex]);
                }
            }
            return(null);
        }
        /// <summary>
        /// Returns a ReadOnly client using the hosts defined in ReadOnlyHosts.
        /// </summary>
        /// <returns></returns>
        public virtual IRedisClient GetReadOnlyClient()
        {
            var nextHost = ReadOnlyHosts[readOnlyHostsIndex++ % ReadOnlyHosts.Count];
            var client   = RedisClientFactory.CreateRedisClient(
                nextHost.Host, nextHost.Port);

            if (this.ConnectTimeout != null)
            {
                client.ConnectTimeout = this.ConnectTimeout.Value;
            }

            if (nextHost.RequiresAuth)
            {
                client.Password = nextHost.Password;
            }

            client.NamespacePrefix  = NamespacePrefix;
            client.ConnectionFilter = ConnectionFilter;

            return(client);
        }
Esempio n. 13
0
        /// <summary>
        /// Called within a lock
        /// </summary>
        /// <returns></returns>
        private RedisClient GetInActiveReadClient()
        {
            var desiredIndex = ReadPoolIndex % readClients.Length;

            //this will loop through all hosts in readClients once even though there are 2 for loops
            //both loops are used to try to get the prefered host according to the round robin algorithm
            for (int x = 0; x < ReadOnlyHosts.Count; x++)
            {
                var nextHostIndex = (desiredIndex + x) % ReadOnlyHosts.Count;
                var nextHost      = ReadOnlyHosts[nextHostIndex];
                for (var i = nextHostIndex; i < readClients.Length; i += ReadOnlyHosts.Count)
                {
                    if (readClients[i] != null && !readClients[i].Active && !readClients[i].HadExceptions)
                    {
                        return(readClients[i]);
                    }
                    else if (readClients[i] == null || readClients[i].HadExceptions)
                    {
                        if (readClients[i] != null)
                        {
                            readClients[i].DisposeConnection();
                        }
                        var client = RedisClientFactory.CreateRedisClient(nextHost.Host, nextHost.Port);

                        if (nextHost.RequiresAuth)
                        {
                            client.Password = nextHost.Password;
                        }

                        client.ClientManager    = this;
                        client.ConnectionFilter = ConnectionFilter;

                        readClients[i] = client;

                        return(client);
                    }
                }
            }
            return(null);
        }