Skip to content

Commit b8e99eb

Browse files
committed
(refactor): Replace config and state arrays with typed objects
1 parent 293a533 commit b8e99eb

9 files changed

Lines changed: 363 additions & 524 deletions

File tree

src/Adapter.php

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class Adapter
3232
/** @var array<string, int> Last activity timestamp per resource */
3333
protected array $lastActivity = [];
3434

35-
/** @var array<string, array{inbound: int, outbound: int}> Byte counters per resource since last flush */
35+
/** @var array<string, Bytes> */
3636
protected array $bytes = [];
3737

3838
/** @var \Closure|null Custom resolve callback, checked before the resolver */
@@ -100,10 +100,9 @@ public function notifyConnect(string $resourceId, array $metadata = []): void
100100
*/
101101
public function notifyClose(string $resourceId, array $metadata = []): void
102102
{
103-
// Flush remaining bytes on disconnect
104103
if (isset($this->bytes[$resourceId])) {
105-
$metadata['inboundBytes'] = $this->bytes[$resourceId]['inbound'];
106-
$metadata['outboundBytes'] = $this->bytes[$resourceId]['outbound'];
104+
$metadata['inboundBytes'] = $this->bytes[$resourceId]->inbound;
105+
$metadata['outboundBytes'] = $this->bytes[$resourceId]->outbound;
107106
unset($this->bytes[$resourceId]);
108107
}
109108

@@ -120,11 +119,11 @@ public function recordBytes(
120119
int $outbound = 0,
121120
): void {
122121
if (!isset($this->bytes[$resourceId])) {
123-
$this->bytes[$resourceId] = ['inbound' => 0, 'outbound' => 0];
122+
$this->bytes[$resourceId] = new Bytes();
124123
}
125124

126-
$this->bytes[$resourceId]['inbound'] += $inbound;
127-
$this->bytes[$resourceId]['outbound'] += $outbound;
125+
$this->bytes[$resourceId]->inbound += $inbound;
126+
$this->bytes[$resourceId]->outbound += $outbound;
128127
}
129128

130129
/**
@@ -141,11 +140,10 @@ public function track(string $resourceId, array $metadata = []): void
141140

142141
$this->lastActivity[$resourceId] = $now;
143142

144-
// Flush accumulated byte counters into the activity metadata
145143
if (isset($this->bytes[$resourceId])) {
146-
$metadata['inboundBytes'] = $this->bytes[$resourceId]['inbound'];
147-
$metadata['outboundBytes'] = $this->bytes[$resourceId]['outbound'];
148-
$this->bytes[$resourceId] = ['inbound' => 0, 'outbound' => 0];
144+
$metadata['inboundBytes'] = $this->bytes[$resourceId]->inbound;
145+
$metadata['outboundBytes'] = $this->bytes[$resourceId]->outbound;
146+
$this->bytes[$resourceId] = new Bytes();
149147
}
150148

151149
$this->resolver?->track($resourceId, $metadata);
@@ -234,7 +232,7 @@ public function route(string $resourceId): ConnectionResult
234232
);
235233
}
236234

237-
if (! $this->skipValidation) {
235+
if (!$this->skipValidation) {
238236
$this->validate($endpoint);
239237
}
240238

@@ -274,7 +272,7 @@ protected function validate(string $endpoint): void
274272
}
275273

276274
$ip = \gethostbyname($host);
277-
if ($ip === $host && ! \filter_var($ip, FILTER_VALIDATE_IP)) {
275+
if ($ip === $host && !\filter_var($ip, FILTER_VALIDATE_IP)) {
278276
throw new ResolverException("Cannot resolve hostname: {$host}");
279277
}
280278

src/Bytes.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Utopia\Proxy;
4+
5+
class Bytes
6+
{
7+
public int $inbound = 0;
8+
9+
public int $outbound = 0;
10+
}

src/Server/HTTP/Config.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Utopia\Proxy\Server\HTTP;
4+
5+
class Config
6+
{
7+
public readonly int $reactorNum;
8+
9+
public function __construct(
10+
public readonly string $host = '0.0.0.0',
11+
public readonly int $port = 80,
12+
public readonly int $workers = 16,
13+
public readonly int $maxConnections = 100_000,
14+
public readonly int $maxCoroutine = 100_000,
15+
public readonly int $socketBufferSize = 2 * 1024 * 1024,
16+
public readonly int $bufferOutputSize = 2 * 1024 * 1024,
17+
public readonly bool $enableCoroutine = true,
18+
public readonly int $maxWaitTime = 60,
19+
public readonly int $serverMode = SWOOLE_PROCESS,
20+
?int $reactorNum = null,
21+
public readonly int $dispatchMode = 2,
22+
public readonly bool $enableReusePort = true,
23+
public readonly int $backlog = 65535,
24+
public readonly bool $parsePost = false,
25+
public readonly bool $parseCookie = false,
26+
public readonly bool $parseFiles = false,
27+
public readonly bool $compression = false,
28+
public readonly int $logLevel = SWOOLE_LOG_ERROR,
29+
public readonly int $timeout = 30,
30+
public readonly bool $keepAlive = true,
31+
public readonly int $poolSize = 1024,
32+
public readonly float $poolTimeout = 0.001,
33+
public readonly bool $telemetry = true,
34+
public readonly bool $fastPath = false,
35+
public readonly bool $fastPathAssumeOk = false,
36+
public readonly ?string $fixedBackend = null,
37+
public readonly ?string $directResponse = null,
38+
public readonly int $directResponseStatus = 200,
39+
public readonly int $keepaliveTimeout = 60,
40+
public readonly bool $httpProtocol = true,
41+
public readonly bool $http2Protocol = false,
42+
public readonly int $maxRequest = 0,
43+
public readonly bool $rawBackend = false,
44+
public readonly bool $rawBackendAssumeOk = false,
45+
public readonly bool $skipValidation = false,
46+
public readonly ?\Closure $requestHandler = null,
47+
public readonly ?\Closure $workerStart = null,
48+
) {
49+
$this->reactorNum = $reactorNum ?? swoole_cpu_num() * 2;
50+
}
51+
}

0 commit comments

Comments
 (0)