Skip to content

Commit

Permalink
Merge pull request #11 from event-band/feature/max-execution-time
Browse files Browse the repository at this point in the history
Change dispatch limit logic
  • Loading branch information
coyl authored Aug 28, 2017
2 parents af87fdc + b2393f1 commit b94fefb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/EventBand/Processor/Control/EventLimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
namespace EventBand\Processor\Control;

use EventBand\Processor\DispatchStopEvent;
use EventBand\Processor\StoppableDispatchEvent;

class EventLimiter
{
Expand All @@ -30,7 +30,7 @@ public function initCounter()
$this->events = 0;
}

public function checkLimit(DispatchStopEvent $event)
public function checkLimit(StoppableDispatchEvent $event)
{
$this->events++;

Expand Down
9 changes: 6 additions & 3 deletions src/EventBand/Processor/Control/TimeLimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
namespace EventBand\Processor\Control;

use EventBand\Processor\DispatchStopEvent;
use EventBand\Processor\StoppableDispatchEvent;

class TimeLimiter
{
Expand All @@ -30,10 +30,13 @@ public function initTimer()
$this->startTime = time();
}

public function checkLimit(DispatchStopEvent $event)
public function checkLimit(StoppableDispatchEvent $event)
{
if ($this->startTime + $this->limit >= time()) {
$now = time();
if ($this->startTime + $this->limit >= $now) {
$event->stopDispatching();
return 0;
}
return (int) ($this->startTime + $this->limit - $now);
}
}
13 changes: 9 additions & 4 deletions src/EventBand/Processor/DispatchProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ public function __construct(BandDispatcher $dispatcher, EventConsumer $consumer,
}
$this->band = $band;

if (($timeout = (int) $timeout) < 0) {
throw new \InvalidArgumentException(sprintf('Timeout %d < 0', $timeout));
}
$this->timeout = $timeout;
$this->setTimeout($timeout);
}

/**
Expand All @@ -69,6 +66,14 @@ public function process()
$this->dispatcher->dispatchEvent(new ProcessStopEvent());
}

public function setTimeout($timeout)
{
if (($timeout = (int)$timeout) < 0) {
throw new \InvalidArgumentException(sprintf('Timeout %d < 0', $timeout));
}
$this->timeout = $timeout;
}

private function getDispatchCallback(&$dispatching)
{
return function (Event $event) use (&$dispatching) {
Expand Down

0 comments on commit b94fefb

Please sign in to comment.