Capital icon indicating copy to clipboard operation
Capital copied to clipboard

Large volume of transactions causes memory depletion

Open Nerahikada opened this issue 3 years ago • 3 comments

If the database server is slow and takes a long time to process transactions, attempting to process a large number of transactions will ~~create a large number of connections and~~ cause the server to crash due to lack of memory.

Using slow database, you can reproduce it with code like this

use SOFe\Capital\{Capital, CapitalException, LabelSet};

Capital::api("0.1.0", function(Capital $api){
    $this->selector = $api->completeConfig(["currency" => "money"]);
});

$this->getScheduler()->scheduleRepeatingTask(new ClosureTask(function(){
    foreach($this->getServer()->getOnlinePlayers() as $player){
        Capital::api("0.1.0", function(Capital $api) use ($player){
            yield from $api->addMoney($this->getName(), $player, $this->selector, 1, new LabelSet([]));
        });
    }
}), 1);

Nerahikada avatar Oct 23 '22 01:10 Nerahikada

It doesn't create a large number of connections. It just queues a huge backlog of queries to execute.

This is not exactly a bug, because each addMoney call is supposed to be atomic and has its own failure status, so it would be incorrect if Capital tries to buffer these calls because that would result in confusing failure behavior. If you would like to add money per tick, perhaps you should implement your own buffer.

This is comparable to the real-life scenario where you wouldn't pay an employee their salary every hour even though their wage is measured hourly.

I consider this issue as a wontfix for now, but any suggestions to improve this would be welcome.

SOF3 avatar Oct 23 '22 02:10 SOF3

I don't think there are actually many situations to add money per tick, but the more currency and number of players, the more likely this is to occur.

Nerahikada avatar Oct 23 '22 03:10 Nerahikada

In that case you should increase the number of workers in the libasynql settings.

SOF3 avatar Oct 23 '22 08:10 SOF3