public RedisResult <RedisRoleInfo> Role()
        {
            var response = ExpectArray(new RedisCommand(RedisConstants.UninitializedDbIndex, RedisCommandList.Role));

            if (!ReferenceEquals(response, null))
            {
                var result = RedisRoleInfo.Parse(response);
                return(new RedisResult <RedisRoleInfo>(result));
            }
            return(new RedisResult <RedisRoleInfo>(null));
        }
Esempio n. 2
0
        protected virtual RedisRole DiscoverRole()
        {
            if (!Disposed)
            {
                var role = RedisRole.Undefined;
                try
                {
                    var array = RunSyncTask(new RedisCommand(RedisConstants.UninitializedDbIndex,
                                                             RedisCommandList.Role,
                                                             RedisCommandType.SendAndReceive)
                    {
                        Priority = RedisCommandPriority.High
                    }) as RedisArray;

                    if (!ReferenceEquals(array, null) && array.IsCompleted)
                    {
                        var info = RedisRoleInfo.Parse(array);
                        if (!ReferenceEquals(info, null))
                        {
                            role = info.Role;
                        }
                    }
                }
                catch (Exception e)
                {
                    var exception = e;
                    while (exception != null)
                    {
                        if (exception is SocketException)
                        {
                            return(RedisRole.Undefined);
                        }

                        var re = e as RedisException;
                        if (re != null && (re.ErrorCode == RedisErrorCode.ConnectionError ||
                                           re.ErrorCode == RedisErrorCode.SocketError))
                        {
                            return(RedisRole.Undefined);
                        }

                        exception = exception.InnerException;
                    }
                }

                if (role == RedisRole.Undefined)
                {
                    try
                    {
                        var serverInfo = GetServerInfo();
                        if (serverInfo != null)
                        {
                            var serverSection = serverInfo.Server;
                            if (serverSection != null)
                            {
                                role = (serverSection.RedisMode ?? String.Empty).Trim().ToRedisRole();
                            }

                            if (role == RedisRole.Undefined)
                            {
                                var replicationSection = serverInfo.Replication;
                                if (replicationSection != null)
                                {
                                    role = (replicationSection.Role ?? String.Empty).Trim().ToRedisRole();
                                }

                                if (role == RedisRole.Undefined)
                                {
                                    var sentinelSection = serverInfo.Sentinel;
                                    if (sentinelSection != null && sentinelSection.SentinelMasters.HasValue)
                                    {
                                        role = RedisRole.Sentinel;
                                    }

                                    if (role == RedisRole.Undefined)
                                    {
                                        role = RedisRole.Master;
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception)
                    { }
                }
                return(role);
            }
            return(RedisRole.Undefined);
        }