Example #1
0
        private int ReplayUnackedMessages(CancellationToken cancellationToken)
        {
            using (var reader = _storage.CreateMessageReader(_peer.Id))
            {
                if (reader == null)
                {
                    return(0);
                }
                var totalMessageCount = 0;

                foreach (var partition in reader.GetUnackedMessages().TakeWhile(m => !cancellationToken.IsCancellationRequested).Partition(_replayBatchSize, true))
                {
                    var messageSentCount    = 0;
                    var batchDuration       = MeasureDuration();
                    var readAndSendDuration = MeasureDuration();
                    foreach (var message in partition.Select(DeserializeTransportMessage))
                    {
                        _unackedIds.Add(message.Id);
                        ReplayMessage(message);
                        messageSentCount++;
                    }

                    totalMessageCount += messageSentCount;

                    _logger.Info($"Read and send for last batch of {messageSentCount} msgs for {_peer.Id} took {readAndSendDuration.Value}. ({messageSentCount / readAndSendDuration.Value.TotalSeconds} msg/s)");
                    WaitForAcks(cancellationToken);
                    _logger.Info($"Last batch for {_peer.Id} took {batchDuration.Value} to be totally replayed ({messageSentCount / batchDuration.Value.TotalSeconds} msg/s)");
                    _reporter.AddReplaySpeedReport(messageSentCount, readAndSendDuration.Value.TotalSeconds, batchDuration.Value.TotalSeconds);
                }

                _logger.Info($"Replay finished for peer {_peer.Id}. Disposing the reader");
                return(totalMessageCount);
            }
        }