From 45b74c92b10d3c399dfc55173306e9ae1545487c Mon Sep 17 00:00:00 2001 From: Ernesto Rodriguez Ortiz Date: Tue, 16 Nov 2021 12:15:20 -0500 Subject: [PATCH] feat(snsqs): allow client http configuration for sns and sqs This changes allow to set in the AWS Sdk client the http configurations: https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_configuration.html#config-http This will make possible to define custom `connect_timeout` and `timeout` to fail fast when the network is not relaible. Reported on https://github.com/php-enqueue/enqueue-dev/issues/1213 --- pkg/sns/SnsConnectionFactory.php | 6 ++++ .../Tests/SnsConnectionFactoryConfigTest.php | 26 +++++++++++++++++ pkg/sns/Tests/SnsConnectionFactoryTest.php | 6 ++-- pkg/sqs/SqsConnectionFactory.php | 6 ++++ .../Tests/SqsConnectionFactoryConfigTest.php | 29 +++++++++++++++++++ pkg/sqs/Tests/SqsConnectionFactoryTest.php | 2 ++ 6 files changed, 73 insertions(+), 2 deletions(-) diff --git a/pkg/sns/SnsConnectionFactory.php b/pkg/sns/SnsConnectionFactory.php index 67903ba22..8a815abad 100644 --- a/pkg/sns/SnsConnectionFactory.php +++ b/pkg/sns/SnsConnectionFactory.php @@ -105,6 +105,10 @@ private function establishConnection(): SnsClient } } + if (isset($this->config['http'])) { + $config['http'] = $this->config['http']; + } + $establishConnection = function () use ($config) { return (new Sdk(['Sns' => $config]))->createMultiRegionSns(); }; @@ -134,6 +138,7 @@ private function parseDsn(string $dsn): array 'lazy' => $dsn->getBool('lazy'), 'endpoint' => $dsn->getString('endpoint'), 'topic_arns' => $dsn->getArray('topic_arns', [])->toArray(), + 'http' => $dsn->getArray('http', [])->toArray(), ]), function ($value) { return null !== $value; }); } @@ -148,6 +153,7 @@ private function defaultConfig(): array 'lazy' => true, 'endpoint' => null, 'topic_arns' => [], + 'http' => [], ]; } } diff --git a/pkg/sns/Tests/SnsConnectionFactoryConfigTest.php b/pkg/sns/Tests/SnsConnectionFactoryConfigTest.php index 51983b126..de32d4265 100644 --- a/pkg/sns/Tests/SnsConnectionFactoryConfigTest.php +++ b/pkg/sns/Tests/SnsConnectionFactoryConfigTest.php @@ -65,6 +65,7 @@ public static function provideConfigs() 'lazy' => true, 'endpoint' => null, 'topic_arns' => [], + 'http' => [], ], ]; @@ -79,6 +80,7 @@ public static function provideConfigs() 'lazy' => true, 'endpoint' => null, 'topic_arns' => [], + 'http' => [], ], ]; @@ -93,6 +95,7 @@ public static function provideConfigs() 'lazy' => true, 'endpoint' => null, 'topic_arns' => [], + 'http' => [], ], ]; @@ -107,6 +110,7 @@ public static function provideConfigs() 'lazy' => false, 'endpoint' => null, 'topic_arns' => [], + 'http' => [], ], ]; @@ -121,6 +125,7 @@ public static function provideConfigs() 'lazy' => false, 'endpoint' => null, 'topic_arns' => [], + 'http' => [], ], ]; @@ -135,6 +140,7 @@ public static function provideConfigs() 'lazy' => false, 'endpoint' => null, 'topic_arns' => [], + 'http' => [], ], ]; @@ -155,6 +161,7 @@ public static function provideConfigs() 'lazy' => false, 'endpoint' => 'http://localstack:1111', 'topic_arns' => [], + 'http' => [], ], ]; @@ -172,6 +179,25 @@ public static function provideConfigs() 'topic1' => 'arn:aws:sns:us-east-1:123456789012:topic1', 'topic2' => 'arn:aws:sns:us-west-2:123456789012:topic2', ], + 'http' => [], + ], + ]; + + yield [ + ['dsn' => 'sns:?http[timeout]=5&http[connect_timeout]=2'], + [ + 'key' => null, + 'secret' => null, + 'token' => null, + 'region' => null, + 'version' => '2010-03-31', + 'lazy' => true, + 'endpoint' => null, + 'topic_arns' => [], + 'http' => [ + 'timeout' => '5', + 'connect_timeout' => '2', + ], ], ]; } diff --git a/pkg/sns/Tests/SnsConnectionFactoryTest.php b/pkg/sns/Tests/SnsConnectionFactoryTest.php index 5cc8f197b..4e9ad6ec9 100644 --- a/pkg/sns/Tests/SnsConnectionFactoryTest.php +++ b/pkg/sns/Tests/SnsConnectionFactoryTest.php @@ -33,7 +33,8 @@ public function testCouldBeConstructedWithEmptyConfiguration() 'region' => null, 'version' => '2010-03-31', 'endpoint' => null, - 'topic_arns' => [], + 'topic_arns' => [], + 'http' => [], ], 'config', $factory); } @@ -49,7 +50,8 @@ public function testCouldBeConstructedWithCustomConfiguration() 'region' => null, 'version' => '2010-03-31', 'endpoint' => null, - 'topic_arns' => [], + 'topic_arns' => [], + 'http' => [], ], 'config', $factory); } diff --git a/pkg/sqs/SqsConnectionFactory.php b/pkg/sqs/SqsConnectionFactory.php index a39cf806a..71e73b705 100644 --- a/pkg/sqs/SqsConnectionFactory.php +++ b/pkg/sqs/SqsConnectionFactory.php @@ -108,6 +108,10 @@ private function establishConnection(): SqsClient } } + if (isset($this->config['http'])) { + $config['http'] = $this->config['http']; + } + $establishConnection = function () use ($config) { return (new Sdk(['Sqs' => $config]))->createMultiRegionSqs(); }; @@ -139,6 +143,7 @@ private function parseDsn(string $dsn): array 'endpoint' => $dsn->getString('endpoint'), 'profile' => $dsn->getString('profile'), 'queue_owner_aws_account_id' => $dsn->getString('queue_owner_aws_account_id'), + 'http' => $dsn->getArray('http', [])->toArray(), ]), function ($value) { return null !== $value; }); } @@ -155,6 +160,7 @@ private function defaultConfig(): array 'endpoint' => null, 'profile' => null, 'queue_owner_aws_account_id' => null, + 'http' => [], ]; } } diff --git a/pkg/sqs/Tests/SqsConnectionFactoryConfigTest.php b/pkg/sqs/Tests/SqsConnectionFactoryConfigTest.php index 83c656c0b..6fa9298be 100644 --- a/pkg/sqs/Tests/SqsConnectionFactoryConfigTest.php +++ b/pkg/sqs/Tests/SqsConnectionFactoryConfigTest.php @@ -67,6 +67,7 @@ public static function provideConfigs() 'endpoint' => null, 'profile' => null, 'queue_owner_aws_account_id' => null, + 'http' => [], ], ]; @@ -83,6 +84,7 @@ public static function provideConfigs() 'endpoint' => null, 'profile' => null, 'queue_owner_aws_account_id' => null, + 'http' => [], ], ]; @@ -99,6 +101,7 @@ public static function provideConfigs() 'endpoint' => null, 'profile' => null, 'queue_owner_aws_account_id' => null, + 'http' => [], ], ]; @@ -115,6 +118,7 @@ public static function provideConfigs() 'endpoint' => null, 'profile' => null, 'queue_owner_aws_account_id' => null, + 'http' => [], ], ]; @@ -131,6 +135,7 @@ public static function provideConfigs() 'endpoint' => null, 'profile' => null, 'queue_owner_aws_account_id' => null, + 'http' => [], ], ]; @@ -147,6 +152,7 @@ public static function provideConfigs() 'endpoint' => null, 'profile' => 'staging', 'queue_owner_aws_account_id' => null, + 'http' => [], ], ]; @@ -163,6 +169,7 @@ public static function provideConfigs() 'endpoint' => null, 'profile' => null, 'queue_owner_aws_account_id' => null, + 'http' => [], ], ]; @@ -185,6 +192,7 @@ public static function provideConfigs() 'endpoint' => 'http://localstack:1111', 'profile' => null, 'queue_owner_aws_account_id' => null, + 'http' => [], ], ]; @@ -203,6 +211,27 @@ public static function provideConfigs() 'endpoint' => null, 'profile' => 'staging', 'queue_owner_aws_account_id' => null, + 'http' => [], + ], + ]; + + yield [ + ['dsn' => 'sqs:?http[timeout]=5&http[connect_timeout]=2'], + [ + 'key' => null, + 'secret' => null, + 'token' => null, + 'region' => null, + 'retries' => 3, + 'version' => '2012-11-05', + 'lazy' => true, + 'endpoint' => null, + 'profile' => null, + 'queue_owner_aws_account_id' => null, + 'http' => [ + 'timeout' => '5', + 'connect_timeout' => '2', + ], ], ]; } diff --git a/pkg/sqs/Tests/SqsConnectionFactoryTest.php b/pkg/sqs/Tests/SqsConnectionFactoryTest.php index 9eb072c77..c327522c5 100644 --- a/pkg/sqs/Tests/SqsConnectionFactoryTest.php +++ b/pkg/sqs/Tests/SqsConnectionFactoryTest.php @@ -36,6 +36,7 @@ public function testCouldBeConstructedWithEmptyConfiguration() 'endpoint' => null, 'profile' => null, 'queue_owner_aws_account_id' => null, + 'http' => [], ], 'config', $factory); } @@ -54,6 +55,7 @@ public function testCouldBeConstructedWithCustomConfiguration() 'endpoint' => null, 'profile' => null, 'queue_owner_aws_account_id' => null, + 'http' => [], ], 'config', $factory); }