Exemplo n.º 1
0
        private static void GetMessageUpdates(RedisChannel channel, RedisValue value)
        {
            var vals = value.ToString().Split('|');
            if (vals.Length != 3 || vals[0] == InstanceId) return;

            GlobalHost.ConnectionManager.GetHubContext<ChatHub>().Clients.All.receiveMessage(vals[1], vals[2]);
        }
Exemplo n.º 2
0
 public void ListenForMessage(RedisChannel channel, RedisValue value)
 {
     Assert.AreEqual(RedisKey, channel.ToString());
     Assert.IsFalse(!value.HasValue || value.IsNullOrEmpty);
     Assert.AreEqual("INFO test pub/sub message", value.ToString());
     ActionRun = true;
 }
 private void OnInstanceNotified(RedisChannel channel,RedisValue notification)
 {
     try
     {
         var list = monitors[channel];
         var notify = BahamutAppInstanceNotification.FromJson(notification);
         switch (notify.NotifyType)
         {
             case BahamutAppInstanceNotification.TYPE_INSTANCE_HEART_BEAT:
                 foreach (var monitor in list) { monitor.OnInstanceHeartBeating(notify); }
                 break;
             case BahamutAppInstanceNotification.TYPE_INSTANCE_OFFLINE:
                 foreach (var monitor in list) { monitor.OnInstanceOffline(notify); }
                 break;
             case BahamutAppInstanceNotification.TYPE_REGIST_APP_INSTANCE:
                 foreach (var monitor in list) { monitor.OnInstanceRegisted(notify); }
                 break;
             default:
                 break;
         }
     }
     catch (Exception)
     {
     }
 }
        void SubscriberMessageReceived(RedisChannel redisChannel, RedisValue value)
        {
            if (!((string)redisChannel).EndsWith("expired")) return;
            var key = Encoding.UTF8.GetString(value);

            LogMethod("Expired", key, null);
            RemoveMethod(key);
        }
Exemplo n.º 5
0
        private void CacheUpdated(RedisChannel channel, RedisValue message)
        {

            var updateNotification = _itemSerializer.Deserialize<CacheUpdateNotificationArgs>(message);

            if (updateNotification.ClientName.Equals(_clientName))
                return;

            if (CacheUpdate != null)
                CacheUpdate(this, updateNotification);
        }
Exemplo n.º 6
0
        private static void GetConnectionUpdates(RedisChannel channel, RedisValue value)
        {
            var vals = value.ToString().Split('|');
            if (vals.Length != 3 || vals[1] == InstanceId) return;

            if (vals[0] == "1")
            {
                GlobalHost.ConnectionManager.GetHubContext<ChatHub>().Clients.All.clientConnected(vals[2]);
            }
            else if (vals[0] == "0")
            {
                GlobalHost.ConnectionManager.GetHubContext<ChatHub>().Clients.All.clientDisconnected(vals[2]);
            }
        }
Exemplo n.º 7
0
        public IDisposable Subscribe(string key, Action<string, string> handler)
        {
            if (string.IsNullOrWhiteSpace(key))
            {
                throw new ArgumentNullException("key");
            }
            if (handler == null)
            {
                throw new ArgumentNullException("handler");
            }
            var subscription = new RedisChannel(key, RedisChannel.PatternMode.Literal);

            var subscriber = new RedisSubscriber(() => _subscriber.Value.Unsubscribe(subscription));

            _subscriber.Value.Subscribe(subscription, (k, v) => handler(k, v));

            return subscriber;
        }
