Skip to content

Commit 4c03857

Browse files
CopilotThavarshan
andcommitted
Fix code style issues: proper namespace imports and class element ordering
Co-authored-by: Thavarshan <10804999+Thavarshan@users.noreply.github.com>
1 parent d418abd commit 4c03857

3 files changed

Lines changed: 75 additions & 74 deletions

File tree

src/Fetch/Concerns/ManagesConnectionPool.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,20 @@ trait ManagesConnectionPool
3535
*/
3636
protected bool $poolingEnabled = false;
3737

38+
/**
39+
* Initialize connection pooling with default configuration.
40+
*
41+
* @param PoolConfiguration|null $config Optional pool configuration
42+
*/
43+
protected static function initializePool(?PoolConfiguration $config = null): void
44+
{
45+
if (self::$connectionPool === null) {
46+
$poolConfig = $config ?? new PoolConfiguration;
47+
self::$connectionPool = new ConnectionPool($poolConfig);
48+
self::$dnsCache = new DnsCache($poolConfig->getDnsCacheTtl());
49+
}
50+
}
51+
3852
/**
3953
* Configure connection pooling for this handler.
4054
*
@@ -213,20 +227,6 @@ public function resetPool(): ClientHandler
213227
return $this;
214228
}
215229

216-
/**
217-
* Initialize connection pooling with default configuration.
218-
*
219-
* @param PoolConfiguration|null $config Optional pool configuration
220-
*/
221-
protected static function initializePool(?PoolConfiguration $config = null): void
222-
{
223-
if (self::$connectionPool === null) {
224-
$poolConfig = $config ?? new PoolConfiguration;
225-
self::$connectionPool = new ConnectionPool($poolConfig);
226-
self::$dnsCache = new DnsCache($poolConfig->getDnsCacheTtl());
227-
}
228-
}
229-
230230
/**
231231
* Get cURL options for HTTP/2 support.
232232
*

src/Fetch/Pool/DnsCache.php

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Fetch\Pool;
66

77
use Fetch\Exceptions\NetworkException;
8+
use GuzzleHttp\Psr7\Request;
89

910
/**
1011
* DNS caching for improved performance.
@@ -124,6 +125,25 @@ public function setTtl(int $ttl): self
124125
return $this;
125126
}
126127

128+
/**
129+
* Prune expired entries from the cache.
130+
*
131+
* @return int Number of entries removed
132+
*/
133+
public function prune(): int
134+
{
135+
$removed = 0;
136+
137+
foreach ($this->cache as $key => $entry) {
138+
if ($this->isExpired($entry)) {
139+
unset($this->cache[$key]);
140+
$removed++;
141+
}
142+
}
143+
144+
return $removed;
145+
}
146+
127147
/**
128148
* Check if a cache entry is expired.
129149
*
@@ -179,29 +199,10 @@ protected function performDnsLookup(string $hostname): array
179199
if (empty($addresses)) {
180200
throw new NetworkException(
181201
"Failed to resolve hostname: {$hostname}",
182-
new \GuzzleHttp\Psr7\Request('GET', "https://{$hostname}/")
202+
new Request('GET', "https://{$hostname}/")
183203
);
184204
}
185205

186206
return $addresses;
187207
}
188-
189-
/**
190-
* Prune expired entries from the cache.
191-
*
192-
* @return int Number of entries removed
193-
*/
194-
public function prune(): int
195-
{
196-
$removed = 0;
197-
198-
foreach ($this->cache as $key => $entry) {
199-
if ($this->isExpired($entry)) {
200-
unset($this->cache[$key]);
201-
$removed++;
202-
}
203-
}
204-
205-
return $removed;
206-
}
207208
}

src/Fetch/Pool/HostConnectionPool.php

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,46 @@ public function returnConnection(Connection $connection): void
106106
}
107107
}
108108

109+
/**
110+
* Get the number of available connections.
111+
*/
112+
public function getAvailableCount(): int
113+
{
114+
return $this->availableConnections->count();
115+
}
116+
117+
/**
118+
* Get pool statistics.
119+
*
120+
* @return array<string, mixed>
121+
*/
122+
public function getStats(): array
123+
{
124+
return [
125+
'host' => $this->host,
126+
'port' => $this->port,
127+
'ssl' => $this->ssl,
128+
'available' => $this->availableConnections->count(),
129+
'total_created' => $this->totalCreated,
130+
'total_borrowed' => $this->totalBorrowed,
131+
'total_returned' => $this->totalReturned,
132+
'success_rate' => $this->totalBorrowed > 0
133+
? $this->totalReturned / $this->totalBorrowed
134+
: 1.0,
135+
];
136+
}
137+
138+
/**
139+
* Close all connections in the pool.
140+
*/
141+
public function closeAll(): void
142+
{
143+
while (! $this->availableConnections->isEmpty()) {
144+
$connection = $this->availableConnections->dequeue();
145+
$connection->close();
146+
}
147+
}
148+
109149
/**
110150
* Create a new connection.
111151
*
@@ -175,44 +215,4 @@ protected function warmupConnections(): void
175215
}
176216
}
177217
}
178-
179-
/**
180-
* Get the number of available connections.
181-
*/
182-
public function getAvailableCount(): int
183-
{
184-
return $this->availableConnections->count();
185-
}
186-
187-
/**
188-
* Get pool statistics.
189-
*
190-
* @return array<string, mixed>
191-
*/
192-
public function getStats(): array
193-
{
194-
return [
195-
'host' => $this->host,
196-
'port' => $this->port,
197-
'ssl' => $this->ssl,
198-
'available' => $this->availableConnections->count(),
199-
'total_created' => $this->totalCreated,
200-
'total_borrowed' => $this->totalBorrowed,
201-
'total_returned' => $this->totalReturned,
202-
'success_rate' => $this->totalBorrowed > 0
203-
? $this->totalReturned / $this->totalBorrowed
204-
: 1.0,
205-
];
206-
}
207-
208-
/**
209-
* Close all connections in the pool.
210-
*/
211-
public function closeAll(): void
212-
{
213-
while (! $this->availableConnections->isEmpty()) {
214-
$connection = $this->availableConnections->dequeue();
215-
$connection->close();
216-
}
217-
}
218218
}

0 commit comments

Comments
 (0)