Ejemplo n.º 1
0
        /// <summary>
        /// Adds a new request to the batch processor for processing
        /// </summary>
        /// <param name="request">Request to be processed</param>
        /// <param name="processImmediate">When set to true, the current batch will be processed as soon as the request is enqueued</param>
        /// <returns>A Task of TResponse which will be completed when the item has been processed</returns>
        public async Task <TResponse> EnqueueAsync(TRequest request, bool processImmediate = false)
        {
            if (request == null)
            {
                return(default(TResponse));
            }

            var newItem = new InternalBatchProcessorItem(request);

            using (var releaser = await queueLock.LockAsync().ConfigureAwait(false))
            {
                if (queue == null)
                {
                    queue = new List <InternalBatchProcessorItem>();
                }

                queue.Add(newItem);

                queueLatestItemStopwatch = Stopwatch.StartNew();
                if (processImmediate || (this.MaximumCount > 0 && queue.Count >= this.MaximumCount))
                {
                    //We've hit our maximum count - fire our batch process now and reset the current queue
                    batchProcessor.Add(queue);
                    queue = new List <InternalBatchProcessorItem>();

                    queueLatestItemStopwatch.Reset();
                    queueFirstItemStopwatch.Reset();
                }
                else if (queue.Count == 1)
                {
                    //Begin our monitoring timer
                    queueFirstItemStopwatch = queueLatestItemStopwatch;
                    batchTimer = new Timer((state) =>
                    {
                        Task t = OnTimerElapsed(state);
                    },
                                           null, this.MinimumTimeInterval, this.MinimumTimeInterval);
                }
            }

            return(await newItem.Task);
        }
Ejemplo n.º 2
0
 internal BatchProcessorRequest(InternalBatchProcessorItem source)
 {
     this.source = source;
 }