Ejemplo n.º 1
0
        private TimeSpan IncreaseDelay(TimeSpan delay)
        {
            var multiplier     = minDelayMultiplier + ThreadSafeRandom.NextDouble() * (maxDelayMultiplier - minDelayMultiplier);
            var increasedDelay = delay.Multiply(multiplier);

            return(TimeSpanExtensions.Min(TimeSpanExtensions.Max(minDelay, increasedDelay), maxDelay));
        }
Ejemplo n.º 2
0
        public TimeSpan Next(int attempt)
        {
            TimeSpan currentDelay;

            try
            {
                long multiple = checked (1 << attempt);

                currentDelay = _step.Multiply(multiple);

                if (currentDelay <= TimeSpan.Zero)
                {
                    throw new OverflowException();
                }
            }
            catch (OverflowException)
            {
                currentDelay = _maxDelay;
            }

            currentDelay = TimeSpanExtensions.Min(currentDelay, _maxDelay);

            if (_minDelay >= currentDelay)
            {
                throw new ArgumentOutOfRangeException($"minDelay {_minDelay}, currentDelay = {currentDelay}");
            }

            return(_random.NextTimeSpan(_minDelay, currentDelay));
        }
Ejemplo n.º 3
0
        public TimeSpan GetTimeout(Request request, IRequestTimeBudget budget, int currentReplicaIndex, int totalReplicas)
        {
            if (currentReplicaIndex >= providers.Length)
            {
                return(tailBehaviour == TailTimeoutBehaviour.UseRemainingBudget
                    ? budget.Remaining
                    : TimeSpanExtensions.Min(providers.Last()(), budget.Remaining));
            }

            return(TimeSpanExtensions.Min(providers[currentReplicaIndex](), budget.Remaining));
        }
Ejemplo n.º 4
0
        public TimeSpan GetRetryDelay(int attemptsUsed)
        {
            var delay = InitialRetryDelay.Multiply(Math.Pow(RetryDelayMultiplier, Math.Max(0, attemptsUsed - 1)));

            var jitterAmount = delay.Multiply(ThreadSafeRandom.NextDouble() * Jitter);

            if (ThreadSafeRandom.NextDouble() <= 0.5)
            {
                jitterAmount = jitterAmount.Negate();
            }

            return(TimeSpanExtensions.Min(MaximumRetryDelay, delay + jitterAmount));
        }
        public async Task SendAsync(Request request, IRequestSender sender, IRequestTimeBudget budget, IEnumerable <Uri> replicas, int replicasCount, CancellationToken cancellationToken)
        {
            var currentReplicaIndex = 0;

            foreach (var replica in replicas)
            {
                if (budget.HasExpired)
                {
                    break;
                }

                var timeout = TimeSpanExtensions.Min(timeoutsProvider.GetTimeout(request, budget, currentReplicaIndex++, replicasCount), budget.Remaining);

                var result = await sender.SendToReplicaAsync(replica, request, timeout, cancellationToken).ConfigureAwait(false);

                if (result.Verdict == ResponseVerdict.Accept)
                {
                    break;
                }

                cancellationToken.ThrowIfCancellationRequested();
            }
        }
Ejemplo n.º 6
0
        public TimeSpan Next(int attempt)
        {
            TimeSpan currentDelay;

            try
            {
                long multiple = checked (1 << attempt);

                currentDelay = _step.Multiply(multiple);

                if (currentDelay <= TimeSpan.Zero)
                {
                    throw new OverflowException();
                }
            }
            catch (OverflowException)
            {
                currentDelay = _maxDelay;
            }

            currentDelay = TimeSpanExtensions.Min(currentDelay, _maxDelay);

            return(TimeSpanExtensions.Max(_minDelay, currentDelay));
        }
