Example #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="BufferedEventPublisher{TEntry}" /> class.
        /// </summary>
        /// <param name="sinkId">An identifier for the sink.</param>
        /// <param name="eventPublisher">The event publisher.</param>
        /// <param name="bufferingInterval">The buffering interval.</param>
        /// <param name="bufferingCount">The buffering count.</param>
        /// <param name="maxBufferSize">The maximum number of entries that can be buffered before the sink starts dropping entries.</param>
        /// <param name="cancellationToken">Cancels any pending operation.</param>
        /// <exception cref="System.ArgumentOutOfRangeException">BufferingCount out of range.</exception>
        /// <exception cref="System.ArgumentException">Argument valdation error.</exception>
        private BufferedEventPublisher(string sinkId, Func <IList <TEntry>, Task <int> > eventPublisher, TimeSpan bufferingInterval, int bufferingCount, int maxBufferSize, CancellationToken cancellationToken)
        {
            Guard.ArgumentNotNullOrEmpty(sinkId, "sinkId");
            Guard.ArgumentNotNull(eventPublisher, "eventPublisherAction");
            Guard.ArgumentGreaterOrEqualThan(500, maxBufferSize, "maxBufferSize");
            Guard.ArgumentNotNull(cancellationToken, "cancellationToken");
            Guard.ArgumentGreaterOrEqualThan(0, bufferingCount, "bufferingCount");
            Guard.ArgumentIsValidTimeout(bufferingInterval, "bufferingInterval");

            if (maxBufferSize < (bufferingCount * 3) && bufferingCount != int.MaxValue)
            {
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.MaxBufferSizeShouldBeLargerThanBufferingCount, maxBufferSize, bufferingCount), "maxBufferSize");
            }

            // throw if not auto flush parameter was supplied
            if (bufferingInterval == Timeout.InfiniteTimeSpan && bufferingCount == 0)
            {
                throw new ArgumentException(Resources.InvalidBufferingArguments);
            }

            this.sinkId         = sinkId;
            this.eventPublisher = eventPublisher;
            this.bufferingCount = bufferingCount;
            //// set minimal interval if less than default value (MinimumInterval).
            this.bufferingInterval       = (bufferingInterval < MinimumInterval && bufferingInterval != Timeout.InfiniteTimeSpan) ? MinimumInterval : bufferingInterval;
            this.maxBufferSize           = maxBufferSize;
            this.maxBatchSize            = this.bufferingCount == 0 ? this.maxBufferSize : this.bufferingCount;
            this.cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(new[] { cancellationToken });

            this.buffer = new BlockingCollection <TEntry>(this.maxBufferSize);
        }
Example #2
0
            public ExponentialBackoff(TimeSpan minBackoff, TimeSpan maxBackoff, TimeSpan deltaBackoff)
            {
                Guard.ArgumentGreaterOrEqualThan(0, minBackoff.TotalMilliseconds, "minBackoff");
                Guard.ArgumentGreaterOrEqualThan(0, maxBackoff.TotalMilliseconds, "maxBackoff");
                Guard.ArgumentGreaterOrEqualThan(0, deltaBackoff.TotalMilliseconds, "deltaBackoff");
                Guard.ArgumentGreaterOrEqualThan(minBackoff.TotalMilliseconds, maxBackoff.TotalMilliseconds, "maxBackoff");

                this.minBackoffMilliseconds   = minBackoff.TotalMilliseconds;
                this.maxBackoffMilliseconds   = maxBackoff.TotalMilliseconds;
                this.deltaBackoffMilliseconds = deltaBackoff.TotalMilliseconds;
            }