Overview of application:

  • A chat bot that connects via IRC, using TPL. As messages come from the socket (from .ReadAsync()), they are ultimately parsed and passed to a handler within the bot itself (Let's call this ChatBot).
  • An async long running task responsible for reading certain information from REST API's, and updating various information in various ChatBot classes.

My worry: When the long running thread is writing information, if the ChatBot is currently processing a message, there could be potentially multiple threads accessing the same information. For example: On the long running task, I can potentially read settings from ChatBot, which could also be changed from various commands entered from chat.

I'd like some direction in how to handle this from a TPL standpoint. This is a console app, so I'm trying to figure out how SynchronizationContexts may work, so that each bot runs in it's own context (Multiple threads can process various messages, but only 1 on each bot). Additionally, how to call said ChatBot functions from long running task in a thread-safe manner.

Additional Notes:

  • This bot is in a few thousand channels. I looked into a few things like SemaphoreSlim, but I'm curious how this may scale through the amount of channels, and overhead from locking every single message. (Do I manage a concurrentdictionary with a thousand objects)? I'm also worried about code creep/uglyness, where each long running task needs to acquire a lock for the current bot context.

How would you design this? I appreciate any and all direction.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.