Skip to content

Commit 45b74c9

Browse files
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 #1213
1 parent 23b0c1c commit 45b74c9

6 files changed

+73
-2
lines changed

pkg/sns/SnsConnectionFactory.php

+6
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ private function establishConnection(): SnsClient
105105
}
106106
}
107107

108+
if (isset($this->config['http'])) {
109+
$config['http'] = $this->config['http'];
110+
}
111+
108112
$establishConnection = function () use ($config) {
109113
return (new Sdk(['Sns' => $config]))->createMultiRegionSns();
110114
};
@@ -134,6 +138,7 @@ private function parseDsn(string $dsn): array
134138
'lazy' => $dsn->getBool('lazy'),
135139
'endpoint' => $dsn->getString('endpoint'),
136140
'topic_arns' => $dsn->getArray('topic_arns', [])->toArray(),
141+
'http' => $dsn->getArray('http', [])->toArray(),
137142
]), function ($value) { return null !== $value; });
138143
}
139144

@@ -148,6 +153,7 @@ private function defaultConfig(): array
148153
'lazy' => true,
149154
'endpoint' => null,
150155
'topic_arns' => [],
156+
'http' => [],
151157
];
152158
}
153159
}

pkg/sns/Tests/SnsConnectionFactoryConfigTest.php

+26
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public static function provideConfigs()
6565
'lazy' => true,
6666
'endpoint' => null,
6767
'topic_arns' => [],
68+
'http' => [],
6869
],
6970
];
7071

@@ -79,6 +80,7 @@ public static function provideConfigs()
7980
'lazy' => true,
8081
'endpoint' => null,
8182
'topic_arns' => [],
83+
'http' => [],
8284
],
8385
];
8486

@@ -93,6 +95,7 @@ public static function provideConfigs()
9395
'lazy' => true,
9496
'endpoint' => null,
9597
'topic_arns' => [],
98+
'http' => [],
9699
],
97100
];
98101

@@ -107,6 +110,7 @@ public static function provideConfigs()
107110
'lazy' => false,
108111
'endpoint' => null,
109112
'topic_arns' => [],
113+
'http' => [],
110114
],
111115
];
112116

@@ -121,6 +125,7 @@ public static function provideConfigs()
121125
'lazy' => false,
122126
'endpoint' => null,
123127
'topic_arns' => [],
128+
'http' => [],
124129
],
125130
];
126131

@@ -135,6 +140,7 @@ public static function provideConfigs()
135140
'lazy' => false,
136141
'endpoint' => null,
137142
'topic_arns' => [],
143+
'http' => [],
138144
],
139145
];
140146

@@ -155,6 +161,7 @@ public static function provideConfigs()
155161
'lazy' => false,
156162
'endpoint' => 'http://localstack:1111',
157163
'topic_arns' => [],
164+
'http' => [],
158165
],
159166
];
160167

@@ -172,6 +179,25 @@ public static function provideConfigs()
172179
'topic1' => 'arn:aws:sns:us-east-1:123456789012:topic1',
173180
'topic2' => 'arn:aws:sns:us-west-2:123456789012:topic2',
174181
],
182+
'http' => [],
183+
],
184+
];
185+
186+
yield [
187+
['dsn' => 'sns:?http[timeout]=5&http[connect_timeout]=2'],
188+
[
189+
'key' => null,
190+
'secret' => null,
191+
'token' => null,
192+
'region' => null,
193+
'version' => '2010-03-31',
194+
'lazy' => true,
195+
'endpoint' => null,
196+
'topic_arns' => [],
197+
'http' => [
198+
'timeout' => '5',
199+
'connect_timeout' => '2',
200+
],
175201
],
176202
];
177203
}

pkg/sns/Tests/SnsConnectionFactoryTest.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ public function testCouldBeConstructedWithEmptyConfiguration()
3333
'region' => null,
3434
'version' => '2010-03-31',
3535
'endpoint' => null,
36-
'topic_arns' => [],
36+
'topic_arns' => [],
37+
'http' => [],
3738
], 'config', $factory);
3839
}
3940

@@ -49,7 +50,8 @@ public function testCouldBeConstructedWithCustomConfiguration()
4950
'region' => null,
5051
'version' => '2010-03-31',
5152
'endpoint' => null,
52-
'topic_arns' => [],
53+
'topic_arns' => [],
54+
'http' => [],
5355
], 'config', $factory);
5456
}
5557

pkg/sqs/SqsConnectionFactory.php

