-
-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathManagesConnectionPool.php
More file actions
271 lines (234 loc) · 7.24 KB
/
ManagesConnectionPool.php
File metadata and controls
271 lines (234 loc) · 7.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<?php
declare(strict_types=1);
namespace Fetch\Concerns;
use Fetch\Interfaces\ClientHandler;
use Fetch\Pool\ConnectionPool;
use Fetch\Pool\DnsCache;
use Fetch\Pool\Http2Configuration;
use Fetch\Pool\PoolConfiguration;
/**
* Trait for managing connection pooling and HTTP/2 support.
*
* Note: The connection pool and DNS cache are stored as static properties
* to enable connection sharing across all ClientHandler instances. This is
* intentional to maximize connection reuse. However, be aware that:
* - Configuration changes affect all handlers globally
* - In multi-threaded environments, proper synchronization may be needed
* - Use resetPool() to isolate test environments
*/
trait ManagesConnectionPool
{
/**
* The connection pool instance (shared across all handlers).
*/
protected static ?ConnectionPool $connectionPool = null;
/**
* The DNS cache instance (shared across all handlers).
*/
protected static ?DnsCache $dnsCache = null;
/**
* The HTTP/2 configuration.
*/
protected ?Http2Configuration $http2Config = null;
/**
* Whether connection pooling is enabled for this handler.
*/
protected bool $poolingEnabled = false;
/**
* Initialize connection pooling with default configuration.
*
* @param PoolConfiguration|null $config Optional pool configuration
*/
protected static function initializePool(?PoolConfiguration $config = null): void
{
if (self::$connectionPool === null) {
$poolConfig = $config ?? new PoolConfiguration;
self::$connectionPool = new ConnectionPool($poolConfig);
self::$dnsCache = new DnsCache($poolConfig->getDnsCacheTtl());
}
}
/**
* Configure connection pooling for this handler.
*
* @param array<string, mixed>|bool $config Pool configuration or boolean to enable/disable
* @return $this
*/
public function withConnectionPool(array|bool $config = true): ClientHandler
{
if (is_bool($config)) {
$this->poolingEnabled = $config;
return $this;
}
$this->poolingEnabled = true;
// Initialize or update the global pool
self::$connectionPool = ConnectionPool::fromArray($config);
// Always initialize DNS cache with configuration TTL (uses default if not specified)
$poolConfig = self::$connectionPool->getConfig();
self::$dnsCache = new DnsCache($poolConfig->getDnsCacheTtl());
return $this;
}
/**
* Configure HTTP/2 for this handler.
*
* @param array<string, mixed>|bool $config HTTP/2 configuration or boolean to enable/disable
* @return $this
*/
public function withHttp2(array|bool $config = true): ClientHandler
{
if (is_bool($config)) {
$this->http2Config = new Http2Configuration(enabled: $config);
} else {
$this->http2Config = Http2Configuration::fromArray($config);
}
// Apply HTTP/2 curl options to the handler options
if ($this->http2Config->isEnabled()) {
$curlOptions = $this->http2Config->getCurlOptions();
if (! empty($curlOptions)) {
$existingCurl = $this->options['curl'] ?? [];
// Use + operator to preserve integer keys (CURL constants)
// and give priority to existing options over defaults
$this->options['curl'] = $existingCurl + $curlOptions;
}
// Set HTTP version in options
$this->options['version'] = 2.0;
}
return $this;
}
/**
* Get the connection pool instance.
*
* @return ConnectionPool|null The pool instance or null if not configured
*/
public function getConnectionPool(): ?ConnectionPool
{
return self::$connectionPool;
}
/**
* Get the DNS cache instance.
*
* @return DnsCache|null The DNS cache or null if not configured
*/
public function getDnsCache(): ?DnsCache
{
return self::$dnsCache;
}
/**
* Get the HTTP/2 configuration.
*
* @return Http2Configuration|null The HTTP/2 config or null if not configured
*/
public function getHttp2Config(): ?Http2Configuration
{
return $this->http2Config;
}
/**
* Check if connection pooling is enabled.
*/
public function isPoolingEnabled(): bool
{
return $this->poolingEnabled && self::$connectionPool !== null && self::$connectionPool->isEnabled();
}
/**
* Check if HTTP/2 is enabled.
*/
public function isHttp2Enabled(): bool
{
return $this->http2Config !== null && $this->http2Config->isEnabled();
}
/**
* Get connection pool statistics.
*
* @return array<string, mixed>
*/
public function getPoolStats(): array
{
if (self::$connectionPool === null) {
return ['enabled' => false];
}
return self::$connectionPool->getStats();
}
/**
* Get DNS cache statistics.
*
* @return array<string, mixed>
*/
public function getDnsCacheStats(): array
{
if (self::$dnsCache === null) {
return ['enabled' => false];
}
return array_merge(['enabled' => true], self::$dnsCache->getStats());
}
/**
* Clear the DNS cache.
*
* @param string|null $hostname Specific hostname to clear, or null for all
* @return $this
*/
public function clearDnsCache(?string $hostname = null): ClientHandler
{
if (self::$dnsCache !== null) {
self::$dnsCache->clear($hostname);
}
return $this;
}
/**
* Close all pooled connections.
*
* @return $this
*/
public function closeAllConnections(): ClientHandler
{
if (self::$connectionPool !== null) {
self::$connectionPool->closeAll();
}
return $this;
}
/**
* Reset the global connection pool and DNS cache.
*
* @return $this
*/
public function resetPool(): ClientHandler
{
if (self::$connectionPool !== null) {
self::$connectionPool->closeAll();
}
self::$connectionPool = null;
self::$dnsCache = null;
$this->poolingEnabled = false;
return $this;
}
/**
* Get cURL options for HTTP/2 support.
*
* @return array<int, mixed>
*/
protected function getHttp2CurlOptions(): array
{
if ($this->http2Config === null) {
return [];
}
return $this->http2Config->getCurlOptions();
}
/**
* Resolve a hostname using the DNS cache.
*
* Returns null if DNS cache is not configured or if DNS resolution fails.
* This method silently catches exceptions to allow fallback behavior.
*
* @param string $hostname The hostname to resolve
* @return string|null The resolved IP address, or null if not available
*/
protected function resolveHostname(string $hostname): ?string
{
if (self::$dnsCache === null) {
return null;
}
try {
return self::$dnsCache->resolveFirst($hostname);
} catch (\Throwable) {
return null;
}
}
}