示例#1
0
        public virtual RedisClient CreateRedisClient(RedisEndpoint config, bool master)
        {
            var client = ClientFactory(config);

            if (master && RedisConfig.VerifyMasterConnections)
            {
                var       firstAttempt  = DateTime.UtcNow;
                Exception firstEx       = null;
                var       retryTimeSpan = TimeSpan.FromMilliseconds(config.RetryTimeout);
                var       i             = 0;
                while (DateTime.UtcNow - firstAttempt < retryTimeSpan)
                {
                    try
                    {
                        client = GetValidMaster(client, config);
                        return(client);
                    }
                    catch (Exception ex)
                    {
                        if (!RedisConfig.RetryReconnectOnFailedMasters)
                        {
                            throw;
                        }

                        firstEx ??= ex;
                        ExecUtils.SleepBackOffMultiplier(++i);
                        client?.Dispose();
                        client = ClientFactory(config);
                    }
                }
                throw new TimeoutException($"Could not resolve master instance within {config.RetryTimeout}ms RetryTimeout", firstEx);
            }

            return(client);
        }
        public void SaveItem(Item item)
        {
            var conf = new RedisEndpoint() { Host = "xxxxxxxxxxxxxx.redis.cache.windows.net", Password = "******", Ssl = true, Port = 6380 };
            using (IRedisClient client = new RedisClient(conf))
            {
                var itemClient = client.As<Item>();
                var itemList = itemClient.Lists["urn:item:" + item.ProductID];
                item.Id = itemClient.GetNextSequence();
                itemList.Add(item);

                client.AddItemToSortedSet("urn:Rank", item.Name, item.Price);

                //Publis top 5 Ranked Items
                IDictionary<string, double> Data = client.GetRangeWithScoresFromSortedSet("urn:Rank", 0, 4);
                List<Item> RankList = new List<Item>();
                int counter = 0;
                foreach (var itm in Data)
                {
                    counter++;
                    RankList.Add(new Item() { Name = itm.Key, Price = (int)itm.Value, Id = counter });
                }

                var itemJson = JsonConvert.SerializeObject(RankList);
                client.PublishMessage("Rank", itemJson);
                //---------------------------------------------
            }
        }
        /// <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;
                RedisEndpoint nextHost      = ReadWriteHosts[nextHostIndex];
                for (var i = nextHostIndex; i < writeClients.Length; i += ReadWriteHosts.Count)
                {
                    if (writeClients[i] != null && !writeClients[i].Active && !writeClients[i].HadExceptions)
                    {
                        return(writeClients[i]);
                    }
                    else if (writeClients[i] == null || writeClients[i].HadExceptions)
                    {
                        if (writeClients[i] != null)
                        {
                            writeClients[i].DisposeConnection();
                        }

                        var client = InitNewClient(nextHost);
                        writeClients[i] = client;

                        return(client);
                    }
                }
            }
            return(null);
        }