+6
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ private function establishConnection(): SqsClient
108108
}
109109
}
110110

111+
if (isset($this->config['http'])) {
112+
$config['http'] = $this->config['http'];
113+
}
114+
111115
$establishConnection = function () use ($config) {
112116
return (new Sdk(['Sqs' => $config]))->createMultiRegionSqs();
113117
};
@@ -139,6 +143,7 @@ private function parseDsn(string $dsn): array
139143
'endpoint' => $dsn->getString('endpoint'),
140144
'profile' => $dsn->getString('profile'),
141145
'queue_owner_aws_account_id' => $dsn->getString('queue_owner_aws_account_id'),
146+
'http' => $dsn->getArray('http', [])->toArray(),
142147
]), function ($value) { return null !== $value; });
143148
}
144149

@@ -155,6 +160,7 @@ private function defaultConfig(): array
155160
'endpoint' => null,
156161
'profile' => null,
157162
'queue_owner_aws_account_id' => null,
163+
'http' => [],
158164
];
159165
}
160166
}

pkg/sqs/Tests/SqsConnectionFactoryConfigTest.php

+29
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public static function provideConfigs()
6767
'endpoint' => null,
6868
'profile' => null,
6969
'queue_owner_aws_account_id' => null,
70+
'http' => [],
7071
],
7172
];
7273

@@ -83,6 +84,7 @@ public static function provideConfigs()
8384
'endpoint' => null,
8485
'profile' => null,
8586
'queue_owner_aws_account_id' => null,
87+
'http' => [],
8688
],
8789
];
8890

@@ -99,6 +101,7 @@ public static function provideConfigs()
99101
'endpoint' => null,
100102
'profile' => null,
101103
'queue_owner_aws_account_id' => null,
104+
'http' => [],
102105
],
103106
];
104107

@@ -115,6 +118,7 @@ public static function provideConfigs()
115118
'endpoint' => null,
116119
'profile' => null,
117120
'queue_owner_aws_account_id' => null,
121+
'http' => [],
118122
],
119123
];
120124

@@ -131,6 +135,7 @@ public static function provideConfigs()
131135
'endpoint' => null,
132136
'profile' => null,
133137
'queue_owner_aws_account_id' => null,
138+
'http' => [],
134139
],
135140
];
136141

@@ -147,6 +152,7 @@ public static function provideConfigs()
147152
'endpoint' => null,
148153
'profile' => 'staging',
149154
'queue_owner_aws_account_id' => null,
155+
'http' => [],
150156
],
151157
];
152158

@@ -163,6 +169,7 @@ public static function provideConfigs()
163169
'endpoint' => null,
164170
'profile' => null,
165171
'queue_owner_aws_account_id' => null,
172+
'http' => [],
166173
],
167174
];
168175

@@ -185,6 +192,7 @@ public static function provideConfigs()
185192
'endpoint' => 'http://localstack:1111',
186193
'profile' => null,
187194
'queue_owner_aws_account_id' => null,
195+
'http' => [],
188196
],
189197
];
190198

@@ -203,6 +211,27 @@ public static function provideConfigs()
203211
'endpoint' => null,
204212
'profile' => 'staging',
205213
'queue_owner_aws_account_id' => null,
214+
'http' => [],
215+
],
216+
];
217+
218+
yield [
219+
['dsn' => 'sqs:?http[timeout]=5&http[connect_timeout]=2'],
220+
[
221+
'key' => null,
222+
'secret' => null,
223+
'token' => null,
224+
'region' => null,
225+
'retries' => 3,
226+
'version' => '2012-11-05',
227+
'lazy' => true,
228+
'endpoint' => null,
229+
'profile' => null,
230+
'queue_owner_aws_account_id' => null,
231+
'http' => [
232+
'timeout' => '5',
233+
'connect_timeout' => '2',
234+
],
206235
],
207236
];
208237
}

pkg/sqs/Tests/SqsConnectionFactoryTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public function testCouldBeConstructedWithEmptyConfiguration()
3636
'endpoint' => null,
3737
'profile' => null,
3838
'queue_owner_aws_account_id' => null,
39+
'http' => [],
3940
], 'config', $factory);
4041
}
4142

@@ -54,6 +55,7 @@ public function testCouldBeConstructedWithCustomConfiguration()
5455
'endpoint' => null,
5556
'profile' => null,
5657
'queue_owner_aws_account_id' => null,
58+
'http' => [],
5759
], 'config', $factory);
5860
}
5961

0 commit comments

Comments
 (0)