Example #1
0
        protected virtual async Task ThrottleAsync(List <Envelope <T> > records, CancellationToken stopToken)
        {
            var recordCount = records.Count;
            var batchBytes  = records.Sum(r => GetRecordSize(r));
            var delay       = GetDelayMilliseconds(recordCount, batchBytes);

            if (delay > 0)
            {
                await Task.Delay((int)(delay * (1.0d + Utility.Random.NextDouble() * _jittingFactor)), stopToken);
            }

            //Implement the network check after the throttle in case that the network becomes unavailable after throttle delay
            if (_networkStatus.DefaultProvider is not null)
            {
                var waitCount = 0;
                while (!_networkStatus.CanUpload(_uploadNetworkPriority))
                {
                    if (waitCount % 30 == 0) //Reduce the log entries
                    {
                        _logger.LogWarning("Network not available. Will retry.");
                    }
                    waitCount++;
                    await Task.Delay(10000, stopToken); //Wait 10 seconds
                }
            }
        }
        protected virtual async Task ThrottledOnNextAsync(List <Envelope <TRecord> > records)
        {
            int  recordCount = records.Count;
            long batchBytes  = records.Select(r => GetRecordSize(r)).Sum();
            long delay       = GetDelayMilliseconds(recordCount, batchBytes);

            if (delay > 0)
            {
                await Task.Delay((int)(delay * (1.0d
                                                + Utility.Random.NextDouble() * _jittingFactor)));
            }

            //Implement the network check after the throttle in case that the network becomes unavailable after throttle delay
            if (NetworkStatus.CurrentNetwork != null)
            {
                int waitCount = 0;
                while (!NetworkStatus.CanUpload(_uploadNetworkPriority))
                {
                    if (waitCount % 30 == 0) //Reduce the log entries
                    {
                        _logger?.LogInformation("Network not available. Will retry.");
                    }
                    waitCount++;
                    await Task.Delay(10000); //Wait 10 seconds
                }
            }
            await OnNextAsync(records, batchBytes);
        }