Skip to content

Commit

Permalink
fix(server): Add throttling to socket_select() calls
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Sep 23, 2024
1 parent 013719b commit 6997e47
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/Socket/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,36 @@ final class Client implements Destroyable

private \Closure $onClose;

private Timer $selectTimer;

/**
* @param positive-int $payloadSize
*/
private function __construct(
private readonly \Socket $socket,
private readonly int $payloadSize,
float $selectPeriod,
) {
$this->selectTimer = new Timer($selectPeriod);

Check warning on line 40 in src/Socket/Client.php

View check run for this annotation

Codecov / codecov/patch

src/Socket/Client.php#L40

Added line #L40 was not covered by tests
\socket_set_nonblock($this->socket);
$this->setOnPayload(static fn(string $payload) => null);
$this->setOnClose(static fn() => null);
}

/**
* @param positive-int $payloadSize Max payload size.
* @param float $selectPeriod Time to wait between socket_select() calls in seconds.
*/
public static function init(
\Socket $socket,
int $payloadSize = 10485760,
float $selectPeriod = .001,
): self {
return new self($socket, $payloadSize);
return new self(
socket: $socket,
payloadSize: $payloadSize,
selectPeriod: $selectPeriod,

Check warning on line 58 in src/Socket/Client.php

View check run for this annotation

Codecov / codecov/patch

src/Socket/Client.php#L55-L58

Added lines #L55 - L58 were not covered by tests
);
}

public function destroy(): void
Expand Down Expand Up @@ -103,6 +113,7 @@ public function process(): void
throw new ClientDisconnected();
}
\Fiber::suspend();
$this->selectTimer->reset()->wait();

Check warning on line 116 in src/Socket/Client.php

View check run for this annotation

Codecov / codecov/patch

src/Socket/Client.php#L116

Added line #L116 was not covered by tests
} while (true);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Socket/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function process(): void
$client = null;
try {
/** @psalm-suppress MixedArgument */
$client = Client::init($socket, $this->payloadSize);
$client = Client::init($socket, $this->payloadSize, $this->acceptPeriod);

Check failure on line 100 in src/Socket/Server.php

View workflow job for this annotation

GitHub Actions / psalm (ubuntu-latest, 8.2, locked)

PossiblyUndefinedVariable

src/Socket/Server.php:100:40: PossiblyUndefinedVariable: Possibly undefined variable $socket, first seen on line 94 (see https://psalm.dev/018)

Check failure on line 100 in src/Socket/Server.php

View workflow job for this annotation

GitHub Actions / psalm (ubuntu-latest, 8.2, locked)

PossiblyUndefinedVariable

src/Socket/Server.php:100:40: PossiblyUndefinedVariable: Possibly undefined variable $socket, first seen on line 94 (see https://psalm.dev/018)

Check warning on line 100 in src/Socket/Server.php

View check run for this annotation

Codecov / codecov/patch

src/Socket/Server.php#L100

Added line #L100 was not covered by tests
$key = (int) \array_key_last($this->clients) + 1;
$this->clients[$key] = $client;
$this->clientInflector !== null and ($this->clientInflector)($client, $key);
Expand Down

0 comments on commit 6997e47

Please sign in to comment.