示例#4
0
        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 (client.Db != Db) //Reset database to default if changed
            {
                client.ChangeDb(Db);
            }

            return(client);
        }
        static void Main(string[] args)
        {
            var conf = new RedisEndpoint() { Host = "xxxxxxxxxxxxxx.redis.cache.windows.net", Password = "******", Ssl = true, Port = 6380 };
            using (IRedisClient client = new RedisClient(conf))
            {
                IRedisSubscription sub = null;
                using (sub = client.CreateSubscription())
                {
                    sub.OnMessage += (channel, message) =>
                    {
                        try
                        {
                            List<Item> items = JsonConvert.DeserializeObject<List<Item>>(message);
                            Console.WriteLine((string)message);
                            SignalRClass sc = new SignalRClass();
                            sc.SendRank(items);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                    };
                }
                sub.SubscribeToChannels(new string[] { "Rank" });
            }

            Console.ReadLine();
        }
示例#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);
        }
 public void SaveProduct(Product product)
 {
     var conf = new RedisEndpoint() { Host = "xxxxxxxxxxxxxx.redis.cache.windows.net", Password = "******", Ssl = true, Port = 6380 };
     using (IRedisClient client = new RedisClient(conf))
     {
         var userClient = client.As<Product>();
         product.Id = userClient.GetNextSequence();
         userClient.Store(product);
     }
 }
        public virtual RedisClient CreateRedisClient(RedisEndpoint config, bool master)
        {
            var client = ClientFactory(config);

            if (master && RedisConfig.VerifyMasterConnections)
            {
                var role = client.GetServerRole();
                if (role != RedisServerRole.Master)
                {
                    Interlocked.Increment(ref RedisState.TotalInvalidMasters);
                    log.Error("Redis Master Host '{0}' is {1}. Resetting allHosts...".Fmt(config.GetHostString(), role));
                    var         newMasters   = new List <RedisEndpoint>();
                    var         newSlaves    = new List <RedisEndpoint>();
                    RedisClient masterClient = null;
                    foreach (var hostConfig in allHosts)
                    {
                        try
                        {
                            var testClient = ClientFactory(hostConfig);
                            testClient.ConnectTimeout = RedisConfig.HostLookupTimeoutMs;
                            var testRole = testClient.GetServerRole();
                            switch (testRole)
                            {
                            case RedisServerRole.Master:
                                newMasters.Add(hostConfig);
                                if (masterClient == null)
                                {
                                    masterClient = testClient;
                                }
                                break;

                            case RedisServerRole.Slave:
                                newSlaves.Add(hostConfig);
                                break;
                            }
                        }
                        catch { /* skip */ }
                    }

                    if (masterClient == null)
                    {
                        Interlocked.Increment(ref RedisState.TotalNoMastersFound);
                        var errorMsg = "No master found in: " + string.Join(", ", allHosts.Map(x => x.GetHostString()));
                        log.Error(errorMsg);
                        throw new Exception(errorMsg);
                    }

                    ResetMasters(newMasters);
                    ResetSlaves(newSlaves);
                    return(masterClient);
                }
            }

            return(client);
        }
        public RedisSentinelWorker(RedisSentinel sentinel, RedisEndpoint sentinelEndpoint)
        {
            this.sentinel = sentinel;

            //Sentinel Servers doesn't support DB, reset to 0
            var timeoutMs = (int) sentinel.SentinelWorkerTimeout.TotalMilliseconds;
            this.sentinelClient = new RedisClient(sentinelEndpoint) { Db = 0, ConnectTimeout = timeoutMs };

            if (Log.IsDebugEnabled)
                Log.Debug("Set up Redis Sentinel on {0}".Fmt(sentinelEndpoint));
        }
        public RedisSentinelWorker(RedisSentinel sentinel, RedisEndpoint sentinelEndpoint)
        {
            this.sentinel = sentinel;
            this.sentinelClient = new RedisClient(sentinelEndpoint) {
                Db = 0, //Sentinel Servers doesn't support DB, reset to 0
                ConnectTimeout = sentinel.SentinelWorkerTimeoutMs,
            };

            if (Log.IsDebugEnabled)
                Log.Debug("Set up Redis Sentinel on {0}".Fmt(sentinelEndpoint));
        }
        public virtual RedisClient CreateRedisClient(RedisEndpoint config, bool master)
        {
            var client = ClientFactory(config);

            if (master && RedisConfig.VerifyMasterConnections)
            {
                var role = client.GetServerRole();
                if (role != RedisServerRole.Master)
                {
                    Interlocked.Increment(ref RedisState.TotalInvalidMasters);
                    log.Error("Redis Master Host '{0}' is {1}. Resetting allHosts...".Fmt(config.GetHostString(), role));
                    var newMasters = new List<RedisEndpoint>();
                    var newSlaves = new List<RedisEndpoint>();
                    RedisClient masterClient = null;
                    foreach (var hostConfig in allHosts)
                    {
                        try
                        {
                            var testClient = ClientFactory(hostConfig);
                            testClient.ConnectTimeout = RedisConfig.HostLookupTimeoutMs;
                            var testRole = testClient.GetServerRole();
                            switch (testRole)
                            {
                                case RedisServerRole.Master:
                                    newMasters.Add(hostConfig);
                                    if (masterClient == null)
                                        masterClient = testClient;
                                    break;
                                case RedisServerRole.Slave:
                                    newSlaves.Add(hostConfig);
                                    break;
                            }

                        }
                        catch { /* skip */ }
                    }

                    if (masterClient == null)
                    {
                        Interlocked.Increment(ref RedisState.TotalNoMastersFound);
                        var errorMsg = "No master found in: " + string.Join(", ", allHosts.Map(x => x.GetHostString()));
                        log.Error(errorMsg);
                        throw new Exception(errorMsg);
                    }

                    ResetMasters(newMasters);
                    ResetSlaves(newSlaves);
                    return masterClient;
                }
            }

            return client;
        }
 protected bool Equals(RedisEndpoint other)
 {
     return(string.Equals(Host, other.Host) &&
            Port == other.Port &&
            Ssl.Equals(other.Ssl) &&
            ConnectTimeout == other.ConnectTimeout &&
            SendTimeout == other.SendTimeout &&
            ReceiveTimeout == other.ReceiveTimeout &&
            IdleTimeOutSecs == other.IdleTimeOutSecs &&
            Db == other.Db &&
            string.Equals(Client, other.Client) &&
            string.Equals(Password, other.Password) &&
            string.Equals(NamespacePrefix, other.NamespacePrefix));
 }
