Skip to content

Commit 1a18ee8

Browse files
committed
fix: break circular reference cycles
1 parent 573c569 commit 1a18ee8

2 files changed

Lines changed: 11 additions & 10 deletions

File tree

src/EndpointDiscovery/EndpointDiscoveryMiddleware.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class EndpointDiscoveryMiddleware
2121
private static $discoveryCooldown = 60;
2222

2323
private $args;
24-
private $client;
24+
private \WeakReference $client;
2525
private $config;
2626
private $discoveryTimes = [];
2727
private $nextHandler;
@@ -53,7 +53,7 @@ public function __construct(
5353
$config
5454
) {
5555
$this->nextHandler = $handler;
56-
$this->client = $client;
56+
$this->client = \WeakReference::create($client);
5757
$this->args = $args;
5858
$this->service = $client->getApi();
5959
$this->config = $config;
@@ -91,7 +91,7 @@ public function __invoke(CommandInterface $cmd, RequestInterface $request)
9191
$identifiers = $this->getIdentifiers($op);
9292

9393
$cacheKey = $this->getCacheKey(
94-
$this->client->getCredentials()->wait(),
94+
$this->client->get()->getCredentials()->wait(),
9595
$cmd,
9696
$identifiers
9797
);
@@ -178,7 +178,7 @@ private function discoverEndpoint(
178178
) {
179179
$discCmd = $this->getDiscoveryCommand($cmd, $identifiers);
180180
$this->discoveryTimes[$cacheKey] = time();
181-
$result = $this->client->execute($discCmd);
181+
$result = $this->client->get()->execute($discCmd);
182182

183183
if (isset($result['Endpoints'])) {
184184
$endpointData = [];
@@ -237,7 +237,7 @@ private function getDiscoveryCommand(
237237
$params['Identifiers'][$identifier] = $cmd[$identifier];
238238
}
239239
}
240-
$command = $this->client->getCommand($endpointOperation, $params);
240+
$command = $this->client->get()->getCommand($endpointOperation, $params);
241241
$command->getHandlerList()->appendBuild(
242242
Middleware::mapRequest(function (RequestInterface $r) {
243243
return $r->withHeader(

src/PresignUrlMiddleware.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*/
1212
class PresignUrlMiddleware
1313
{
14-
private $client;
14+
private \WeakReference $client;
1515
private $endpointProvider;
1616
private $nextHandler;
1717
/** @var array names of operations that require presign url */
@@ -32,7 +32,7 @@ public function __construct(
3232
callable $nextHandler
3333
) {
3434
$this->endpointProvider = $endpointProvider;
35-
$this->client = $client;
35+
$this->client = \WeakReference::create($client);
3636
$this->nextHandler = $nextHandler;
3737
$this->commandPool = $options['operations'];
3838
$this->serviceName = $options['service'];
@@ -61,15 +61,16 @@ public function __invoke(CommandInterface $cmd, ?RequestInterface $request = nul
6161
if (in_array($cmd->getName(), $this->commandPool)
6262
&& (!isset($cmd['__skip' . $cmd->getName()]))
6363
) {
64-
$cmd['DestinationRegion'] = $this->client->getRegion();
64+
$client = $this->client->get();
65+
$cmd['DestinationRegion'] = $client->getRegion();
6566
if (!empty($cmd['SourceRegion']) && !empty($cmd[$this->presignParam])) {
6667
goto nexthandler;
6768
}
6869
if (!$this->requireDifferentRegion
6970
|| (!empty($cmd['SourceRegion'])
7071
&& $cmd['SourceRegion'] !== $cmd['DestinationRegion'])
7172
) {
72-
$cmd[$this->presignParam] = $this->createPresignedUrl($this->client, $cmd);
73+
$cmd[$this->presignParam] = $this->createPresignedUrl($client, $cmd);
7374
}
7475
}
7576
nexthandler:
@@ -91,7 +92,7 @@ private function createPresignedUrl(
9192
// Create the new endpoint for the target endpoint.
9293
if ($this->endpointProvider instanceof \Aws\EndpointV2\EndpointProviderV2) {
9394
$providerArgs = array_merge(
94-
$this->client->getEndpointProviderArgs(),
95+
$this->client->get()->getEndpointProviderArgs(),
9596
['Region' => $cmd['SourceRegion']]
9697
);
9798
$endpoint = $this->endpointProvider->resolveEndpoint($providerArgs)->getUrl();

0 commit comments

Comments
 (0)