Ejemplo n.º 7
0
        private void ExtractMedia()
        {
            if (MediaDecoder == null)
            {
                throw new InvalidOperationException("The Media Codec Extractor has not been initialized");
            }

            if (!isInitialized)
            {
                throw new InvalidOperationException("The Media Codec has not been initialized for a media");
            }

            var bufferInfo      = new MediaCodec.BufferInfo();
            var waitDefaultTime = TimeSpan.FromMilliseconds(10);

            MediaDecoder.Start();
            while (true)
            {
                var waitTime = waitDefaultTime; // time to wait at the end of the loop iteration

                //Process the commands
                if (ProcessCommandsAndUpdateCurrentState())
                {
                    waitTime = TimeSpan.Zero;
                }

                // terminate the thread on disposal
                if (currentState == SchedulerAsyncCommandEnum.Dispose)
                {
                    return;
                }

                //=================================================================================================
                //Extract video inputs
                if (!inputExtractionDone)
                {
                    int inputBufIndex = MediaDecoder.DequeueInputBuffer(0);
                    if (inputBufIndex >= 0)
                    {
                        waitTime = TimeSpan.Zero;
                        var inputBuffer = MediaDecoder.GetInputBuffer(inputBufIndex);

                        // Read the sample data into the ByteBuffer.  This neither respects nor updates inputBuf's position, limit, etc.
                        int chunkSize = mediaExtractor.ReadSampleData(inputBuffer, 0);
                        if (chunkSize > 0)
                        {
                            if (mediaExtractor.SampleTrackIndex != mediaTrackIndex)
                            {
                                throw new Exception($"Got media sample from track {mediaExtractor.SampleTrackIndex}, track expected {mediaTrackIndex}");
                            }

                            MediaDecoder.QueueInputBuffer(inputBufIndex, 0, chunkSize, mediaExtractor.SampleTime, 0);
                            mediaExtractor.Advance();
                        }
                        else // End of stream -- send empty frame with EOS flag set.
                        {
                            MediaDecoder.QueueInputBuffer(inputBufIndex, 0, 0, 0L, MediaCodecBufferFlags.EndOfStream);
                            inputExtractionDone = true;
                        }
                    }
                    else
                    {
                        //do nothing: the input buffer queue is full (we need to output them first)
                    }
                }

                //=================================================================================================
                // Process the output buffers
                if (ShouldProcessDequeueOutput(ref waitTime))
                {
                    int indexOutput = MediaDecoder.DequeueOutputBuffer(bufferInfo, 0);
                    switch (indexOutput)
                    {
                    case (int)MediaCodecInfoState.TryAgainLater:        // decoder not ready yet (haven't processed input yet)
                    case (int)MediaCodecInfoState.OutputBuffersChanged: //deprecated: we just ignore it
                        break;

                    case (int)MediaCodecInfoState.OutputFormatChanged:
                        Logger.Verbose("decoder output format changed: " + MediaDecoder.OutputFormat.ToString());
                        break;

                    default:     // the index of the output buffer

                        if (indexOutput < 0)
                        {
                            Logger.Warning("unexpected index from decoder.dequeueOutputBuffer: " + indexOutput);
                            isEOF = true;
                            break;
                        }

                        if ((bufferInfo.Flags & MediaCodecBufferFlags.EndOfStream) != 0)
                        {
                            isEOF = true;
                            MediaDecoder.ReleaseOutputBuffer(indexOutput, false);
                            break;
                        }

                        MediaCurrentTime = TimeSpanExtensions.FromMicroSeconds(bufferInfo.PresentationTimeUs);

                        ProcessOutputBuffer(bufferInfo, indexOutput);

                        break;
                    }
                }

                if (waitTime > TimeSpan.Zero)
                {
                    // sleep required time to avoid active looping
                    // Note: do not sleep more than 'waitDefaultTime' to continue processing play commands
                    Utilities.Sleep(TimeSpanExtensions.Min(waitDefaultTime, waitTime));
                }
            }
        }
Ejemplo n.º 8
0
 public static void ApplySample(NormalizedStatementStatistics cumulativeData, NormalizedStatementStatistics newSample)
 {
     if (newSample.MaxDuration > cumulativeData.MaxDuration)
     {
         cumulativeData.RepresentativeStatement = newSample.RepresentativeStatement;
     }
     cumulativeData.MaxDuration = TimeSpanExtensions.Max(cumulativeData.MaxDuration, newSample.MaxDuration);
     cumulativeData.MinDuration = TimeSpanExtensions.Min(cumulativeData.MinDuration, newSample.MinDuration);
     if (cumulativeData.TotalExecutionsCount > 0 || newSample.TotalExecutionsCount > 0)
     {
         cumulativeData.AvgDuration = TimeSpan.FromMilliseconds(
             (
                 cumulativeData.TotalExecutionsCount * cumulativeData.AvgDuration.TotalMilliseconds
                 + newSample.TotalExecutionsCount * newSample.AvgDuration.TotalMilliseconds
             )
             / (cumulativeData.TotalExecutionsCount + newSample.TotalExecutionsCount)
             );
     }
     else
     {
         cumulativeData.AvgDuration = newSample.AvgDuration;
     }
     cumulativeData.TotalExecutionsCount += newSample.TotalExecutionsCount;
     cumulativeData.TotalDuration        += newSample.TotalDuration;
     if (cumulativeData.MaxTotalCost.HasValue)
     {
         if (newSample.MaxTotalCost.HasValue)
         {
             cumulativeData.MaxTotalCost = Math.Max(cumulativeData.MaxTotalCost.Value, newSample.MaxTotalCost.Value);
         }
     }
     else
     {
         cumulativeData.MaxTotalCost = newSample.MaxTotalCost;
     }
     if (cumulativeData.MinTotalCost.HasValue)
     {
         if (newSample.MinTotalCost.HasValue)
         {
             cumulativeData.MinTotalCost = Math.Max(cumulativeData.MinTotalCost.Value, newSample.MinTotalCost.Value);
         }
     }
     else
     {
         cumulativeData.MinTotalCost = newSample.MinTotalCost;
     }
     if (cumulativeData.AvgTotalCost.HasValue)
     {
         if (newSample.AvgTotalCost.HasValue && (cumulativeData.TotalExecutionsCount > 0 || newSample.TotalExecutionsCount > 0))
         {
             cumulativeData.AvgTotalCost = (cumulativeData.TotalExecutionsCount * cumulativeData.AvgTotalCost.Value
                                            + newSample.TotalExecutionsCount * newSample.AvgTotalCost.Value
                                            )
                                           / (cumulativeData.TotalExecutionsCount + newSample.TotalExecutionsCount);
         }
     }
     else
     {
         cumulativeData.AvgTotalCost = newSample.AvgTotalCost;
     }
 }