One channel and one janitor

Hi there, let's see if there's someone with more and better ideas than me.

I am trying to create a channel in rust, i don't care about the library for now.
Now i have an unbounded channel with Sender and Receiver called from different endpoints.
I would need 1 janitor that triggers every x seconds to discard old messages, because the endpoint to consume maybe is not called in x minutes/hours.

With a queue and a big mutex it's easy, block the entire queue each time the janitor runs and that would be it, but how can i do it without blocking?

I don't care about the code, someone has any ideas how can i do this?

Thanks for reading!

Here's some questions I could come up with that could further clarify what kind of thing you are looking for.

Is this single or multiple receivers? In case of multiple receivers, do the receivers share the workload or is each message broadcast to every receiver?

Am I correct in assuming that "discard old message" refers to messages whose age, as measured in how much time has passed since they were sent, is more than some particular fixed amount of minutes?

Is the amount of messages that could come up high enough to worry about effects of blocking? In naive, mutex based implementations, would you be equally worried about blocking senders and blocking receivers or is one thing more acceptable than the other? And would blocking either for short (bounded length) time frames at a time seem acceptable?

Thanks for answering!

For now i'm using the async_channel crate which gives multiple senders and multiple receivers and each receiver sees one message.

The project is a toy connection pool, the old idle connections needs to be pruned by the janitor ideally with the little blocking as possible, so a little bit seems acceptable, im not against using other data structures or other libraries though but the channel of idle connections seems so right for the problem, no need for synchronization using a mutex and a queue/stack.
I'm equally worried about blocking senders and receivers yeah, i would like to hear ideas about the problem.
Now the pool is working without the janitor thing, which for me is the hardest because of the concurrency.
Thanks!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.