Exemplo n.º 8
0
        internal RedisChannel AsRedisChannel(byte[] channelPrefix, RedisChannel.PatternMode mode)
        {
            switch (Type)
            {
                case ResultType.SimpleString:
                case ResultType.BulkString:
                    if (channelPrefix == null)
                    {
                        return new RedisChannel(GetBlob(), mode);
                    }
                    if (AssertStarts(channelPrefix))
                    {
                        var src = (byte[])arr;

                        byte[] copy = new byte[count - channelPrefix.Length];
                        Buffer.BlockCopy(src, offset + channelPrefix.Length, copy, 0, copy.Length);
                        return new RedisChannel(copy, mode);
                    }
                    return default(RedisChannel);
                default:
                    throw new InvalidCastException("Cannot convert to RedisChannel: " + Type);
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// 发布
        /// </summary>
        /// <param name="channel"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public long Publish(RedisChannel channel, RedisValue message)
        {
            var sub = _connMultiplexer.GetSubscriber();

            return(sub.Publish(channel, message));
        }
Exemplo n.º 10
0
 private void OnUserDisconnectMessage(RedisChannel channel, RedisValue user)
 {
     UsersByConnectionId.Remove(user);
 }
Exemplo n.º 11
0
        private void OnStop(RedisChannel ch, RedisValue data)
        {
            var shardId = JsonConvert.DeserializeObject <int>(data);

            OnStop(shardId);
        }
Exemplo n.º 12
0
				private void MasterWasSwitched(RedisChannel redisChannel, RedisValue redisValue)
        {
	        _connection.ConnectionFailed -= OnConnectionFailed;
	        _connection.ConnectionRestored -= OnConnectionRestored;
	        _connection.ErrorMessage -= OnError;
          _connection.Close();
          if (redisValue.IsNullOrEmpty) return;
          var message = redisValue.ToString();
          var messageParts = message.Split(' ');
          var ip = IPAddress.Parse(messageParts[3]);
          var port = int.Parse(messageParts[4]);
          EndPoint newMasterEndpoint = new IPEndPoint(ip, port);
          if (_options.EndPoints.Any() && newMasterEndpoint == _options.EndPoints[0]) return;
          _options.EndPoints.Clear();
          _options.EndPoints.Add(newMasterEndpoint);

          _connection = ConnectionMultiplexer.Connect(_options);
          _connection.ConnectionFailed += OnConnectionFailed;
          _connection.ConnectionRestored += OnConnectionRestored;
          _connection.ErrorMessage += OnError;
          _redisSubscriber = _connection.GetSubscriber();
          var handler = ConnectionRestored;
          if (handler != null) handler(new ApplicationException("Redis master was switched"));
        }
Exemplo n.º 13
0
 /// <summary>
 /// Handlers the specified redis channel.
 /// </summary>
 /// <param name="redisChannel">The redis channel.</param>
 /// <param name="redisValue">The redis value.</param>
 private void Handler(RedisChannel redisChannel, RedisValue redisValue)
 {
     _waitHandle.Set();
 }
Exemplo n.º 14
0
        private void WaitChannelPulse(RedisChannel redisChannel, RedisValue redisValue)
        {
            lock (this.waitLock)
            {
                if (this.wasWaitPulsed)
                {
                    // somehow we got message that we shouldn't get
                    return;
                }

                try
                {
                    this.wasWaitPulsed = true;
                    var pulseType = (PulseType)(int.Parse(redisValue.ToString()));
                    if (pulseType == PulseType.One)
                    {
                        // try get permission to wake
                        try
                        {
                            this.redisStorage.Remove(this.PulseFile);
                        }
                        catch (StorageKeyDoesntExistException)
                        {
                            // failed - need to sleep longer
                            return;
                        }
                    }

                    // unsubscribe yourself
                    this.redisStorage.Unsubscribe(this.WaitChannel, this.WaitChannelPulse);
                    Monitor.Pulse(this.waitLock);
                }
                finally
                {
                    this.wasWaitPulsed = false;
                }
            }
        }
Exemplo n.º 15
0
 public async Task SubscribeAsync(RedisChannel channel, Action<RedisChannel, RedisValue> action)
 {
     await pubsubRedis.GetSubscriber().SubscribeAsync(AddChannelWithPrefix(channel), (c, v) =>
     {
         action(RemoveChannelPrefix(c), v);
     });
 }
        private void onMessage(RedisChannel channel, RedisValue value)
        {
            log(Logging.LoggingLevel.Detailed, "Received Message: {0}", value.ToString());

            var dict = value.ToString().ToObject<Dictionary<string, string>>();
            if (OnMessage != null)
                OnMessage(dict["cacheName"], dict["message"]);
        }
Exemplo n.º 17
0
        private void onReceiveMessage(RedisChannel channel, RedisValue value)
        {
            string work = (string)database.ListRightPop($"{(string)channel}-bl");
            if (work == null)
            {
            //                Console.WriteLine($"{channel} got thing, but no message");
                return;
            }
            #if log
            Console.WriteLine("Receiving Message: " + channel + "    " + work);
            Console.WriteLine();
            #endif

            RedisMessage message = JsonConvert.DeserializeObject<RedisMessage>(work, new JsonSerializerSettings()
            {
                TypeNameHandling = TypeNameHandling.Objects
            });

            if (lateTaskManager.Exists(message.Guid))
            {
                lateTaskManager.Resolve(message.Guid,message);
            }
            else
            {
                if (subscriptions[channel] != null)
                {
                    subscriptions[channel](message);
                }
            }
        }
Exemplo n.º 18
0
            private void DoSomeThing(RedisChannel channel, RedisValue message)
            {
                DataRow row;
                row = this.Rows.Find(channel.ToString());
                if (row == null)
                {
                    row = this.NewRow();
                    row["ContractCode"] = channel.ToString();
                    try
                    {
                        foreach (String item in ((string)message).Split(','))
                        {
                            String key;
                            String value;
                            String[] keyValue = item.Split(':');
                            key = keyValue[0];
                            value = keyValue[1];
                            if (this.Columns[key].DataType == typeof(String))
                            {
                                row[key] = value;
                            }
                            else if (this.Columns[key].DataType == typeof(decimal))
                            {
                                row[key] = Convert.ToDecimal(value);
                            }
                        }

                        this.Rows.Add(row);


                    }
                    catch (Exception e)
                    {
                        MessageBox.Show(e.Message, "Exception");
                    }
                }
                else
                {
                    try
                    {
                        foreach (String item in ((string)message).Split(','))
                        {
                            String key;
                            String value;
                            String[] keyValue = item.Split(':');
                            key = keyValue[0];
                            value = keyValue[1];
                            if (this.Columns[key].DataType == typeof(String))
                            {
                                row[key] = value;
                            }
                            else if (this.Columns[key].DataType == typeof(decimal))
                            {
                                row[key] = Convert.ToDecimal(value);
                            }
                            else if (this.Columns[key].DataType == typeof(DateTime))
                            {
                                row[key] = DateTime.ParseExact(value, "HHmmssfff", CultureInfo.InvariantCulture);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show(e.Message, "Exception");
                    }
                }
            }
Exemplo n.º 19
0
 /// <summary>
 /// 订阅
 /// </summary>
 /// <param name="channel"></param>
 /// <param name="handle"></param>
 public async Task SubscribeAsync(RedisChannel channel, Action <RedisChannel, RedisValue> handle)
 {
     var sub = _connMultiplexer.GetSubscriber();
     await sub.SubscribeAsync(channel, handle);
 }
Exemplo n.º 20
0
        /// <summary>
        /// Handlers the specified redis channel.
        /// </summary>
        /// <param name="redisChannel">The redis channel.</param>
        /// <param name="redisValue">The redis value.</param>
        private void Handler(RedisChannel redisChannel, RedisValue redisValue)
        {
            if (!redisValue.HasValue || _messageId.Id.Value.ToString() != redisValue.ToString())
            {
                return;
            }

            _waitHandle.Set();
        }
Exemplo n.º 21
0
 private void HandleMessage(RedisChannel channel, RedisValue key)
 {
     this.TryDequeueCommand();
 }
Exemplo n.º 22
0
 private void OnNotificationReceived(RedisChannel redisChannel, RedisValue redisValue)
 {
     _logger.Debug("Redis notification received in receiver for {RedisKey}", _redisKey);
     _receiveSemaphore.Release();
 }
 public PubSubReceiver(RedisConfiguration config, string channelName, TraceWriter trace)
 {
     _config = config;
     _channel = new RedisChannel(channelName, RedisChannel.PatternMode.Auto);
     _trace = trace;
 }
Exemplo n.º 24
0
 public async Task SubscribeAsync(RedisChannel channel, Action <RedisChannel, RedisValue> handler)
 {
     await this.subscriber.SubscribeAsync(channel, handler);
 }
Exemplo n.º 25
0
 private void ProcessRedisCacheInstruction(RedisChannel channel, RedisValue message )
 {
     string[] messageParts = message.ToString().Split( ',' );
     if (messageParts.Length > 0 )
     {
         var action = messageParts[0];
         switch ( action )
         {
             case "REMOVE":
                 {
                     if (messageParts.Length > 1 )
                     {
                         RemoveFromMemoryCache( messageParts[1] );
                     }
                     break;
                 }
             case "FLUSH":
                 {
                     FlushMemoryCache();
                     break;
                 }
             case "REFRESH_AUTH_ENTITY":
                 {
                     if ( messageParts.Length > 2 )
                     {
                         Rock.Security.Authorization.RefreshEntity( messageParts[1].AsInteger(), messageParts[2].AsInteger() );
                     }
                     break;
                 }
             case "REFRESH_AUTH_ACTION":
                 {
                     if ( messageParts.Length > 3 )
                     {
                         Rock.Security.Authorization.RefreshAction( messageParts[1].AsInteger(), messageParts[2].AsInteger(), messageParts[3] );
                     }
                     break;
                 }
             case "FLUSH_AUTH":
                 {
                     Rock.Security.Authorization.FlushAuth();
                     break;
                 }
             case "REMOVE_ENTITY_ATTRIBUTES":
                 {
                     Rock.Web.Cache.AttributeCache.RemoveEntityAttributes();
                     break;
                 }
             case "PING":
                 {
                     var response = string.Format( "PONG: From {0}: {1}", System.Environment.MachineName, System.AppDomain.CurrentDomain.FriendlyName );
                     SendRedisCommand( response );
                     break;
                 }
         }
     }
 }
Exemplo n.º 26
0
 public async Task PublishAsync(RedisChannel channel, RedisValue value)
 {
     await this.subscriber.PublishAsync(channel, value);
 }
 private void OnInvalidationMessage(RedisChannel pattern, RedisValue data)
 {
     if (pattern == Constants.DEFAULT_INVALIDATION_CHANNEL)
     {
         this.ProcessInvalidationMessage(data.ToString());
     }
 }
Exemplo n.º 28
0
 public Task <long> PublishAsync(RedisChannel channel, RedisValue message, CommandFlags flags = CommandFlags.None)
 {
     return(Inner.PublishAsync(ToInner(channel), message, flags));
 }
Exemplo n.º 29
0
 private void ChannelPulse(RedisChannel redisChannel, RedisValue redisValue)
 {
     lock (this.acquireLock)
     {
         this.wasPulsed = true;
         Monitor.Pulse(this.acquireLock);
     }
 }
Exemplo n.º 30
0
 protected RedisChannel ToInner(RedisChannel outer)
 {
     return(RedisKey.ConcatenateBytes(Prefix, null, (byte[])outer));
 }
        private async Task TestAsync(Targets targets, CancellationToken token)
        {
            var servers = targets.Slaves.Select(s => new { Host = s, IsMaster = false })
                          .Prepend(new { Host = targets.Master, IsMaster = true })
                          .ToList();

            var muxTasks = servers.Select(n => ConnectionMultiplexer.ConnectAsync(n.Host.ToRedisOptions(Auth)))
                           .ToList();

            Logger.LogInformation("Connecting to {nodeCount} Redis servers...", servers.Count);

            await Task.WhenAll(muxTasks);

            var connections = muxTasks.Select((t, i) => new { Mux = t.Result, Server = servers[i] }).ToList();

            var slavesSubscribed = new CountdownEvent(servers.Count - 1);
            var startEvent       = new ManualResetEventSlim(false);

            var message = Guid.NewGuid().ToString();
            var channel = new RedisChannel(message, RedisChannel.PatternMode.Literal);

            var jobs = connections.Where(c => !c.Server.IsMaster).Select(slave =>
                                                                         Task.Run(() =>
            {
                var stopwatch = new Stopwatch();

                try
                {
                    var msgReceivedEvent = new ManualResetEventSlim(false);
                    slave.Mux.GetSubscriber().Subscribe(channel, (c, v) =>
                    {
                        Logger.LogInformation("{Latency} - Message received by {host}.", stopwatch.Elapsed, slave.Server.Host);
                        msgReceivedEvent.Set();
                    });

                    slavesSubscribed.Signal();
                    startEvent.Wait(token);
                    stopwatch.Start();
                    msgReceivedEvent.Wait(token);
                }
                catch (OperationCanceledException)
                {
                    Logger.LogError("{Latency} - Timed-out: {host}.", stopwatch.Elapsed, slave.Server.Host);
                }
                finally
                {
                    slave.Mux.Dispose();
                }
            }, token)).ToList();

            var master = connections.Single(c => c.Server.IsMaster);
            var db     = master.Mux.GetDatabase();

            slavesSubscribed.Wait(token);

            Logger.LogInformation("Publishing message on master: {host}", master.Server.Host);
            startEvent.Set();
            db.Publish(channel, message);

            await Task.WhenAll(jobs);
        }
Exemplo n.º 32
0
        /// <inheritdoc/>
        public Task <long> PublishAsync <T>(RedisChannel channel, T message, CommandFlags flags = CommandFlags.None)
        {
            var sub = connectionMultiplexer.GetSubscriber();

            return(sub.PublishAsync(channel, Serializer.Serialize(message), flags));
        }
Exemplo n.º 33
0
        public async Task SubscriptionsSurviveMasterSwitchAsync(bool useSharedSocketManager)
        {
            using (var a = Create(allowAdmin: true, useSharedSocketManager: useSharedSocketManager))
                using (var b = Create(allowAdmin: true, useSharedSocketManager: useSharedSocketManager))
                {
                    RedisChannel channel = Me();
                    var          subA    = a.GetSubscriber();
                    var          subB    = b.GetSubscriber();

                    long masterChanged = 0, aCount = 0, bCount = 0;
                    a.ConfigurationChangedBroadcast += delegate
                    {
                        Output.WriteLine("a noticed config broadcast: " + Interlocked.Increment(ref masterChanged));
                    };
                    b.ConfigurationChangedBroadcast += delegate
                    {
                        Output.WriteLine("b noticed config broadcast: " + Interlocked.Increment(ref masterChanged));
                    };
                    subA.Subscribe(channel, (ch, message) =>
                    {
                        Output.WriteLine("a got message: " + message);
                        Interlocked.Increment(ref aCount);
                    });
                    subB.Subscribe(channel, (ch, message) =>
                    {
                        Output.WriteLine("b got message: " + message);
                        Interlocked.Increment(ref bCount);
                    });

                    Assert.False(a.GetServer(TestConfig.Current.MasterServerAndPort).IsSlave, $"{TestConfig.Current.MasterServerAndPort} should be master via a");
                    Assert.True(a.GetServer(TestConfig.Current.SlaveServerAndPort).IsSlave, $"{TestConfig.Current.SlaveServerAndPort} should be slave via a");
                    Assert.False(b.GetServer(TestConfig.Current.MasterServerAndPort).IsSlave, $"{TestConfig.Current.MasterServerAndPort} should be master via b");
                    Assert.True(b.GetServer(TestConfig.Current.SlaveServerAndPort).IsSlave, $"{TestConfig.Current.SlaveServerAndPort} should be slave via b");

                    var epA = subA.SubscribedEndpoint(channel);
                    var epB = subB.SubscribedEndpoint(channel);
                    Output.WriteLine("a: " + EndPointCollection.ToString(epA));
                    Output.WriteLine("b: " + EndPointCollection.ToString(epB));
                    subA.Publish(channel, "a1");
                    subB.Publish(channel, "b1");
                    subA.Ping();
                    subB.Ping();

                    Assert.Equal(2, Interlocked.Read(ref aCount));
                    Assert.Equal(2, Interlocked.Read(ref bCount));
                    Assert.Equal(0, Interlocked.Read(ref masterChanged));

                    try
                    {
                        Interlocked.Exchange(ref masterChanged, 0);
                        Interlocked.Exchange(ref aCount, 0);
                        Interlocked.Exchange(ref bCount, 0);
                        Output.WriteLine("Changing master...");
                        using (var sw = new StringWriter())
                        {
                            a.GetServer(TestConfig.Current.SlaveServerAndPort).MakeMaster(ReplicationChangeOptions.All, sw);
                            Output.WriteLine(sw.ToString());
                        }
                        subA.Ping();
                        subB.Ping();
                        Output.WriteLine("Pausing...");
                        await Task.Delay(6000).ForAwait();

                        Assert.True(a.GetServer(TestConfig.Current.MasterServerAndPort).IsSlave, $"{TestConfig.Current.MasterServerAndPort} should be a slave via a");
                        Assert.False(a.GetServer(TestConfig.Current.SlaveServerAndPort).IsSlave, $"{TestConfig.Current.SlaveServerAndPort} should be a master via a");
                        Assert.True(b.GetServer(TestConfig.Current.MasterServerAndPort).IsSlave, $"{TestConfig.Current.MasterServerAndPort} should be a slave via b");
                        Assert.False(b.GetServer(TestConfig.Current.SlaveServerAndPort).IsSlave, $"{TestConfig.Current.SlaveServerAndPort} should be a master via b");

                        Output.WriteLine("Pause complete");
                        var counters = a.GetCounters();
                        Output.WriteLine("a outstanding: " + counters.TotalOutstanding);
                        counters = b.GetCounters();
                        Output.WriteLine("b outstanding: " + counters.TotalOutstanding);
                        subA.Ping();
                        subB.Ping();
                        await Task.Delay(2000).ForAwait();

                        epA = subA.SubscribedEndpoint(channel);
                        epB = subB.SubscribedEndpoint(channel);
                        Output.WriteLine("a: " + EndPointCollection.ToString(epA));
                        Output.WriteLine("b: " + EndPointCollection.ToString(epB));
                        Output.WriteLine("a2 sent to: " + subA.Publish(channel, "a2"));
                        Output.WriteLine("b2 sent to: " + subB.Publish(channel, "b2"));
                        subA.Ping();
                        subB.Ping();
                        Output.WriteLine("Checking...");

                        Assert.Equal(2, Interlocked.Read(ref aCount));
                        Assert.Equal(2, Interlocked.Read(ref bCount));
                        // Expect 6, because a sees a, but b sees a and b due to replication
                        Assert.Equal(6, Interlocked.CompareExchange(ref masterChanged, 0, 0));
                    }
                    finally
                    {
                        Output.WriteLine("Restoring configuration...");
                        try
                        {
                            a.GetServer(TestConfig.Current.MasterServerAndPort).MakeMaster(ReplicationChangeOptions.All);
                        }
                        catch
                        { }
                    }
                }
        }
 private void Pulse(RedisChannel channel, RedisValue value)
 {
     _inner.Invalidate(value);
 }
Exemplo n.º 35
0
        /// <summary>
        /// 发布(使用序列化)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="channel"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public async Task <long> PublishAsync <T>(RedisChannel channel, T message)
        {
            var sub = _connMultiplexer.GetSubscriber();

            return(await sub.PublishAsync(channel, Serialize(message)));
        }
Exemplo n.º 36
0
        public void SubscriptionsSurviveMasterSwitch(bool useSharedSocketManager)
        {
            using (var a = Create(allowAdmin: true, useSharedSocketManager: useSharedSocketManager))
                using (var b = Create(allowAdmin: true, useSharedSocketManager: useSharedSocketManager))
                {
                    RedisChannel channel = Me();
                    var          subA    = a.GetSubscriber();
                    var          subB    = b.GetSubscriber();

                    long masterChanged = 0, aCount = 0, bCount = 0;
                    a.ConfigurationChangedBroadcast += delegate {
                        Console.WriteLine("a noticed config broadcast: " + Interlocked.Increment(ref masterChanged));
                    };
                    b.ConfigurationChangedBroadcast += delegate {
                        Console.WriteLine("b noticed config broadcast: " + Interlocked.Increment(ref masterChanged));
                    };
                    subA.Subscribe(channel, (ch, message) => {
                        Console.WriteLine("a got message: " + message);
                        Interlocked.Increment(ref aCount);
                    });
                    subB.Subscribe(channel, (ch, message) => {
                        Console.WriteLine("b got message: " + message);
                        Interlocked.Increment(ref bCount);
                    });


                    Assert.IsFalse(a.GetServer(PrimaryServer, PrimaryPort).IsSlave, PrimaryPortString + " is master via a");
                    Assert.IsTrue(a.GetServer(PrimaryServer, SlavePort).IsSlave, SlavePortString + " is slave via a");
                    Assert.IsFalse(b.GetServer(PrimaryServer, PrimaryPort).IsSlave, PrimaryPortString + " is master via b");
                    Assert.IsTrue(b.GetServer(PrimaryServer, SlavePort).IsSlave, SlavePortString + " is slave via b");


                    var epA = subA.SubscribedEndpoint(channel);
                    var epB = subB.SubscribedEndpoint(channel);
                    Console.WriteLine("a: " + EndPointCollection.ToString(epA));
                    Console.WriteLine("b: " + EndPointCollection.ToString(epB));
                    subA.Publish(channel, "a1");
                    subB.Publish(channel, "b1");
                    subA.Ping();
                    subB.Ping();

                    Assert.AreEqual(2, Interlocked.Read(ref aCount), "a");
                    Assert.AreEqual(2, Interlocked.Read(ref bCount), "b");
                    Assert.AreEqual(0, Interlocked.Read(ref masterChanged), "master");

                    try
                    {
                        Interlocked.Exchange(ref masterChanged, 0);
                        Interlocked.Exchange(ref aCount, 0);
                        Interlocked.Exchange(ref bCount, 0);
                        Console.WriteLine("Changing master...");
                        using (var sw = new StringWriter())
                        {
                            a.GetServer(PrimaryServer, SlavePort).MakeMaster(ReplicationChangeOptions.All, sw);
                            Console.WriteLine(sw);
                        }
                        subA.Ping();
                        subB.Ping();
                        Console.WriteLine("Pausing...");
                        Thread.Sleep(2000);

                        Assert.IsTrue(a.GetServer(PrimaryServer, PrimaryPort).IsSlave, PrimaryPortString + " is slave via a");
                        Assert.IsFalse(a.GetServer(PrimaryServer, SlavePort).IsSlave, SlavePortString + " is master via a");
                        Assert.IsTrue(b.GetServer(PrimaryServer, PrimaryPort).IsSlave, PrimaryPortString + " is slave via b");
                        Assert.IsFalse(b.GetServer(PrimaryServer, SlavePort).IsSlave, SlavePortString + " is master via b");

                        Console.WriteLine("Pause complete");
                        var counters = a.GetCounters();
                        Console.WriteLine("a outstanding: " + counters.TotalOutstanding);
                        counters = b.GetCounters();
                        Console.WriteLine("b outstanding: " + counters.TotalOutstanding);
                        subA.Ping();
                        subB.Ping();
                        epA = subA.SubscribedEndpoint(channel);
                        epB = subB.SubscribedEndpoint(channel);
                        Console.WriteLine("a: " + EndPointCollection.ToString(epA));
                        Console.WriteLine("b: " + EndPointCollection.ToString(epB));
                        Console.WriteLine("a2 sent to: " + subA.Publish(channel, "a2"));
                        Console.WriteLine("b2 sent to: " + subB.Publish(channel, "b2"));
                        subA.Ping();
                        subB.Ping();
                        Console.WriteLine("Checking...");

                        Assert.AreEqual(2, Interlocked.Read(ref aCount), "a");
                        Assert.AreEqual(2, Interlocked.Read(ref bCount), "b");
                        Assert.AreEqual(4, Interlocked.CompareExchange(ref masterChanged, 0, 0), "master");
                    }
                    finally
                    {
                        Console.WriteLine("Restoring configuration...");
                        try
                        {
                            a.GetServer(PrimaryServer, PrimaryPort).MakeMaster(ReplicationChangeOptions.All);
                        }
                        catch
                        { }
                    }
                }
        }
Exemplo n.º 37
0
 public long Publish(RedisChannel channel, RedisValue message, CommandFlags flags = CommandFlags.None)
 {
     return(Inner.Publish(ToInner(channel), message, flags));
 }
Exemplo n.º 38
0
 private void OnTopicMessage(RedisChannel redisChannel, RedisValue redisValue)
 {
     Log.Trace().Message("Queue OnMessage {0}: {1}", _queueName, redisValue).Write();
     _autoEvent.Set();
 }
Exemplo n.º 39
0
 public RedisChannelReceiver(RedisConfiguration configuration, IRedisAttribute attribute)
 {
     _configuration = configuration;
     _channel       = new RedisChannel(attribute.ChannelOrKey, RedisChannel.PatternMode.Auto);
     _service       = _configuration.RedisServiceFactory.CreateService(_configuration.ResolveConnectionString(attribute.ConnectionStringSetting));
 }
Exemplo n.º 40
0
 private void OnQueue(RedisChannel channel, RedisValue message)
 {
     if (m_queue.Count > 0)
     {
     }
 }
Exemplo n.º 41
0
        /// <summary>
        /// 订阅
        /// </summary>
        /// <param name="channel">通道名称</param>
        /// <param name="handle"></param>
        public void Subscribe(RedisChannel channel, Action <RedisChannel, RedisValue> handle)
        {
            var sub = _connMultiplexer.GetSubscriber();

            sub.Subscribe(channel, handle);
        }
Exemplo n.º 42
0
 private void OnMessage(RedisChannel channel, RedisValue message)
 {
 }
Exemplo n.º 43
0
        /// <summary>
        /// 发布(使用序列化)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="channel"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public long Publish <T>(RedisChannel channel, T message)
        {
            var sub = _connMultiplexer.GetSubscriber();

            return(sub.Publish(channel, SerializeObject(message)));
        }
Exemplo n.º 44
0
 /// <summary>
 /// Handles unfollow_stream pubs to keep the counter up to date.
 /// When counter reaches 0, stream is removed from tracking because
 /// that means no guilds are subscribed to that stream anymore
 /// </summary>
 private void HandleUnfollowStream(RedisChannel ch, RedisValue val)
 => Task.Run(() =>
 {
Exemplo n.º 45
0
        /// <summary>
        /// 发布
        /// </summary>
        /// <param name="channel"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public async Task <long> PublishAsync(RedisChannel channel, RedisValue message)
        {
            var sub = _connMultiplexer.GetSubscriber();

            return(await sub.PublishAsync(channel, message));
        }
Exemplo n.º 46
0
 private void OnKeyRemoveFromRedis(RedisChannel redisChannel,
                                   RedisValue redisValue)
 {
     SendOnItemRemoveFromCacheEvent(redisValue);
 }
Exemplo n.º 47
0
        public async Task PubSubGetAllCorrectOrder()
        {
            using (var muxer = Create(configuration: TestConfig.Current.RemoteServerAndPort, syncTimeout: 20000, log: Writer))
            {
                var          sub      = muxer.GetSubscriber();
                RedisChannel channel  = Me();
                const int    count    = 250;
                var          syncLock = new object();

                var data       = new List <int>(count);
                var subChannel = await sub.SubscribeAsync(channel).ForAwait();

                await sub.PingAsync().ForAwait();

                async Task RunLoop()
                {
                    while (!subChannel.Completion.IsCompleted)
                    {
                        var work = await subChannel.ReadAsync().ForAwait();

                        int i = int.Parse(Encoding.UTF8.GetString(work.Message));
                        lock (data)
                        {
                            data.Add(i);
                            if (data.Count == count)
                            {
                                break;
                            }
                            if ((data.Count % 100) == 99)
                            {
                                Log("Received: " + data.Count.ToString());
                            }
                        }
                    }
                    lock (syncLock)
                    {
                        Log("PulseAll.");
                        Monitor.PulseAll(syncLock);
                    }
                }

                lock (syncLock)
                {
                    // Intentionally not awaited - running in parallel
                    _ = Task.Run(RunLoop);
                    for (int i = 0; i < count; i++)
                    {
                        sub.Publish(channel, i.ToString());
                        if ((i % 100) == 99)
                        {
                            Log("Published: " + i.ToString());
                        }
                    }
                    Log("Send loop complete.");
                    if (!Monitor.Wait(syncLock, 20000))
                    {
                        throw new TimeoutException("Items: " + data.Count);
                    }
                    Log("Unsubscribe.");
                    subChannel.Unsubscribe();
                    Log("Sub Ping.");
                    sub.Ping();
                    Log("Database Ping.");
                    muxer.GetDatabase().Ping();
                    for (int i = 0; i < count; i++)
                    {
                        Assert.Equal(i, data[i]);
                    }
                }

                Log("Awaiting completion.");
                await subChannel.Completion;
                Log("Completion awaited.");
                await Assert.ThrowsAsync <ChannelClosedException>(async delegate
                {
                    await subChannel.ReadAsync().ForAwait();
                }).ForAwait();

                Log("End of muxer.");
            }
            Log("End of test.");
        }
Exemplo n.º 48
0
        public async Task PubSubGetAllCorrectOrder_OnMessage_Async()
        {
            using (var muxer = Create(configuration: TestConfig.Current.RemoteServerAndPort, syncTimeout: 20000, log: Writer))
            {
                var          sub      = muxer.GetSubscriber();
                RedisChannel channel  = Me();
                const int    count    = 1000;
                var          syncLock = new object();

                var data       = new List <int>(count);
                var subChannel = await sub.SubscribeAsync(channel).ForAwait();

                subChannel.OnMessage(msg =>
                {
                    int i      = int.Parse(Encoding.UTF8.GetString(msg.Message));
                    bool pulse = false;
                    lock (data)
                    {
                        data.Add(i);
                        if (data.Count == count)
                        {
                            pulse = true;
                        }
                        if ((data.Count % 100) == 99)
                        {
                            Log("Received: " + data.Count.ToString());
                        }
                    }
                    if (pulse)
                    {
                        lock (syncLock)
                        {
                            Monitor.PulseAll(syncLock);
                        }
                    }
                    return(i % 2 == 0 ? null : Task.CompletedTask);
                });
                await sub.PingAsync().ForAwait();

                // Give a delay between subscriptions and when we try to publish to be safe
                await Task.Delay(1000).ForAwait();

                lock (syncLock)
                {
                    for (int i = 0; i < count; i++)
                    {
                        sub.Publish(channel, i.ToString(), CommandFlags.FireAndForget);
                        if ((i % 100) == 99)
                        {
                            Log("Published: " + i.ToString());
                        }
                    }
                    Log("Send loop complete.");
                    if (!Monitor.Wait(syncLock, 20000))
                    {
                        throw new TimeoutException("Items: " + data.Count);
                    }
                    Log("Unsubscribe.");
                    subChannel.Unsubscribe();
                    Log("Sub Ping.");
                    sub.Ping();
                    Log("Database Ping.");
                    muxer.GetDatabase().Ping();
                    for (int i = 0; i < count; i++)
                    {
                        Assert.Equal(i, data[i]);
                    }
                }

                Log("Awaiting completion.");
                await subChannel.Completion;
                Log("Completion awaited.");
                Assert.True(subChannel.Completion.IsCompleted);
                await Assert.ThrowsAsync <ChannelClosedException>(async delegate
                {
                    await subChannel.ReadAsync().ForAwait();
                }).ForAwait();

                Log("End of muxer.");
            }
            Log("End of test.");
        }
 internal void Write(RedisChannel channel)
 {
     WriteUnified(outStream, ChannelPrefix, channel.Value);
 }
Exemplo n.º 50
0
        public async Task SubscriptionsSurviveConnectionFailureAsync()
        {
            using (var muxer = (Create(allowAdmin: true, shared: false, log: Writer, syncTimeout: 1000) as ConnectionMultiplexer) !)
            {
                var          profiler = muxer.AddProfiler();
                RedisChannel channel  = Me();
                var          sub      = muxer.GetSubscriber();
                int          counter  = 0;
                Assert.True(sub.IsConnected());
                await sub.SubscribeAsync(channel, delegate
                {
                    Interlocked.Increment(ref counter);
                }).ConfigureAwait(false);

                var profile1 = Log(profiler);

                Assert.Equal(1, muxer.GetSubscriptionsCount());

                await Task.Delay(200).ConfigureAwait(false);

                await sub.PublishAsync(channel, "abc").ConfigureAwait(false);

                sub.Ping();
                await Task.Delay(200).ConfigureAwait(false);

                var counter1 = Thread.VolatileRead(ref counter);
                Log($"Expecting 1 message, got {counter1}");
                Assert.Equal(1, counter1);

                var server      = GetServer(muxer);
                var socketCount = server.GetCounters().Subscription.SocketCount;
                Log($"Expecting 1 socket, got {socketCount}");
                Assert.Equal(1, socketCount);

                // We might fail both connections or just the primary in the time period
                SetExpectedAmbientFailureCount(-1);

                // Make sure we fail all the way
                muxer.AllowConnect = false;
                Log("Failing connection");
                // Fail all connections
                server.SimulateConnectionFailure(SimulatedFailureType.All);
                // Trigger failure (RedisTimeoutException because of backlog behavior)
                Assert.Throws <RedisTimeoutException>(() => sub.Ping());
                Assert.False(sub.IsConnected(channel));

                // Now reconnect...
                muxer.AllowConnect = true;
                Log("Waiting on reconnect");
                // Wait until we're reconnected
                await UntilConditionAsync(TimeSpan.FromSeconds(10), () => sub.IsConnected(channel));

                Log("Reconnected");
                // Ensure we're reconnected
                Assert.True(sub.IsConnected(channel));

                // Ensure we've sent the subscribe command after reconnecting
                var profile2 = Log(profiler);
                //Assert.Equal(1, profile2.Count(p => p.Command == nameof(RedisCommand.SUBSCRIBE)));

                Log("Issuing ping after reconnected");
                sub.Ping();

                var muxerSubCount = muxer.GetSubscriptionsCount();
                Log($"Muxer thinks we have {muxerSubCount} subscriber(s).");
                Assert.Equal(1, muxerSubCount);

                var muxerSubs = muxer.GetSubscriptions();
                foreach (var pair in muxerSubs)
                {
                    var muxerSub = pair.Value;
                    Log($"  Muxer Sub: {pair.Key}: (EndPoint: {muxerSub.GetCurrentServer()}, Connected: {muxerSub.IsConnected})");
                }

                Log("Publishing");
                var published = await sub.PublishAsync(channel, "abc").ConfigureAwait(false);

                Log($"Published to {published} subscriber(s).");
                Assert.Equal(1, published);

                // Give it a few seconds to get our messages
                Log("Waiting for 2 messages");
                await UntilConditionAsync(TimeSpan.FromSeconds(5), () => Thread.VolatileRead(ref counter) == 2);

                var counter2 = Thread.VolatileRead(ref counter);
                Log($"Expecting 2 messages, got {counter2}");
                Assert.Equal(2, counter2);

                // Log all commands at the end
                Log("All commands since connecting:");
                var profile3 = profiler.FinishProfiling();
                foreach (var command in profile3)
                {
                    Log($"{command.EndPoint}: {command}");
                }
            }
        }
        private void onExpireItemCache(RedisChannel channel, RedisValue value)
        {
            log(Logging.LoggingLevel.Detailed, "Received Expire Item: {0}", value.ToJson());

            var dict = value.ToString().ToObject<Dictionary<string, string>>();
            if (OnExpireItem != null)
                OnExpireItem(dict["cacheName"], dict["key"], dict["itemKey"]);
        }
Exemplo n.º 52
0
        public static IDivideAndConquerPublisher GetDivideAndConquerPublisher(this ConnectionMultiplexer redis, RedisChannel channel)
        {
            var dict = Cache.GetValue(redis, _ => new ConcurrentDictionary <RedisChannel, IDivideAndConquerPublisher>());

            return(dict.GetOrAdd(channel, ch => new DivideAndConquerPublisher(redis, ch)));
        }
 private void OnKeySpaceNotificationMessage(RedisChannel pattern, RedisValue data)
 {
     var prefix = pattern.ToString().Substring(0, 10);
     switch (prefix)
     {
         case "__keyevent":
             this.ProcessInvalidationMessage(data.ToString());
             break;
         default:
             //nop
             break;
     }
 }
Exemplo n.º 54
0
 private void MessageSignalReceivedHandler(RedisChannel channel, RedisValue redisValue)
 {
     //Cancel the pumps delay
     _pumpDelayCancellationTokenSource.Cancel();
 }
 public MessageCompletable(RedisChannel channel, RedisValue message, Action<RedisChannel, RedisValue> handler)
 {
     this.channel = channel;
     this.message = message;
     this.handler = handler;
 }
        private void MasterWasSwitched(RedisChannel redisChannel, RedisValue redisValue)
        {
            if (redisValue.IsNullOrEmpty) return;
              var message = redisValue.ToString();
              var messageParts = message.Split(' ');
              var ip = IPAddress.Parse(messageParts[3]);
              var port = int.Parse(messageParts[4]);
              EndPoint newMasterEndpoint = new IPEndPoint(ip, port);
              if (_configOption.EndPoints.Any() && newMasterEndpoint == _configOption.EndPoints[0]) return;
              _configOption.EndPoints.Clear();
              _configOption.EndPoints.Add(newMasterEndpoint);

              _redisMultiplexer = LogUtility.logger == null
            ? ConnectionMultiplexer.Connect(_configOption)
            : ConnectionMultiplexer.Connect(_configOption, LogUtility.logger);

              _connection = _redisMultiplexer.GetDatabase(_configuration.DatabaseId);
        }