Beispiel #1
0
 private void PubSubMessageReceived(RedisPubSubMessage message)
 {
     if (!message.IsEmpty)
     {
         try
         {
             InvokeOnSentinelMessage(message);
         }
         catch (Exception)
         { }
     }
 }
Beispiel #2
0
        protected virtual void MessageReceived(RedisPubSubMessage message)
        {
            if (!Disposed &&
                !ReferenceEquals(message, null) && !message.IsEmpty &&
                CanSendMessage(message))
            {
                m_LastMessageSeenTime = DateTime.UtcNow;

                if (!message.IsEmpty)
                {
                    switch (message.Type)
                    {
                    case RedisPubSubMessageType.Message:
                    {
                        var subscriptions = m_Subscriptions;
                        if (subscriptions != null &&
                            subscriptions.HasCallbacks(message.Channel))
                        {
                            subscriptions.Invoke(message.Channel, message);
                        }
                    }
                    break;

                    case RedisPubSubMessageType.PMessage:
                    {
                        var subscriptions = m_PSubscriptions;
                        if (subscriptions != null &&
                            subscriptions.HasCallbacks(message.Pattern))
                        {
                            subscriptions.Invoke(message.Pattern, message);
                        }
                    }
                    break;

                    default:
                        break;
                    }
                }
            }
        }
Beispiel #3
0
 protected virtual bool CanSendMessage(RedisPubSubMessage message)
 {
     return(true);
 }
Beispiel #4
0
        private void InvokeOnSentinelMessage(RedisPubSubMessage message)
        {
            var messageType = ToSentinelMessageType(message.Channel);

            if (messageType == RedisSentinelMessageType.Undefined)
            {
                return;
            }

            var data = message.Data;

            if (data != null)
            {
                var rawData = data.RawData as byte[];
                if (rawData != null)
                {
                    var msgText = rawData.ToUTF8String();

                    if (!msgText.IsEmpty())
                    {
                        var parts = msgText.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                        if (parts != null)
                        {
                            var partsLength = parts.Length;
                            if (partsLength > 0)
                            {
                                /*
                                 +switch-master mymaster 127.0.0.1 6379 127.0.0.1 6380
                                 */
                                if (messageType == RedisSentinelMessageType.MasterChanged)
                                {
                                    if (partsLength > 2)
                                    {
                                        var onSentinelMessage = m_OnSentinelMessage;
                                        if (onSentinelMessage != null)
                                        {
                                            var masterName = parts[0];
                                            if (masterName == MasterName)
                                            {
                                                var oldEndPoint = ToEndPoint(parts[1], parts[2]);
                                                var newEndPoint = (partsLength > 4) ? ToEndPoint(parts[3], parts[4]) : RedisEndPoint.Empty;

                                                onSentinelMessage(new RedisSentinelMessage(messageType, masterName,
                                                                                           new RedisNodeInfo(newEndPoint, RedisRole.Master),
                                                                                           new RedisNodeInfo(oldEndPoint, RedisRole.Master)));
                                            }
                                        }
                                    }
                                    return;
                                }

                                /* Samples:
                                 +sdown master mymaster 127.0.0.1 6380
                                 +sdown slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379
                                 * -sdown slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379
                                 *
                                 +odown master mymaster 127.0.0.1 6379 #quorum 2/2
                                 * -odown master mymaster 127.0.0.1 6379
                                 *
                                 +sdown sentinel 127.0.0.1:26381 127.0.0.1 26381 @ mymaster 127.0.0.1 6379
                                 * -dup-sentinel master mymaster 127.0.0.1 6381 #duplicate of 127.0.0.1:26381 or cab1c287ec59309126ad1a63b354ba132bb4e55b
                                 +sentinel sentinel 127.0.0.1:26381 127.0.0.1 26379 @ mymaster 127.0.0.1 6379
                                 */
                                if (partsLength > 3)
                                {
                                    var onSentinelMessage = m_OnSentinelMessage;
                                    if (onSentinelMessage != null)
                                    {
                                        var instanceType = (parts[0] ?? String.Empty).ToLowerInvariant();

                                        var role = instanceType.ToRedisRole();
                                        switch (role)
                                        {
                                        case RedisRole.Master:
                                        {
                                            var masterName = parts[1];
                                            if (masterName == MasterName)
                                            {
                                                var masterEndPoint = ToEndPoint(parts[2], parts[3]);

                                                onSentinelMessage(new RedisSentinelMessage(messageType, masterName,
                                                                                           new RedisNodeInfo(masterEndPoint, RedisRole.Master),
                                                                                           new RedisNodeInfo(RedisEndPoint.Empty, RedisRole.Undefined)));
                                            }
                                            break;
                                        }

                                        case RedisRole.Slave:
                                        case RedisRole.Sentinel:
                                        {
                                            if (partsLength > 4)
                                            {
                                                var masterName = parts[5];
                                                if (masterName == MasterName)
                                                {
                                                    var instanceName = parts[1];

                                                    var instanceEndPoint = ToEndPoint(parts[2], parts[3]);
                                                    var masterEndPoint   = (partsLength > 7) ? ToEndPoint(parts[6], parts[7]) : RedisEndPoint.Empty;

                                                    if (instanceEndPoint.IsEmpty() && !instanceName.IsEmpty())
                                                    {
                                                        var nameParts = instanceName.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                                                        if (!nameParts.IsEmpty())
                                                        {
                                                            instanceEndPoint = ToEndPoint(nameParts[0], nameParts[1]);
                                                        }
                                                    }

                                                    onSentinelMessage(new RedisSentinelMessage(messageType, masterName,
                                                                                               new RedisNodeInfo(instanceEndPoint, role, instanceName),
                                                                                               new RedisNodeInfo(masterEndPoint, !masterEndPoint.IsEmpty() ? RedisRole.Master : RedisRole.Undefined)));
                                                }
                                            }
                                            break;
                                        }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }