private Task RunSenderThread()

in src/Amazon.CloudWatch.EMF/Sink/AgentSink.cs [92:127]


        private Task RunSenderThread(ILoggerFactory loggerFactory)
        {
            return Task.Run(
                async () =>
                {
                    var logger = loggerFactory.CreateLogger("Amazon.CloudWatch.EMF.Sink.AgentSink.RunSenderThread");
                    logger.LogDebug("Starting sender thread.");

                    while (!_cancellationTokenSource.IsCancellationRequested || _queue.Count > 0)
                    {
                        if (_cancellationTokenSource.IsCancellationRequested)
                        {
                            _logger.LogDebug($"Shutdown request received. {_queue.Count} messages pending.");
                        }

                        var message = _queue.Take();
                        logger.LogDebug("Sending message to socket");

                        // TODO: move into another method to avoid confusion of while loops
                        while (true)
                        {
                            try
                            {
                                await _socketClient.SendMessageAsync(message);
                                logger.LogDebug("Successfully wrote to socket.");
                                break;
                            }
                            catch (Exception e)
                            {
                                logger.LogWarning("Failed to write message to socket. Backing off and trying again. {}", e.Message);
                                Thread.Sleep(1000); // TODO: backoff
                            }
                        }
                    }
                });
        }