8000 feature #38850 [Messenger] Do not call getQueueUrl when the url is kn… · symfony/symfony@57e39b4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 57e39b4

Browse files
committed
feature #38850 [Messenger] Do not call getQueueUrl when the url is known in AmazonSqs transport (jderusse)
This PR was merged into the 5.2-dev branch. Discussion ---------- [Messenger] Do not call getQueueUrl when the url is known in AmazonSqs transport | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Fix #38849 | License | MIT | Doc PR | TODO When user provides a DSN that looks like a queueUrl, we don't need to call the `getQueueUrl` method. This PR inject the known queueUrl and prevent performing a useless call to the API when sending a message Commits ------- f1f44d4 Do not call getQueueUrl when the url is known
2 parents d2bac32 + f1f44d4 commit 57e39b4

File tree

2 files changed

+68
-3
lines changed

2 files changed

+68
-3
lines changed

src/Symfony/Component/Messenger/Bridge/AmazonSqs/Tests/Transport/ConnectionTest.php

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function testFromDsnAsQueueUrl()
9292
{
9393
$httpClient = $this->getMockBuilder(HttpClientInterface::class)->getMock();
9494
$this->assertEquals(
95-
new Connection(['queue_name' => 'ab1-MyQueue-A2BCDEF3GHI4', 'account' => '123456789012'], new SqsClient(['region' => 'us-east-2', 'endpoint' => 'https://sqs.us-east-2.amazonaws.com', 'accessKeyId' => null, 'accessKeySecret' => null], null, $httpClient)),
95+
new Connection(['queue_name' => 'ab1-MyQueue-A2BCDEF3GHI4', 'account' => '123456789012'], new SqsClient(['region' => 'us-east-2', 'endpoint' => 'https://sqs.us-east-2.amazonaws.com', 'accessKeyId' => null, 'accessKeySecret' => null], null, $httpClient), 'https://sqs.us-east-2.amazonaws.com/123456789012/ab1-MyQueue-A2BCDEF3GHI4'),
9696
Connection::fromDsn('https://sqs.us-east-2.amazonaws.com/123456789012/ab1-MyQueue-A2BCDEF3GHI4', [], $httpClient)
9797
);
9898
}
@@ -259,4 +259,57 @@ public function testUnexpectedSqsError()
259259
$connection = new Connection(['queue_name' => 'queue', 'account' => 123, 'auto_setup' => false], $client);
260260
$connection->get();
261261
}
262+
263+
/**
264+
* @dataProvider provideQueueUrl
265+
*/
266+
public function testInjectQueueUrl(string $dsn, string $queueUrl)
267+
{
268+
$connection = Connection::fromDsn($dsn);
269+
270+
$r = new \ReflectionObject($connection);
271+
$queueProperty = $r->getProperty('queueUrl');
272+
$queueProperty->setAccessible(true);
273+
274+
$this->assertSame($queueUrl, $queueProperty->getValue($connection));
275+
}
276+
277+
public function provideQueueUrl()
278+
{
279+
yield ['https://sqs.us-east-2.amazonaws.com/123456/queue', 'https://sqs.us-east-2.amazonaws.com/123456/queue'];
280+
yield ['https://KEY:SECRET@sqs.us-east-2.amazonaws.com/123456/queue', 'https://sqs.us-east-2.amazonaws.com/123456/queue'];
281+
yield ['https://sqs.us-east-2.amazonaws.com/123456/queue?auto_setup=1', 'https://sqs.us-east-2.amazonaws.com/123456/queue'];
282+
}
283+
284+
/**
285+
* @dataProvider provideNotQueueUrl
286+
*/
287+
public function testNotInjectQueueUrl(string $dsn)
288+
{
289+
$connection = Connection::fromDsn($dsn);
290+
291+
$r = new \ReflectionObject($connection);
292+
$queueProperty = $r->getProperty('queueUrl');
293+
$queueProperty->setAccessible(true);
294+
295+
$this->assertNull($queueProperty->getValue($connection));
296+
}
297+
298+
public function provideNotQueueUrl()
299+
{
300+
yield ['https://sqs.us-east-2.amazonaws.com/queue'];
301+
yield ['https://us-east-2/123456/ab1-MyQueue-A2BCDEF3GHI4'];
302+
yield ['sqs://default/queue'];
303+
}
304+
305+
public function testGetQueueUrlNotCalled()
306+
{
307+
$client = $this->getMockBuilder(SqsClient::class)->getMock();
308+
$connection = new Connection(['queue_name' => 'ab1-MyQueue-A2BCDEF3GHI4', 'account' => '123456789012'], $client, 'https://sqs.us-east-2.amazonaws.com/123456789012/ab1-MyQueue-A2BCDEF3GHI4');
309+
310+
$client->expects($this->never())->method('getQueueUrl');
311+
$client->expects($this->once())->method('deleteMessage');
312+
313+
$connection->delete('id');
314+
}
262315
}

src/Symfony/Component/Messenger/Bridge/AmazonSqs/Transport/Connection.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,11 @@ class Connection
5757
/** @var string|null */
5858
private $queueUrl;
5959

60-
public function __construct(array $configuration, SqsClient $client = null)
60+
public function __construct(array $configuration, SqsClient $client = null, string $queueUrl = null)
6161
{
6262
$this->configuration = array_replace_recursive(self::DEFAULT_OPTIONS, $configuration);
6363
$this->client = $client ?? new SqsClient([]);
64+
$this->queueUrl = $queueUrl;
6465
}
6566

6667
public function __destruct()
@@ -140,7 +141,18 @@ public static function fromDsn(string $dsn, array $options = [], HttpClientInter
140141
}
141142
$configuration['account'] = 2 === \count($parsedPath) ? $parsedPath[0] : $options['account'] ?? self::DEFAULT_OPTIONS['account'];
142143

143-
return new self($configuration, new SqsClient($clientConfiguration, null, $client));
144+
// When the DNS looks like a QueueUrl, we can directly inject it in the connection
145+
// https://sqs.REGION.amazonaws.com/ACCOUNT/QUEUE
146+
$queueUrl = null;
147+
if (
148+
'https' === $parsedUrl['scheme']
149+
&& ($parsedUrl['host'] ?? 'default') === "sqs.{$clientConfiguration['region']}.amazonaws.com"
150+
&& ($parsedUrl['path'] ?? '/') === "/{$configuration['account']}/{$configuration['queue_name']}"
151+
) {
152+
$queueUrl = 'https://'.$parsedUrl['host'].$parsedUrl['path'];
153+
}
154+
155+
return new self($configuration, new SqsClient($clientConfiguration, null, $client), $queueUrl);
144156
}
145157

146158
public function get(): ?array

0 commit comments

Comments
 (0)
0