示例#13
0
 protected bool Equals(RedisEndpoint other)
 {
     return string.Equals(Host, other.Host) 
         && Port == other.Port 
         && Ssl.Equals(other.Ssl) 
         && ConnectTimeout == other.ConnectTimeout 
         && SendTimeout == other.SendTimeout 
         && ReceiveTimeout == other.ReceiveTimeout 
         && IdleTimeOutSecs == other.IdleTimeOutSecs 
         && Db == other.Db 
         && string.Equals(Client, other.Client) 
         && string.Equals(Password, other.Password) 
         && string.Equals(NamespacePrefix, other.NamespacePrefix);
 }
示例#14
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);
        }
        public RedisSentinelWorker(RedisSentinel sentinel, RedisEndpoint sentinelEndpoint)
        {
            this.sentinel       = sentinel;
            this.sentinelClient = new RedisClient(sentinelEndpoint)
            {
                Db             = 0, //Sentinel Servers doesn't support DB, reset to 0
                ConnectTimeout = sentinel.SentinelWorkerConnectTimeoutMs,
                ReceiveTimeout = sentinel.SentinelWorkerReceiveTimeoutMs,
                SendTimeout    = sentinel.SentinelWorkerSendTimeoutMs,
            };

            if (Log.IsDebugEnabled)
            {
                Log.Debug("Set up Redis Sentinel on {0}".Fmt(sentinelEndpoint));
            }
        }
        public void UpdateItem(Item item)
        {
            var conf = new RedisEndpoint() { Host = "xxxxxxxxxxxxxx.redis.cache.windows.net", Password = "******", Ssl = true, Port = 6380 };
            using (IRedisClient client = new RedisClient(conf))
            {
                IRedisTypedClient<Item> itemClient = client.As<Item>();
                IRedisList<Item> itemList = itemClient.Lists["urn:item:" + item.ProductID];

                var index = itemList.Select((Value, Index) => new { Value, Index })
                 .Single(p => p.Value.Id == item.Id).Index;

                var toUpdateItem = itemList.First(x => x.Id == item.Id);

                //var index = itemList.IndexOf(toUpdateItem);

                toUpdateItem.Name = item.Name;
                toUpdateItem.Price = item.Price;

                itemList.RemoveAt(index);
                if (itemList.Count - 1 < index)
                    itemList.Add(toUpdateItem);
                else itemList.Insert(index, toUpdateItem);

                client.RemoveItemFromSortedSet("urn:Rank", item.Name);
                client.AddItemToSortedSet("urn:Rank", item.Name, item.Price);

                //Publis top 5 Ranked Items
                IDictionary<string, double> Data = client.GetRangeWithScoresFromSortedSet("urn:Rank", 0, 4);
                List<Item> RankList = new List<Item>();
                int counter = 0;
                foreach (var itm in Data)
                {
                    counter++;
                    RankList.Add(new Item() { Name = itm.Key, Price = (int)itm.Value, Id = counter });
                }

                var itemJson = JsonConvert.SerializeObject(RankList);
                client.PublishMessage("Rank", itemJson);
                //---------------------------------------------
            }
        }
        public virtual RedisClient CreateRedisClient(RedisEndpoint config, bool readWrite)
        {
            var client = RedisConfig.ClientFactory(config);

            if (readWrite)
            {
                var role = client.GetServerRole();
                if (role != RedisServerRole.Master)
                {
                    try
                    {
                        var stopwatch = Stopwatch.StartNew();
                        while (true)
                        {
                            var masterConfig = sentinel.GetMaster();
                            var master = RedisConfig.ClientFactory(masterConfig);
                            var masterRole = master.GetServerRole();
                            if (masterRole == RedisServerRole.Master)
                                return master;

                            if (stopwatch.Elapsed > sentinel.MaxWaitBetweenSentinelLookups)
                                throw new TimeoutException("Max Wait Between Sentinel Lookups Elapsed: {0}"
                                    .Fmt(sentinel.MaxWaitBetweenSentinelLookups.ToString()));

                            Thread.Sleep(sentinel.WaitBetweenSentinelLookups);
                        }
                    }
                    catch (Exception ex)
                    {
                        log.Error("Redis Master Host '{0}' is {1}. Resetting allHosts...".Fmt(config.GetHostString(), role), ex);
                        var newMasters = new List<RedisEndpoint>();
                        var newSlaves = new List<RedisEndpoint>();
                        RedisClient masterClient = null;
                        foreach (var hostConfig in allHosts)
                        {
                            try
                            {
                                var testClient = new RedisClient(hostConfig)
                                {
                                    ConnectTimeout = RedisConfig.HostLookupTimeout
                                };
                                var testRole = testClient.GetServerRole();
                                switch (testRole)
                                {
                                    case RedisServerRole.Master:
                                        newMasters.Add(hostConfig);
                                        if (masterClient == null)
                                            masterClient = testClient;
                                        break;
                                    case RedisServerRole.Slave:
                                        newSlaves.Add(hostConfig);
                                        break;
                                }

                            }
                            catch { /* skip */ }
                        }

                        if (masterClient == null)
                        {
                            var errorMsg = "No master found in: " + string.Join(", ", allHosts.Map(x => x.GetHostString()));
                            log.Error(errorMsg);
                            throw new Exception(errorMsg);
                        }

                        ResetMasters(newMasters);
                        ResetSlaves(newSlaves);
                        return masterClient;
                    }
                }
            }

            return client;
        }
 public JsonResult GetProducts()
 {
     var conf = new RedisEndpoint() { Host = "xxxxxxxxxxxxxx.redis.cache.windows.net", Password = "******", Ssl = true, Port = 6380 };
     using (IRedisClient client = new RedisClient(conf))
     {
         var productClient = client.As<Product>();
         var products = productClient.GetAll();
         return Json(products, JsonRequestBehavior.AllowGet);
     }
 }
