Example #1
0
        private static async Task RunConsumer(IConsumer <TC> parallelConsumer,
                                              IConsumerBuffer <TP> feed, IDataAdapter <TP, TC> adapter,
                                              CancellationToken token, CancellationTokenSource tokenSrc)
        {
            try
            {
                using (parallelConsumer)
                {
                    await parallelConsumer.InitAsync().StartIfNeeded().ConfigureAwait(false);

                    token.ThrowIfCancellationRequested();
                    while (adapter.TryGet(feed, token, out var consumable))
                    {
                        await parallelConsumer.ConsumeAsync(consumable, token).StartIfNeeded().ConfigureAwait(false);
                    }
                }
            }
            catch
            {
                if (!token.IsCancellationRequested)
                {
                    tokenSrc.Cancel();
                }
                throw;
            }
        }
Example #2
0
 internal static Task RunConsumers(IReadOnlyList <IConsumer <TC> > consumers,
                                   IConsumerBuffer <TP> feed, IDataAdapter <TP, TC> adapter,
                                   CancellationToken token, CancellationTokenSource tokenSrc)
 {
     return(new Func <int, CancellationToken, Task>(async(i, t) =>
                                                    await RunConsumer(consumers[i], feed, adapter, t, tokenSrc).ConfigureAwait(false))
            .WhenAll(consumers.Count, token));
 }
Example #3
0
        /// <summary>
        /// Standardized implementation to recover single item from buffer.
        /// Calls <see cref="Adapt"/> to perform the desired transformed.
        /// <para>Returns true when a consumable instance can be created from
        /// <paramref name="producerDataFeed"/> else returns false.</para>
        /// </summary>
        /// <param name="producerDataFeed">Feed that is getting populated by producers.</param>
        /// <param name="token">Cancellation token</param>
        /// <param name="consumable">transfomed item that can be passed to consumer</param>
        public bool TryGet(IConsumerBuffer <TP> producerDataFeed, CancellationToken token, out TC consumable)
        {
            if (producerDataFeed.TryGet(Timeout.Infinite, token, out var produced))
            {
                consumable = Adapt(produced, token);
                return(true);
            }

            consumable = default;
            return(false);
        }
        protected AbstractConsumer(IConsumerBuffer buffer)
        {
            Guard.ArgumentNotNull(buffer, buffer.GetType().FullName);
            Buffer = buffer;

            _cts = new CancellationTokenSource();
            _threadPool = new ThreadPool();
            _isTaskStart = new ThreadSafe.Boolean(false);
            
            ConfigUtil.Instance.NotifyPropertyChange += OnNotifyPropertyChange;
#if DEBUG
            this.debugLog = ObjectFactory.Current.Get<IDebugLogWriter>(Lifetime.ContainerControlled);
#endif 
        }
 /// <summary>
 /// Return true when a consumable instance can be created from
 /// <paramref name="producerDataFeed"/> else returns false.
 /// <para>NOTE:</para>
 /// <para>1. It does NOT observes <seealso cref="IConsumerBuffer{T}.Finished"/> status</para>
 /// <para>2. The first element is ALWAYS (irrespective of provided timeout value) waited for
 /// <seealso cref="Timeout.Infinite"/>. Then items are added to list as long as they can be recovered
 /// before provided millisecond timeout is reached.</para>
 /// <para>3. Further, items are recovered and populated as long as they are available without
 /// any wait. Then the list if finalized and returned. Step 2 and 3 are same when inifinite timeout
 /// is suplied.</para>
 /// <para>4. If method returns true, then the list would contains at least 1 item.</para>
 /// <para>5. List max size would be respected as provided in Ctor.</para>
 /// </summary>
 /// <param name="producerDataFeed">Data feed</param>
 /// <param name="token">token to observe</param>
 /// <param name="consumable">consumable data instance</param>
 public bool TryGet(IConsumerBuffer <TP> producerDataFeed, CancellationToken token, out List <TC> consumable)
 {
     consumable = default(List <TC>);
     if (!producerDataFeed.TryGet(Timeout.Infinite, token, out var value))
     {
         return(false);
     }
     consumable = new List <TC>(_maxListSize)
     {
         Adapt(value, token)
     };
     return(_millisecTimeout == Timeout.Infinite
         ? TryGetWithInfiniteTo(producerDataFeed, token, consumable)
         : TryGetWithFiniteTo(producerDataFeed, token, consumable));
 }
        private bool TryGetWithInfiniteTo(IConsumerBuffer <TP> producerDataFeed, CancellationToken token,
                                          ICollection <TC> consumable)
        {
            while (consumable.Count < _maxListSize)
            {
                if (producerDataFeed.TryGet(Timeout.Infinite, token, out var value))
                {
                    consumable.Add(Adapt(value, token));
                }
                else
                {
                    return(true);
                }
            }

            return(true);
        }
        private bool TryGetWithFiniteTo(IConsumerBuffer <TP> producerDataFeed, CancellationToken token,
                                        ICollection <TC> consumable)
        {
            var timeRemains = _millisecTimeout;
            var sw          = Stopwatch.StartNew();

            while (consumable.Count < _maxListSize)
            {
                if (producerDataFeed.TryGet(timeRemains, token, out var value))
                {
                    consumable.Add(Adapt(value, token));
                    if (timeRemains != 0)
                    {
                        timeRemains = (int)Math.Max(0, _millisecTimeout - sw.ElapsedMilliseconds);
                    }
                }
                else
                {
                    return(true);
                }
            }

            return(true);
        }
Example #8
0
 public QueueConsumer(IConsumerBuffer buffer) : base(buffer) { }
Example #9
0
 public TopicConsumer(IConsumerBuffer buffer) : base(buffer) { }
 public DeadLetterConsumer(IConsumerBuffer buffer) : base(buffer) { }