Skip to content

Commit e0e5c27

Browse files
committed
(refactor): Simplify names across src — drop redundant qualifiers and expand abbreviations
1 parent 6953440 commit e0e5c27

File tree

9 files changed

+141
-141
lines changed

9 files changed

+141
-141
lines changed

src/Adapter.php

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@ class Adapter
2727
protected bool $skipValidation = false;
2828

2929
/** @var int Activity tracking interval in seconds */
30-
protected int $activityInterval = 30;
30+
protected int $interval = 30;
3131

3232
/** @var array<string, int> Last activity timestamp per resource */
33-
protected array $lastActivityUpdate = [];
33+
protected array $lastActivity = [];
3434

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

3838
/** @var \Closure|null Custom resolve callback, checked before the resolver */
39-
protected ?\Closure $resolveCallback = null;
39+
protected ?\Closure $callback = null;
4040

4141
public function __construct(
4242
public ?Resolver $resolver = null {
@@ -54,9 +54,9 @@ public function __construct(
5454
/**
5555
* Set activity tracking interval
5656
*/
57-
public function setActivityInterval(int $seconds): static
57+
public function setInterval(int $seconds): static
5858
{
59-
$this->activityInterval = $seconds;
59+
$this->interval = $seconds;
6060

6161
return $this;
6262
}
@@ -68,7 +68,7 @@ public function setActivityInterval(int $seconds): static
6868
*/
6969
public function onResolve(callable $callback): static
7070
{
71-
$this->resolveCallback = $callback(...);
71+
$this->callback = $callback(...);
7272

7373
return $this;
7474
}
@@ -101,14 +101,14 @@ public function notifyConnect(string $resourceId, array $metadata = []): void
101101
public function notifyClose(string $resourceId, array $metadata = []): void
102102
{
103103
// Flush remaining bytes on disconnect
104-
if (isset($this->byteCounters[$resourceId])) {
105-
$metadata['inboundBytes'] = $this->byteCounters[$resourceId]['inbound'];
106-
$metadata['outboundBytes'] = $this->byteCounters[$resourceId]['outbound'];
107-
unset($this->byteCounters[$resourceId]);
104+
if (isset($this->bytes[$resourceId])) {
105+
$metadata['inboundBytes'] = $this->bytes[$resourceId]['inbound'];
106+
$metadata['outboundBytes'] = $this->bytes[$resourceId]['outbound'];
107+
unset($this->bytes[$resourceId]);
108108
}
109109

110110
$this->resolver?->onDisconnect($resourceId, $metadata);
111-
unset($this->lastActivityUpdate[$resourceId]);
111+
unset($this->lastActivity[$resourceId]);
112112
}
113113

114114
/**
@@ -119,12 +119,12 @@ public function recordBytes(
119119
int $inbound = 0,
120120
int $outbound = 0,
121121
): void {
122-
if (!isset($this->byteCounters[$resourceId])) {
123-
$this->byteCounters[$resourceId] = ['inbound' => 0, 'outbound' => 0];
122+
if (!isset($this->bytes[$resourceId])) {
123+
$this->bytes[$resourceId] = ['inbound' => 0, 'outbound' => 0];
124124
}
125125

126-
$this->byteCounters[$resourceId]['inbound'] += $inbound;
127-
$this->byteCounters[$resourceId]['outbound'] += $outbound;
126+
$this->bytes[$resourceId]['inbound'] += $inbound;
127+
$this->bytes[$resourceId]['outbound'] += $outbound;
128128
}
129129

130130
/**
@@ -133,19 +133,19 @@ public function recordBytes(
133133
public function track(string $resourceId, array $metadata = []): void
134134
{
135135
$now = time();
136-
$lastUpdate = $this->lastActivityUpdate[$resourceId] ?? 0;
136+
$lastUpdate = $this->lastActivity[$resourceId] ?? 0;
137137

138-
if (($now - $lastUpdate) < $this->activityInterval) {
138+
if (($now - $lastUpdate) < $this->interval) {
139139
return;
140140
}
141141

142-
$this->lastActivityUpdate[$resourceId] = $now;
142+
$this->lastActivity[$resourceId] = $now;
143143

144144
// Flush accumulated byte counters into the activity metadata
145-
if (isset($this->byteCounters[$resourceId])) {
146-
$metadata['inboundBytes'] = $this->byteCounters[$resourceId]['inbound'];
147-
$metadata['outboundBytes'] = $this->byteCounters[$resourceId]['outbound'];
148-
$this->byteCounters[$resourceId] = ['inbound' => 0, 'outbound' => 0];
145+
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];
149149
}
150150

151151
$this->resolver?->track($resourceId, $metadata);
@@ -205,8 +205,8 @@ public function route(string $resourceId): ConnectionResult
205205
$this->stats['cacheMisses']++;
206206

207207
try {
208-
if ($this->resolveCallback !== null) {
209-
$resolved = ($this->resolveCallback)($resourceId);
208+
if ($this->callback !== null) {
209+
$resolved = ($this->callback)($resourceId);
210210
$result = $resolved instanceof Resolver\Result
211211
? $resolved
212212
: new Resolver\Result(endpoint: (string) $resolved);
@@ -228,7 +228,7 @@ public function route(string $resourceId): ConnectionResult
228228
}
229229

230230
if (! $this->skipValidation) {
231-
$this->validateEndpoint($endpoint);
231+
$this->validate($endpoint);
232232
}
233233

234234
$this->router->set($resourceId, [
@@ -252,7 +252,7 @@ public function route(string $resourceId): ConnectionResult
252252
/**
253253
* Validate backend endpoint to prevent SSRF attacks
254254
*/
255-
protected function validateEndpoint(string $endpoint): void
255+
protected function validate(string $endpoint): void
256256
{
257257
$parts = \explode(':', $endpoint);
258258
if (\count($parts) > 2) {

src/Adapter/TCP.php

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,24 @@
3939
class TCP extends Adapter
4040
{
4141
/** @var array<int, Client> */
42-
protected array $backendConnections = [];
42+
protected array $connections = [];
4343

4444
/** @var float Backend connection timeout in seconds */
45-
protected float $connectTimeout = 5.0;
45+
protected float $timeout = 5.0;
4646

4747
/** @var bool Whether read/write split routing is enabled */
4848
protected bool $readWriteSplit = false;
4949

5050
/** @var Parser|null Lazy-initialized query parser */
51-
protected ?Parser $queryParser = null;
51+
protected ?Parser $parser = null;
5252

5353
/**
5454
* Per-connection transaction pinning state.
5555
* When a connection is in a transaction, all queries are routed to primary.
5656
*
5757
* @var array<int, bool>
5858
*/
59-
protected array $pinnedConnections = [];
59+
protected array $pinned = [];
6060

6161
public function __construct(
6262
?Resolver $resolver = null,
@@ -72,9 +72,9 @@ public function __construct(
7272
/**
7373
* Set backend connection timeout
7474
*/
75-
public function setConnectTimeout(float $timeout): static
75+
public function setTimeout(float $timeout): static
7676
{
77-
$this->connectTimeout = $timeout;
77+
$this->timeout = $timeout;
7878

7979
return $this;
8080
}
@@ -105,9 +105,9 @@ public function isReadWriteSplit(): bool
105105
/**
106106
* Check if a connection is pinned to primary (in a transaction)
107107
*/
108-
public function isConnectionPinned(int $clientFd): bool
108+
public function isPinned(int $clientFd): bool
109109
{
110-
return $this->pinnedConnections[$clientFd] ?? false;
110+
return $this->pinned[$clientFd] ?? false;
111111
}
112112

113113
/**
@@ -149,29 +149,29 @@ public function getDescription(): string
149149
* @param int $clientFd Client file descriptor for transaction tracking
150150
* @return QueryType QueryType::Read or QueryType::Write
151151
*/
152-
public function classifyQuery(string $data, int $clientFd): QueryType
152+
public function classify(string $data, int $clientFd): QueryType
153153
{
154154
if (!$this->readWriteSplit) {
155155
return QueryType::Write;
156156
}
157157

158158
// If connection is pinned to primary (in transaction), everything goes to primary
159-
if ($this->isConnectionPinned($clientFd)) {
160-
$classification = $this->getQueryParser()->parse($data);
159+
if ($this->isPinned($clientFd)) {
160+
$classification = $this->getParser()->parse($data);
161161

162162
// Transaction end unpins
163163
if ($classification === QueryType::TransactionEnd) {
164-
unset($this->pinnedConnections[$clientFd]);
164+
unset($this->pinned[$clientFd]);
165165
}
166166

167167
return QueryType::Write;
168168
}
169169

170-
$classification = $this->getQueryParser()->parse($data);
170+
$classification = $this->getParser()->parse($data);
171171

172172
// Transaction begin pins to primary
173173
if ($classification === QueryType::TransactionBegin) {
174-
$this->pinnedConnections[$clientFd] = true;
174+
$this->pinned[$clientFd] = true;
175175

176176
return QueryType::Write;
177177
}
@@ -215,9 +215,9 @@ public function routeQuery(string $resourceId, QueryType $queryType): Connection
215215
*
216216
* Should be called when a client disconnects to clean up state.
217217
*/
218-
public function clearConnectionState(int $clientFd): void
218+
public function clearState(int $clientFd): void
219219
{
220-
unset($this->pinnedConnections[$clientFd]);
220+
unset($this->pinned[$clientFd]);
221221
}
222222

223223
/**
@@ -231,10 +231,10 @@ public function clearConnectionState(int $clientFd): void
231231
*
232232
* @throws \Exception
233233
*/
234-
public function getBackendConnection(string $initialData, int $clientFd): Client
234+
public function getConnection(string $initialData, int $clientFd): Client
235235
{
236-
if (isset($this->backendConnections[$clientFd])) {
237-
return $this->backendConnections[$clientFd];
236+
if (isset($this->connections[$clientFd])) {
237+
return $this->connections[$clientFd];
238238
}
239239

240240
$result = $this->route($initialData);
@@ -245,47 +245,47 @@ public function getBackendConnection(string $initialData, int $clientFd): Client
245245
$client = new Client(SWOOLE_SOCK_TCP);
246246

247247
$client->set([
248-
'timeout' => $this->connectTimeout,
249-
'connect_timeout' => $this->connectTimeout,
248+
'timeout' => $this->timeout,
249+
'connect_timeout' => $this->timeout,
250250
'open_tcp_nodelay' => true,
251251
'socket_buffer_size' => 2 * 1024 * 1024,
252252
]);
253253

254-
if (!$client->connect($host, $port, $this->connectTimeout)) {
254+
if (!$client->connect($host, $port, $this->timeout)) {
255255
throw new \Exception("Failed to connect to backend: {$host}:{$port}");
256256
}
257257

258-
$this->backendConnections[$clientFd] = $client;
258+
$this->connections[$clientFd] = $client;
259259

260260
return $client;
261261
}
262262

263263
/**
264264
* Close backend connection for a client
265265
*/
266-
public function closeBackendConnection(int $clientFd): void
266+
public function closeConnection(int $clientFd): void
267267
{
268-
if (isset($this->backendConnections[$clientFd])) {
269-
$this->backendConnections[$clientFd]->close();
270-
unset($this->backendConnections[$clientFd]);
268+
if (isset($this->connections[$clientFd])) {
269+
$this->connections[$clientFd]->close();
270+
unset($this->connections[$clientFd]);
271271
}
272272
}
273273

274274
/**
275275
* Get or create the query parser instance (lazy initialization)
276276
*/
277-
protected function getQueryParser(): Parser
277+
protected function getParser(): Parser
278278
{
279-
if ($this->queryParser === null) {
280-
$this->queryParser = match ($this->getProtocol()) {
279+
if ($this->parser === null) {
280+
$this->parser = match ($this->getProtocol()) {
281281
Protocol::PostgreSQL => new PostgreSQLParser(),
282282
Protocol::MySQL => new MySQLParser(),
283283
Protocol::MongoDB => new MongoDBParser(),
284284
default => throw new \Exception('No query parser for protocol: ' . $this->getProtocol()->value),
285285
};
286286
}
287287

288-
return $this->queryParser;
288+
return $this->parser;
289289
}
290290

291291
/**
@@ -310,7 +310,7 @@ protected function routeRead(string $resourceId): ConnectionResult
310310
}
311311

312312
if (!$this->skipValidation) {
313-
$this->validateEndpoint($endpoint);
313+
$this->validate($endpoint);
314314
}
315315

316316
return new ConnectionResult(
@@ -346,7 +346,7 @@ protected function routeWrite(string $resourceId): ConnectionResult
346346
}
347347

348348
if (!$this->skipValidation) {
349-
$this->validateEndpoint($endpoint);
349+
$this->validate($endpoint);
350350
}
351351

352352
return new ConnectionResult(

src/Server/HTTP/Swoole.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ class Swoole
3131
protected array $config;
3232

3333
/** @var array<string, Channel> */
34-
protected array $backendPools = [];
34+
protected array $pools = [];
3535

3636
/** @var array<string, Channel> */
37-
protected array $rawBackendPools = [];
37+
protected array $rawPools = [];
3838

3939
/**
4040
* @param array<string, mixed> $config
@@ -275,10 +275,10 @@ protected function forwardRequest(Request $request, Response $response, string $
275275
$port = (int) $port;
276276

277277
$poolKey = "{$host}:{$port}";
278-
if (! isset($this->backendPools[$poolKey])) {
279-
$this->backendPools[$poolKey] = new Channel($this->config['backend_pool_size']);
278+
if (! isset($this->pools[$poolKey])) {
279+
$this->pools[$poolKey] = new Channel($this->config['backend_pool_size']);
280280
}
281-
$pool = $this->backendPools[$poolKey];
281+
$pool = $this->pools[$poolKey];
282282

283283
$isNewClient = false;
284284
$client = $pool->pop($this->config['backend_pool_timeout']);
@@ -425,10 +425,10 @@ protected function forwardRawRequest(Request $request, Response $response, strin
425425
$port = (int) $port;
426426

427427
$poolKey = "{$host}:{$port}";
428-
if (! isset($this->rawBackendPools[$poolKey])) {
429-
$this->rawBackendPools[$poolKey] = new Channel($this->config['backend_pool_size']);
428+
if (! isset($this->rawPools[$poolKey])) {
429+
$this->rawPools[$poolKey] = new Channel($this->config['backend_pool_size']);
430430
}
431-
$pool = $this->rawBackendPools[$poolKey];
431+
$pool = $this->rawPools[$poolKey];
432432

433433
$client = $pool->pop($this->config['backend_pool_timeout']);
434434
if (! $client instanceof CoroutineClient || ! $client->isConnected()) {

0 commit comments

Comments
 (0)