示例#19
0
 public RedisClient CreateRedisClient(RedisEndpoint config, bool master)
 {
     return(ClientFactory(config));
 }
 public JsonResult GetRanks()
 {
     var conf = new RedisEndpoint() { Host = "xxxxxxxxxxxxxx.redis.cache.windows.net", Password = "******", Ssl = true, Port = 6380 };
     using (IRedisClient client = new RedisClient(conf))
     {
         IDictionary<string, double> Data = client.GetRangeWithScoresFromSortedSet("urn:Rank",0,4);                
         List<Item> RankList = new List<Item>();
         int counter = 0;
         foreach(var item in Data)
         {
             counter++;
             RankList.Add(new Item() { Name = item.Key, Price = (int)item.Value, Id=counter });
         }                
         return Json(RankList, JsonRequestBehavior.AllowGet);
     }
 }
 public static RedisClient CreateRedisClient(this IRedisResolver resolver, RedisEndpoint config, bool master)
 {
     return ((IRedisResolverExtended)resolver).CreateRedisClient(config, master);
 }
示例#22
0
 public static RedisClient CreateRedisClient(this IRedisResolver resolver, RedisEndpoint config, bool master)
 {
     return(((IRedisResolverExtended)resolver).CreateRedisClient(config, master));
 }
 public RedisClient CreateRedisClient(RedisEndpoint config, bool readWrite)
 {
     return RedisConfig.ClientFactory(config);
 }
        public virtual RedisClient CreateRedisClient(RedisEndpoint config, bool master)
        {
            var client = ClientFactory(config);
            if (master)
            {
                var role = RedisServerRole.Unknown;
                try
                {
                    role = client.GetServerRole();
                    if (role == RedisServerRole.Master)
                    {
                        lastValidMasterFromSentinelAt = DateTime.UtcNow;
                        return client;
                    }
                }
                catch (Exception ex)
                {
                    Interlocked.Increment(ref RedisState.TotalInvalidMasters);

                    if (client.GetHostString() == lastInvalidMasterHost)
                    {
                        lock (oLock)
                        {
                            if (DateTime.UtcNow - lastValidMasterFromSentinelAt > sentinel.WaitBeforeForcingMasterFailover)
                            {
                                lastInvalidMasterHost = null;
                                lastValidMasterFromSentinelAt = DateTime.UtcNow;

                                log.Error("Valid master was not found at '{0}' within '{1}'. Sending SENTINEL failover...".Fmt(
                                    client.GetHostString(), sentinel.WaitBeforeForcingMasterFailover), ex);

                                Interlocked.Increment(ref RedisState.TotalForcedMasterFailovers);

                                sentinel.ForceMasterFailover();
                                TaskUtils.Sleep(sentinel.WaitBetweenFailedHosts);
                                role = client.GetServerRole();
                            }
                        }
                    }
                    else
                    {
                        lastInvalidMasterHost = client.GetHostString();
                    }
                }

                if (role != RedisServerRole.Master && RedisConfig.VerifyMasterConnections)
                {
                    try
                    {
                        var stopwatch = Stopwatch.StartNew();
                        while (true)
                        {
                            try
                            {
                                var masterConfig = sentinel.GetMaster();
                                var masterClient = ClientFactory(masterConfig);
                                masterClient.ConnectTimeout = sentinel.SentinelWorkerConnectTimeoutMs;

                                var masterRole = masterClient.GetServerRole();
                                if (masterRole == RedisServerRole.Master)
                                {
                                    lastValidMasterFromSentinelAt = DateTime.UtcNow;
                                    return masterClient;
                                }
                                else
                                {
                                    Interlocked.Increment(ref RedisState.TotalInvalidMasters);
                                }
                            }
                            catch { /* Ignore errors until MaxWait */ }

                            if (stopwatch.Elapsed > sentinel.MaxWaitBetweenFailedHosts)
                                throw new TimeoutException("Max Wait Between Sentinel Lookups Elapsed: {0}"
                                    .Fmt(sentinel.MaxWaitBetweenFailedHosts.ToString()));

                            TaskUtils.Sleep(sentinel.WaitBetweenFailedHosts);
                        }
                    }
                    catch (Exception ex)
                    {
                        log.Error("Redis Master Host '{0}' is {1}. Resetting allHosts...".Fmt(config.GetHostString(), role), ex);

                        var newMasters = new List<RedisEndpoint>();
                        var newSlaves = new List<RedisEndpoint>();
                        RedisClient masterClient = null;
                        foreach (var hostConfig in allHosts)
                        {
                            try
                            {
                                var testClient = ClientFactory(hostConfig);
                                testClient.ConnectTimeout = RedisConfig.HostLookupTimeoutMs;
                                var testRole = testClient.GetServerRole();
                                switch (testRole)
                                {
                                    case RedisServerRole.Master:
                                        newMasters.Add(hostConfig);
                                        if (masterClient == null)
                                            masterClient = testClient;
                                        break;
                                    case RedisServerRole.Slave:
                                        newSlaves.Add(hostConfig);
                                        break;
                                }

                            }
                            catch { /* skip past invalid master connections */ }
                        }

                        if (masterClient == null)
                        {
                            Interlocked.Increment(ref RedisState.TotalNoMastersFound);
                            var errorMsg = "No master found in: " + string.Join(", ", allHosts.Map(x => x.GetHostString()));
                            log.Error(errorMsg);
                            throw new Exception(errorMsg);
                        }

                        ResetMasters(newMasters);
                        ResetSlaves(newSlaves);
                        return masterClient;
                    }
                }
            }

            return client;
        }
 private void Init(RedisEndpoint config)
 {
     Host = config.Host;
     Port = config.Port;
     ConnectTimeout = config.ConnectTimeout;
     SendTimeout = config.SendTimeout;
     ReceiveTimeout = config.ReceiveTimeout;
     RetryTimeout = config.RetryTimeout;
     Password = config.Password;
     NamespacePrefix = config.NamespacePrefix;
     Client = config.Client;
     Db = config.Db;
     Ssl = config.Ssl;
     IdleTimeOutSecs = config.IdleTimeOutSecs;
     ServerVersionNumber = RedisConfig.AssumeServerVersion.GetValueOrDefault();
 }
 public JsonResult GetItems(int? productID)
 {
     var conf = new RedisEndpoint() { Host = "xxxxxxxxxxxxxx.redis.cache.windows.net", Password = "******", Ssl = true, Port = 6380 };
     using (IRedisClient client = new RedisClient(conf))
     {
         var itemClient = client.As<Item>();
         var itemList = itemClient.Lists["urn:item:" + productID];
         var items = itemList.GetAll();
         return Json(items, JsonRequestBehavior.AllowGet);
     }
 }
 private void Init(RedisEndpoint config)
 {
     Host = config.Host;
     Port = config.Port;
     ConnectTimeout = config.ConnectTimeout;
     SendTimeout = config.SendTimeout;
     ReceiveTimeout = config.ReceiveTimeout;
     Password = config.Password;
     NamespacePrefix = config.NamespacePrefix;
     Client = config.Client;
     Db = config.Db;
     Ssl = config.Ssl;
     IdleTimeOutSecs = config.IdleTimeOutSecs;
 }
 public JsonResult GetEditItem(int ProductID, int Id)
 {
     var conf = new RedisEndpoint() { Host = "xxxxxxxxxxxxxx.redis.cache.windows.net", Password = "******", Ssl = true, Port = 6380 };
     using (IRedisClient client = new RedisClient(conf))
     {
         var itemClient = client.As<Item>();
         var itemList = itemClient.Lists["urn:item:" + ProductID];
         var toUpdateItem = itemList.First(x => x.Id == Id);
         return Json(toUpdateItem, JsonRequestBehavior.AllowGet);
     }
 }        
        public static RedisEndpoint ToRedisEndpoint(this string connectionString, int? defaultPort = null)
        {
            if (connectionString == null)
                throw new ArgumentNullException("connectionString");
            if (connectionString.StartsWith("redis://"))
                connectionString = connectionString.Substring("redis://".Length);

            var domainParts = connectionString.SplitOnLast('@');
            var qsParts = domainParts.Last().SplitOnFirst('?');
            var hostParts = qsParts[0].SplitOnLast(':');
            var useDefaultPort = true;
            var port = defaultPort.GetValueOrDefault(RedisNativeClient.DefaultPort);
            if (hostParts.Length > 1)
            {
                port = int.Parse(hostParts[1]);
                useDefaultPort = false;
            }
            var endpoint = new RedisEndpoint(hostParts[0], port);
            if (domainParts.Length > 1)
            {
                var authParts = domainParts[0].SplitOnFirst(':');
                if (authParts.Length > 1)
                    endpoint.Client = authParts[0];

                endpoint.Password = authParts.Last();
            }

            if (qsParts.Length > 1)
            {
                var qsParams = qsParts[1].Split('&');
                foreach (var param in qsParams)
                {
                    var entry = param.Split('=');
                    var value = entry.Length > 1 ? entry[1].UrlDecode() : null;
                    if (value == null) continue;

                    var name = entry[0].ToLower(); 
                    switch (name)
                    {
                        case "db":
                            endpoint.Db = int.Parse(value);
                            break;
                        case "ssl":
                            endpoint.Ssl = bool.Parse(value);
                            if (useDefaultPort)
                                endpoint.Port = RedisNativeClient.DefaultPortSsl;
                            break;
                        case "client":
                            endpoint.Client = value;
                            break;
                        case "password":
                            endpoint.Password = value;
                            break;
                        case "namespaceprefix":
                            endpoint.NamespacePrefix = value;
                            break;
                        case "connecttimeout":
                            endpoint.ConnectTimeout = int.Parse(value);
                            break;
                        case "sendtimeout":
                            endpoint.SendTimeout = int.Parse(value);
                            break;
                        case "receivetimeout":
                            endpoint.ReceiveTimeout = int.Parse(value);
                            break;
                        case "idletimeout":
                        case "idletimeoutsecs":
                            endpoint.IdleTimeOutSecs = int.Parse(value);
                            break;
                    }
                }
            }

            return endpoint;
        }
        private RedisClient InitNewClient(RedisEndpoint nextHost)
        {
            var client = RedisClientFactory.CreateRedisClient(nextHost);
            client.Id = Interlocked.Increment(ref RedisClientCounter);
            client.ClientManager = this;
            client.ConnectionFilter = ConnectionFilter;

            return client;
        }
 public RedisClient CreateRedisClient(RedisEndpoint config)
 {
     return(new RedisClient(config));
 }
