Do you mean to say its mysql table type or just keep it in memory?? And I assume the data is inserted every 5 seconds (everytime the daemon checks) and when will you clear the data for that card from this table?
The data for each channel will be cleared form the memory as soon as the channels is closed either by the daemon or by the callers or by asterisk. A cleaning will be done from time to time to make sure that if as channel is no longer in asterisk then it will be removed from our active channels list.
I assume you are checking the remaining balance using this formula. remaining balance - (sum of all inserted data for that account in memory table). Please explain.
You assumed correctly. In fact, here is what the update query looks like for the moment:
UPDATE `cc_cards`, (SELECT `account`, SUM(`rate`) global_rate FROM `billing_data` GROUP BY `account`) AS rates
SET credit = credit - global_rate
WHERE cardnumber = `account`
Improvement still have to be done in the way the date is being manipulated. Instead of querying the remaining balance and calculating the sum of the rates on each card withing SQL, I am convinced that there is a way to keep some of these information in a buffer (an array for instance). The buffer can be updated at the time a new call is added or removed. So the only time the DB will access the card balance will be when it will be time to update it. This part is still a theory.
In a situation say there are 100 cards used and 300 calls going, you have to check the balance for those 100 cards every 5 secs. Are there going to be any performance impact?
Yes. I think that it will affect performance. But I have not yet benchmarked it. The 5 seconds frequency has been decided because we wanted to see account credit changes happening at a fast pace. I believe that it would be reasonable to extend that to 60s. Even then, by doing the proper math, calculations can be done in respect to any kind of billing block (1 sec billing, 6 seconds, 60s, ...)
And if you lose the daemon or daemon died then the data in memory will get destroyed and you also think about how to handle this.
This may seem risky, but we will have to build it reliably and we will have to rely on the stability of Linux itself. In fact, asterisk runs as daemon and uses threads. And guess what? If they fail, all the calls get lost also. With that said, I am sure that I will have to face tons of bugs and situations that I didn't foresee. For the moment, I do not see how or the daemon should fail without a reasonable explanation.
By the way, I don't mind porting this over to C++ and compile it into a binary code to make it thread safer and a lot more efficient if that is what it take.
Again, suggestions, questions, critics, comments, ... ?