Пример #1
0
        private void OnMessage(RefreshVouchers message)
        {
            this.vouchers = this.limit;

            if (this.queue.Count <= 0)
            {
                this.IsIdle = true;
#if DEBUG
                this.Logger.LogTrace("Idle.");
#endif

                return;
            }

            Task.Run(this.RunTimer);

            if (this.IsIdle)
            {
                this.IsIdle = false;
#if DEBUG
                this.Logger.LogTrace("Active.");
#endif
            }

            this.ProcessQueue();
        }
Пример #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Throttler{T}"/> class.
        /// </summary>
        /// <param name="container">The componentry container.</param>
        /// <param name="receiver">The throttled object receiver.</param>
        /// <param name="intervalDuration">The throttle timer interval.</param>
        /// <param name="limit">The message limit per interval.</param>
        /// <param name="subName">The sub-name for the throttler.</param>
        public Throttler(
            IComponentryContainer container,
            Action <T> receiver,
            Duration intervalDuration,
            int limit,
            string subName)
            : base(container, subName)
        {
            Condition.PositiveInt32((int)intervalDuration.TotalMilliseconds, nameof(intervalDuration.TotalMilliseconds));
            Condition.PositiveInt32(limit, nameof(limit));

            this.receiver = receiver;
            this.interval = intervalDuration.ToTimeSpan();
            this.refresh  = new RefreshVouchers();
            this.limit    = limit;
            this.queue    = new Queue <T>();

            this.IsIdle     = true;
            this.vouchers   = limit;
            this.totalCount = 0;

            this.RegisterHandler <RefreshVouchers>(this.OnMessage);
            this.RegisterHandler <T>(this.OnMessage);
        }