Skip to content
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

Separate polling intervals for sockets #140

Merged
merged 3 commits into from
Sep 24, 2024
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
1 change: 1 addition & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
findUnusedBaselineEntry="false"
findUnusedCode="false"
errorBaseline="psalm-baseline.xml"
phpVersion="8.1"
>
<issueHandlers>
<RedundantConditionGivenDocblockType errorLevel="suppress"/>
Expand Down
1 change: 1 addition & 0 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@
return Server::init(
$config->port,
payloadSize: 524_288,
acceptPeriod: .001,

Check warning on line 259 in src/Application.php

View check run for this annotation

Codecov / codecov/patch

src/Application.php#L259

Added line #L259 was not covered by tests
clientInflector: $clientInflector,
logger: $this->logger,
);
Expand Down
23 changes: 12 additions & 11 deletions src/Sender/Console/Renderer/Sentry/Exceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,26 +87,27 @@
$class = empty($class) ? '' : $class . '::';
$function = $getValue($frame, 'function');

$renderer = static fn() => $output->writeln(
\sprintf(
"<fg=gray>%s</><fg=white;options=bold>%s<fg=yellow>%s</>\n%s<fg=yellow>%s</><fg=gray>%s()</>",
\str_pad("#$i", $numPad, ' '),
(string) $file,
$line !== '' ? ":$line" : '',
\str_repeat(' ', $numPad),
$class,
(string) $function,
),
$renderedLine = \sprintf(
"<fg=gray>%s</><fg=white;options=bold>%s<fg=yellow>%s</>\n%s<fg=yellow>%s</><fg=gray>%s()</>",
\str_pad("#$i", $numPad, ' '),
(string) $file,
$line !== '' ? ":$line" : '',
\str_repeat(' ', $numPad),
$class,
(string) $function,

Check warning on line 97 in src/Sender/Console/Renderer/Sentry/Exceptions.php

View check run for this annotation

Codecov / codecov/patch

src/Sender/Console/Renderer/Sentry/Exceptions.php#L90-L97

Added lines #L90 - L97 were not covered by tests
);

if ($isFirst) {
$isFirst = false;
$output->writeln('Stacktrace:');
$renderer();
$output->writeln($renderedLine);

Check warning on line 103 in src/Sender/Console/Renderer/Sentry/Exceptions.php

View check run for this annotation

Codecov / codecov/patch

src/Sender/Console/Renderer/Sentry/Exceptions.php#L103

Added line #L103 was not covered by tests
self::renderCodeSnippet($output, $frame, padding: $numPad);
continue;
}

$renderer = static function () use ($output, $renderedLine): void {
$output->writeln($renderedLine);
};

Check warning on line 110 in src/Sender/Console/Renderer/Sentry/Exceptions.php

View check run for this annotation

Codecov / codecov/patch

src/Sender/Console/Renderer/Sentry/Exceptions.php#L108-L110

Added lines #L108 - L110 were not covered by tests
if (!$verbose && \str_starts_with(\ltrim(\str_replace('\\', '/', $file), './'), 'vendor/')) {
$vendorLines[] = $renderer;
continue;
Expand Down
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 @@

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 @@
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
23 changes: 17 additions & 6 deletions src/Socket/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Buggregator\Trap\Processable;
use Buggregator\Trap\Socket\Exception\ClientDisconnected;
use Buggregator\Trap\Socket\Exception\ServerStopped;
use Socket;

/**
* @internal
Expand All @@ -27,13 +26,18 @@

private bool $cancelled = false;

/** Timestamp with microseconds when last socket_accept() was called */
private float $lastAccept = 0;

/**
* @param null|\Closure(Client, int $id): void $clientInflector
* @param positive-int $payloadSize Max payload size.
* @param float $acceptPeriod Time to wait between socket_accept() calls in seconds.
*/
private function __construct(
int $port,
private readonly int $payloadSize,
private readonly float $acceptPeriod,
private readonly ?\Closure $clientInflector,
private readonly Logger $logger,
) {
Expand All @@ -44,21 +48,23 @@

\socket_set_nonblock($this->socket);

$logger->status('Application', 'Server started on 127.0.0.1:%s', $port);
$logger->status('App', 'Server started on 127.0.0.1:%s', $port);

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

View check run for this annotation

Codecov / codecov/patch

src/Socket/Server.php#L51

Added line #L51 was not covered by tests
}

/**
* @param int<1, 65535> $port
* @param positive-int $payloadSize Max payload size.
* @param float $acceptPeriod Time to wait between socket_accept() calls in seconds.
* @param null|\Closure(Client, int $id): void $clientInflector
*/
public static function init(
int $port = 9912,
int $payloadSize = 10485760,
float $acceptPeriod = .001,
?\Closure $clientInflector = null,
Logger $logger = new Logger(),
): self {
return new self($port, $payloadSize, $clientInflector, $logger);
return new self($port, $payloadSize, $acceptPeriod, $clientInflector, $logger);

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

View check run for this annotation

Codecov / codecov/patch

src/Socket/Server.php#L67

Added line #L67 was not covered by tests
}

public function destroy(): void
Expand All @@ -82,11 +88,16 @@
public function process(): void
{
// /** @psalm-suppress PossiblyInvalidArgument */
while (!$this->cancelled and false !== ($socket = \socket_accept($this->socket))) {
while (match(true) {
$this->cancelled,
\microtime(true) - $this->lastAccept <= $this->acceptPeriod => false,
default => false !== ($socket = \socket_accept($this->socket)),
}) {
$this->lastAccept = \microtime(true);

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

View check run for this annotation

Codecov / codecov/patch

src/Socket/Server.php#L91-L96

Added lines #L91 - L96 were not covered by tests
$client = null;
try {
/** @psalm-suppress MixedArgument */
$client = Client::init($socket, $this->payloadSize);
/** @var \Socket $socket */
$client = Client::init($socket, $this->payloadSize, $this->acceptPeriod);

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
Loading