cryptoeax / arbbot

Arbitrator, a bitcoin/altcoin arbitrage trading bot
https://gitter.im/cryptoeax-arbbot/Lobby
GNU General Public License v3.0
199 stars 78 forks source link

Duplicate addTimer code at arbitrator. Is it a bug? #70

Closed arvasko closed 6 years ago

arvasko commented 6 years ago

Sorry for maybe asking something silly, but I was reading the code and I found a weird situation. At Arbitrator->__construct(), there is a piece of code exactly equal to the code at innerRun(), doing exactly the same thing. Is that desired? Or is it a bug? The piece of code is this:

$this->eventLoop->addTimer( 1, function() use($self) { $self->innerRun(); } );

Calling this code once I understand. But calling a function that calls the same code got me confused.

cryptoeax commented 6 years ago

There are no silly questions. :-) (FWIW for conversations you may want to use gitter, it may be easier than issues.)

The idea here is that Arbitrator schedules its next run after it finishes one of its cycles. So after the object is constructed (where we schedule a timer to run the first cycle for the object) we want to set a timer to schedule the second cycle for the object, and when that timer has fired and the cycle has run, we want to set a timer to schedule the third cycle for the object, and so on. Here, each cycle is a call to the innerRun() method.

The EventLoop class also has an addPeriodicTimer() API which would eliminate the need to call addTimer() several times, but the reason we don't use that here is that sometimes a cycle run takes several seconds to complete, and we want these tasks to be scheduled 1 second apart no matter how long they take, so if we were to use addPeriodicTimer() and one call to innerRun() takes more than a second, several timer callbacks get queued up to be processed, which isn't desirable, so we have to do the scheduling manually as you see.

I hope this answers your question.