예제 #1
0
        //移除不活跃的订阅(默认24小时前)
        protected void RemoveSubscriber(IEventStorage storage, EventMessage subscriber)
        {
            //判断队列是否有消息
            var queueName = subscriber.ChannelID;
            var qkey      = this.SubscriberQueuePrefix + queueName;
            var qlength   = storage.GetLength(qkey);

            if (qlength <= 0)             //没有任何消息,忽略(实际上此队列在Redis中是不存在的)
            {
                return;
            }
            //24小时前
            var now      = DateTime.Now;
            var minTicks = subscriber.Priority > 0 ? now.AddSeconds(-subscriber.Priority).Ticks : now.AddHours(-24).Ticks;
            //获取最后一次活跃时间
            var qfkey  = this.SubscriberRefKeyPrefix + queueName;
            var bytes  = storage.Get(qfkey);
            var remove = true;

            if (bytes != null && bytes.Length > 0)
            {
                var ticks = BitConverter.ToInt64(bytes, 0);
                if (ticks > minTicks)                 //还是活跃的
                {
                    remove = false;
                }
            }
            if (!remove)             //此订阅还是活跃的,忽略
            {
                return;
            }
            //把订阅信息移除
            var sk = this.SubscriberKeyPrefix + queueName;

            storage.Delete(sk);
            //把队列里的消息引用数-1
            var mlist = new List <string>();

            do
            {
                bytes = storage.RightPop(qkey);
                if (bytes != null && bytes.Length > 0)
                {
                    var messageId = bytes.ToGuidString();
                    mlist.Add(this.MessageRefPrefix + messageId);
                }
            } while(bytes != null);
            //把订阅队列移除
            storage.Pipeline(s => {
                foreach (var msgid in mlist)
                {
                    s.Decrement(msgid);
                }
                s.Delete(qkey);
            });
        }