tags:

views:

21

answers:

3

Hi!

I have a system where clients can make orders. After making order they have 60 minutes to pay fot it before it will be deleted. On the server side when order is made i create timer and set elapsed time to 60 minutes

System.Timer.Timers timer = new System.Timers.Timer(1000*60*60);
timer.AutoReset = false;
timer.Elapsed += HandleElapsed;
timer.Start();

Because I want to be able to dispose timer if client decides to pay and I want to be able to cancel order if he doesn't I keep two Dictionaries:

Dictionary<int, Timer> _orderTimer;
Dictionary<Timer, int> _timerOrder;

Then when client pay's I can access Timer by orderId with O(1) thanks to _orderTimer dictionary and when time elapsed I can access order with O(1) thanks to _timerOrder dictionary.

My question is: Is it good aproach? Assuming that max number of rows I have to keep in dictionary in one moment will be 50000?

Maybe it would be better to derive from Timer class, add property called OrderId, keep it all in List and search for order/timer using linq?

Or maybe you I should do this in different way?

+2  A: 

50000 Timers may be more of a problem than the Dictionary.

I would add them to a List<> or Queue<> and use 1 Timer, set to your max tolerance (~ 30 seconds?) and every time the Timer elapses, batch remove all expired orders.

Henk Holterman
I get your point. I also thought that many timers could be a problem. Thanks for answer.
Adrian Serafin
A: 

Just as an add to Henks answer i would make a SortedList<Order> which is sorted by the expired date. So your single timer thread just has to enumerate through the list till a date/time is out of scope.

Oliver
If every customer has 60 minutes, a queue would work better. If certain customers had longer or shorter time periods in which to pay, then a `SortedList` would be best.
Daniel Rasmussen
yes, you're right. By a static timespan for everyone a queue fits better.
Oliver
A: 

I don't know if this is viable, but another approach would be to store the time it expires in your data store, then just write one timer that then picks up all the appropriate records and decides what to do with them.

Having 50,000+ timers sitting out there in memory just seems a little...scary.

CubanX