示例#32
0
 private static void AssertClient(RedisClient redis, RedisEndpoint expected)
 {
     Assert.That(redis.Host, Is.EqualTo(expected.Host));
     Assert.That(redis.Port, Is.EqualTo(expected.Port));
     Assert.That(redis.Ssl, Is.EqualTo(expected.Ssl));
     Assert.That(redis.Client, Is.EqualTo(expected.Client));
     Assert.That(redis.Password, Is.EqualTo(expected.Password));
     Assert.That(redis.Db, Is.EqualTo(expected.Db));
     Assert.That(redis.ConnectTimeout, Is.EqualTo(expected.ConnectTimeout));
     Assert.That(redis.SendTimeout, Is.EqualTo(expected.SendTimeout));
     Assert.That(redis.ReceiveTimeout, Is.EqualTo(expected.ReceiveTimeout));
    // Assert.That(redis.RetryTimeout, Is.EqualTo(expected.RetryTimeout));
     Assert.That(redis.IdleTimeOutSecs, Is.EqualTo(expected.IdleTimeOutSecs));
     Assert.That(redis.NamespacePrefix, Is.EqualTo(expected.NamespacePrefix));
 }
 public RedisNativeClient(RedisEndpoint config)
 {
     Init(config);
 }
示例#34
0
        private static void AssertClientManager(IRedisClientsManager redisManager, RedisEndpoint expected)
        {
            using (var readWrite = (RedisClient)redisManager.GetClient())
            using (var readOnly = (RedisClient)redisManager.GetReadOnlyClient())
            using (var cacheClientWrapper = (RedisClientManagerCacheClient)redisManager.GetCacheClient())
            {
                AssertClient(readWrite, expected);
                AssertClient(readOnly, expected);

                using (var cacheClient = (RedisClient)cacheClientWrapper.GetClient())
                {
                    AssertClient(cacheClient, expected);
                }
            }
        }
        public static RedisEndpoint ToRedisEndpoint(this string connectionString, int?defaultPort = null)
        {
            if (connectionString == null)
            {
                throw new ArgumentNullException("connectionString");
            }
            if (connectionString.StartsWith("redis://"))
            {
                connectionString = connectionString.Substring("redis://".Length);
            }

            var domainParts    = connectionString.SplitOnLast('@');
            var qsParts        = domainParts.Last().SplitOnFirst('?');
            var hostParts      = qsParts[0].SplitOnLast(':');
            var useDefaultPort = true;
            var port           = defaultPort.GetValueOrDefault(RedisConfig.DefaultPort);

            if (hostParts.Length > 1)
            {
                port           = int.Parse(hostParts[1]);
                useDefaultPort = false;
            }
            var endpoint = new RedisEndpoint(hostParts[0], port);

            if (domainParts.Length > 1)
            {
                var authParts = domainParts[0].SplitOnFirst(':');
                if (authParts.Length > 1)
                {
                    endpoint.Client = authParts[0];
                }

                endpoint.Password = authParts.Last();
            }

            if (qsParts.Length > 1)
            {
                var qsParams = qsParts[1].Split('&');
                foreach (var param in qsParams)
                {
                    var entry = param.Split('=');
                    var value = entry.Length > 1 ? entry[1].UrlDecode() : null;
                    if (value == null)
                    {
                        continue;
                    }

                    var name = entry[0].ToLower();
                    switch (name)
                    {
                    case "db":
                        endpoint.Db = int.Parse(value);
                        break;

                    case "ssl":
                        endpoint.Ssl = bool.Parse(value);
                        if (useDefaultPort)
                        {
                            endpoint.Port = RedisConfig.DefaultPortSsl;
                        }
                        break;

                    case "client":
                        endpoint.Client = value;
                        break;

                    case "password":
                        endpoint.Password = value;
                        break;

                    case "namespaceprefix":
                        endpoint.NamespacePrefix = value;
                        break;

                    case "connecttimeout":
                        endpoint.ConnectTimeout = int.Parse(value);
                        break;

                    case "sendtimeout":
                        endpoint.SendTimeout = int.Parse(value);
                        break;

                    case "receivetimeout":
                        endpoint.ReceiveTimeout = int.Parse(value);
                        break;

                    case "retrytimeout":
                        endpoint.RetryTimeout = int.Parse(value);
                        break;

                    case "idletimeout":
                    case "idletimeoutsecs":
                        endpoint.IdleTimeOutSecs = int.Parse(value);
                        break;
                    }
                }
            }

            return(endpoint);
        }
 public RedisClient CreateRedisClient(RedisEndpoint config)
 {
     return new RedisClient(config);
 }
