Skip to content

Scale depending on demand #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion etc/qa/phpcs.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<ruleset>
<arg name="basepath" value="." />
<arg name="basepath" value="../../" />
<arg name="extensions" value="php" /> <!-- which extensions to look for -->
<arg name="colors" />
<arg name="cache" value=".phpcs.cache" /> <!-- cache the results and don't commit them -->
Expand Down
80 changes: 61 additions & 19 deletions src/EventLoopBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@

final class EventLoopBridge
{
private const DEFAULT_SCALE_RANGE = [
0.1,
0.01,
0.001,
];

private const DEFAULT_SCALE_POSITION = 2;

private LoopInterface $loop;

private ?Metrics $metrics = null;
Expand All @@ -41,6 +49,11 @@ final class EventLoopBridge

private bool $timerActive = FALSE_;

/** @var array<float> */
private array $scaleRange = self::DEFAULT_SCALE_RANGE;
private int $scalePosition = self::DEFAULT_SCALE_POSITION;
private int $scaleNoItemsCount = 0;

public function __construct(LoopInterface $loop)
{
$this->loop = $loop;
Expand Down Expand Up @@ -96,8 +109,30 @@ private function startTimer(): void
$this->metrics->timer()->counter(new Label('event', 'start'))->incr();
}

// Call 1K times per second
$this->timer = $this->loop->addPeriodicTimer(0.001, function (): void {
$this->runTimer();

$this->timerActive = TRUE_;
}

private function stopTimer(): void
{
if (count($this->channels) !== ZERO || count($this->futures) !== ZERO) {
return;
}

$this->loop->cancelTimer($this->timer);
$this->timerActive = FALSE_;

if (! ($this->metrics instanceof Metrics)) {
return;
}

$this->metrics->timer()->counter(new Label('event', 'stop'))->incr();
}

private function runTimer(): void
{
$this->timer = $this->loop->addPeriodicTimer($this->scaleRange[$this->scalePosition], function (): void {
$items = 0;

try {
Expand Down Expand Up @@ -131,30 +166,37 @@ private function startTimer(): void

$this->stopTimer();

/** @phpstan-ignore-next-line */
if ($items > 0 && isset($this->scaleRange[$this->scalePosition + 1])) {
$this->loop->cancelTimer($this->timer);

$this->scalePosition++;
$this->runTimer();

$this->scaleNoItemsCount = 0;
}

if ($items === 0) {
$this->scaleNoItemsCount++;

/** @phpstan-ignore-next-line */
if ($this->scaleNoItemsCount > 10 && isset($this->scaleRange[$this->scalePosition - 1])) {
$this->loop->cancelTimer($this->timer);

$this->scalePosition--;
$this->runTimer();

$this->scaleNoItemsCount = 0;
}
}

if (! ($this->metrics instanceof Metrics)) {
return;
}

$this->metrics->timer()->counter(new Label('event', 'tick'))->incr();
$this->metrics->timerItems()->counter(new Label('count', (string) $items))->incr();
});
$this->timerActive = TRUE_;
}

private function stopTimer(): void
{
if (count($this->channels) !== ZERO || count($this->futures) !== ZERO) {
return;
}

$this->loop->cancelTimer($this->timer);
$this->timerActive = FALSE_;

if (! ($this->metrics instanceof Metrics)) {
return;
}

$this->metrics->timer()->counter(new Label('event', 'stop'))->incr();
}

private function handleReadEvent(Events\Event $event): void
Expand Down