Is it a problem that BufferBlock is always on?

114 Views Asked by At

I need log provider for my .NET Core project (especially, I want to send logs to Graylog with HTTP) and I searched all log repos on GitHub to find a good idea.

At the end of the day I found a few solutions and wanted to use the one that has BufferBlock; But I have some questions.

Firstly, here is the code.

using System;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow;

namespace Gelf.Extensions.Logging
{
    public class GelfMessageProcessor
    {
        private readonly BufferBlock<GelfMessage> _messageBuffer;

        private Task _processorTask = Task.CompletedTask;

        public GelfMessageProcessor(IGelfClient gelfClient)
        {
            GelfClient = gelfClient;
            _messageBuffer = new BufferBlock<GelfMessage>(new DataflowBlockOptions
            {
                BoundedCapacity = 10000
            });
        }

        internal IGelfClient GelfClient { get; set; }

        public void Start()
        {
            _processorTask = Task.Run(StartAsync);
        }

        private async Task StartAsync()
        {
            while (!_messageBuffer.Completion.IsCompleted)
            {
                try
                {
                    var message = await _messageBuffer.ReceiveAsync();
                    await GelfClient.SendMessageAsync(message);
                }
                catch (InvalidOperationException)
                {
                    // The source completed without providing data to receive.
                }
                catch (Exception ex)
                {
                    Debug.Fail("Unhandled exception while sending GELF message.",
                        ex.ToString());
                }
            }
        }

        public void Stop()
        {
            _messageBuffer.Complete();
            _processorTask.Wait();
        }

        public void SendMessage(GelfMessage message)
        {
            if (!_messageBuffer.Post(message))
            {
                Debug.Fail("Failed to add GELF message to buffer.");
            }
        }
    }
}

If you check this code, you can see that we have a BufferBlock where we have sent messages. And this block receive our message and send in a while loop, this while loop never stop in this project because it’s never been triggered as IsCompleted.

My first question is;

  1. Its BufferBlock is looking always on, should I close this block?
  2. Is it normal to being always on? If it stays on through the process, how much memory does it consume?
  3. What would happen if no message came to BufferBlock for few hours?

I want to use this, but I’m confused for this two questions, I couldn't find any answer. I can't predict what kind of problems it will cause unless I find the answers.

Also, I am open to suggestions about how to send logs to Graylog with HTTP and without using BufferBlock, maybe we can find a different way. (Please don't suggest me NuGet packages, I have to do without packages.)

0

There are 0 best solutions below