I often have the need to process a queue of items where no one user should be able to block the queue and the items in the queue should be processed in some order. I frequently write a class to do this, but I thought there should be some generic version but I can't find one.
So I'm looking for a queue class where I can specify a type, a selector for partitions and a selector to order by such that I can add objects to the queue and then when I get objects back out, I get the first object from the next partition ordered by my order specifier.
For example, I would call like this specifying how to partition and how to sort the queue:
var queue = new RoundRobinQueue<Message>(
_ => _.UserID,
_ => _.SendDate
);
And after I've added lots of Message's, I can Parallel.ForEach
the items in my queue and process them in the order of the earliest SendDate
for the next User
. That way, if one user is slow, his items won't block the queue since he only gets one thread, but if there is only one user, he's the only partition so he gets all the threads.
I looked all over but couldn't find a nice generic implementation in C# for this. Any ideas?