示例#37
0
        public virtual RedisClient CreateRedisClient(RedisEndpoint config, bool master)
        {
            var client = ClientFactory(config);

            if (master)
            {
                var role = RedisServerRole.Unknown;
                try
                {
                    role = client.GetServerRole();
                    if (role == RedisServerRole.Master)
                    {
                        lastValidMasterFromSentinelAt = DateTime.UtcNow;
                        return(client);
                    }
                }
                catch (Exception ex)
                {
                    Interlocked.Increment(ref RedisState.TotalInvalidMasters);

                    if (client.GetHostString() == lastInvalidMasterHost)
                    {
                        lock (oLock)
                        {
                            if (DateTime.UtcNow - lastValidMasterFromSentinelAt > sentinel.WaitBeforeForcingMasterFailover)
                            {
                                lastInvalidMasterHost         = null;
                                lastValidMasterFromSentinelAt = DateTime.UtcNow;

                                log.Error("Valid master was not found at '{0}' within '{1}'. Sending SENTINEL failover...".Fmt(
                                              client.GetHostString(), sentinel.WaitBeforeForcingMasterFailover), ex);

                                Interlocked.Increment(ref RedisState.TotalForcedMasterFailovers);

                                sentinel.ForceMasterFailover();
                                Thread.Sleep(sentinel.WaitBetweenSentinelLookups);
                                role = client.GetServerRole();
                            }
                        }
                    }
                    else
                    {
                        lastInvalidMasterHost = client.GetHostString();
                    }
                }

                if (role != RedisServerRole.Master)
                {
                    try
                    {
                        var stopwatch = Stopwatch.StartNew();
                        while (true)
                        {
                            try
                            {
                                var masterConfig = sentinel.GetMaster();
                                var masterClient = ClientFactory(masterConfig);
                                masterClient.ConnectTimeout = sentinel.SentinelWorkerConnectTimeoutMs;

                                var masterRole = masterClient.GetServerRole();
                                if (masterRole == RedisServerRole.Master)
                                {
                                    lastValidMasterFromSentinelAt = DateTime.UtcNow;
                                    return(masterClient);
                                }
                                else
                                {
                                    Interlocked.Increment(ref RedisState.TotalInvalidMasters);
                                }
                            }
                            catch { /* Ignore errors until MaxWait */ }

                            if (stopwatch.Elapsed > sentinel.MaxWaitBetweenSentinelLookups)
                            {
                                throw new TimeoutException("Max Wait Between Sentinel Lookups Elapsed: {0}"
                                                           .Fmt(sentinel.MaxWaitBetweenSentinelLookups.ToString()));
                            }

                            Thread.Sleep(sentinel.WaitBetweenSentinelLookups);
                        }
                    }
                    catch (Exception ex)
                    {
                        log.Error("Redis Master Host '{0}' is {1}. Resetting allHosts...".Fmt(config.GetHostString(), role), ex);

                        var         newMasters   = new List <RedisEndpoint>();
                        var         newSlaves    = new List <RedisEndpoint>();
                        RedisClient masterClient = null;
                        foreach (var hostConfig in allHosts)
                        {
                            try
                            {
                                var testClient = ClientFactory(hostConfig);
                                testClient.ConnectTimeout = RedisConfig.HostLookupTimeoutMs;
                                var testRole = testClient.GetServerRole();
                                switch (testRole)
                                {
                                case RedisServerRole.Master:
                                    newMasters.Add(hostConfig);
                                    if (masterClient == null)
                                    {
                                        masterClient = testClient;
                                    }
                                    break;

                                case RedisServerRole.Slave:
                                    newSlaves.Add(hostConfig);
                                    break;
                                }
                            }
                            catch { /* skip past invalid master connections */ }
                        }

                        if (masterClient == null)
                        {
                            Interlocked.Increment(ref RedisState.TotalNoMastersFound);
                            var errorMsg = "No master found in: " + string.Join(", ", allHosts.Map(x => x.GetHostString()));
                            log.Error(errorMsg);
                            throw new Exception(errorMsg);
                        }

                        ResetMasters(newMasters);
                        ResetSlaves(newSlaves);
                        return(masterClient);
                    }
                }
            }

            return(client);
        }
 public RedisClient CreateRedisClient(RedisEndpoint config, bool master)
 {
     return ClientFactory(config);
 }
示例#39
0
 public RedisClient(RedisEndpoint config)
     : base(config)
 {
     Init();
 }
        private RedisClient InitNewClient(RedisEndpoint nextHost, bool readWrite)
        {
            var client = ClientFactory != null
                ? ClientFactory(nextHost)
                : RedisResolver.CreateRedisClient(nextHost, readWrite:true);

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

            return client;
        }