/
Connection.php
359 lines (304 loc) · 13 KB
/
Connection.php
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Messenger\Bridge\AmazonSqs\Transport;
use AsyncAws\Sqs\Enum\QueueAttributeName;
use AsyncAws\Sqs\Result\ReceiveMessageResult;
use AsyncAws\Sqs\SqsClient;
use AsyncAws\Sqs\ValueObject\MessageAttributeValue;
use Symfony\Component\Messenger\Exception\InvalidArgumentException;
use Symfony\Component\Messenger\Exception\TransportException;
use Symfony\Contracts\HttpClient\HttpClientInterface;
/**
* A SQS connection.
*
* @author Jérémy Derussé <jeremy@derusse.com>
*
* @internal
* @final
*/
class Connection
{
private const AWS_SQS_FIFO_SUFFIX = '.fifo';
private const MESSAGE_ATTRIBUTE_NAME = 'X-Symfony-Messenger';
private const DEFAULT_OPTIONS = [
'buffer_size' => 9,
'wait_time' => 20,
'poll_timeout' => 0.1,
'visibility_timeout' => null,
'auto_setup' => true,
'access_key' => null,
'secret_key' => null,
'endpoint' => 'https://sqs.eu-west-1.amazonaws.com',
'region' => 'eu-west-1',
'queue_name' => 'messages',
'account' => null,
];
private $configuration;
private $client;
/** @var ReceiveMessageResult */
private $currentResponse;
/** @var array[] */
private $buffer = [];
/** @var string|null */
private $queueUrl;
public function __construct(array $configuration, SqsClient $client = null)
{
$this->configuration = array_replace_recursive(self::DEFAULT_OPTIONS, $configuration);
$this->client = $client ?? new SqsClient([]);
}
public function __destruct()
{
$this->reset();
}
/**
* Creates a connection based on the DSN and options.
*
* Available options:
*
* * endpoint: absolute URL to the SQS service (Default: https://sqs.eu-west-1.amazonaws.com)
* * region: name of the AWS region (Default: eu-west-1)
* * queue_name: name of the queue (Default: messages)
* * account: identifier of the AWS account
* * access_key: AWS access key
* * secret_key: AWS secret key
* * buffer_size: number of messages to prefetch (Default: 9)
* * wait_time: long polling duration in seconds (Default: 20)
* * poll_timeout: amount of seconds the transport should wait for new message
* * visibility_timeout: amount of seconds the message won't be visible
* * auto_setup: Whether the queue should be created automatically during send / get (Default: true)
*/
public static function fromDsn(string $dsn, array $options = [], HttpClientInterface $client = null): self
{
if (false === $parsedUrl = parse_url($dsn)) {
throw new InvalidArgumentException(sprintf('The given Amazon SQS DSN "%s" is invalid.', $dsn));
}
$query = [];
if (isset($parsedUrl['query'])) {
parse_str($parsedUrl['query'], $query);
}
$configuration = [
'buffer_size' => $options['buffer_size'] ?? (int) ($query['buffer_size'] ?? self::DEFAULT_OPTIONS['buffer_size']),
'wait_time' => $options['wait_time'] ?? (int) ($query['wait_time'] ?? self::DEFAULT_OPTIONS['wait_time']),
'poll_timeout' => $options['poll_timeout'] ?? ($query['poll_timeout'] ?? self::DEFAULT_OPTIONS['poll_timeout']),
'visibility_timeout' => $options['visibility_timeout'] ?? ($query['visibility_timeout'] ?? self::DEFAULT_OPTIONS['visibility_timeout']),
'auto_setup' => $options['auto_setup'] ?? (bool) ($query['auto_setup'] ?? self::DEFAULT_OPTIONS['auto_setup']),
];
$clientConfiguration = [
'region' => $options['region'] ?? ($query['region'] ?? self::DEFAULT_OPTIONS['region']),
'accessKeyId' => $options['access_key'] ?? (urldecode($parsedUrl['user'] ?? '') ?: self::DEFAULT_OPTIONS['access_key']),
'accessKeySecret' => $options['secret_key'] ?? (urldecode($parsedUrl['pass'] ?? '') ?: self::DEFAULT_OPTIONS['secret_key']),
];
unset($query['region']);
if ('default' !== ($parsedUrl['host'] ?? 'default')) {
$clientConfiguration['endpoint'] = sprintf('%s://%s%s', ($query['sslmode'] ?? null) === 'disable' ? 'http' : 'https', $parsedUrl['host'], ($parsedUrl['port'] ?? null) ? ':'.$parsedUrl['port'] : '');
if (preg_match(';^sqs\.([^\.]++)\.amazonaws\.com$;', $parsedUrl['host'], $matches)) {
$clientConfiguration['region'] = $matches[1];
}
unset($query['sslmode']);
}
$parsedPath = explode('/', ltrim($parsedUrl['path'] ?? '/', '/'));
if (\count($parsedPath) > 0) {
$configuration['queue_name'] = end($parsedPath);
}
$configuration['account'] = 2 === \count($parsedPath) ? $parsedPath[0] : null;
// check for extra keys in options
$optionsExtraKeys = array_diff(array_keys($options), array_keys(self::DEFAULT_OPTIONS));
if (0 < \count($optionsExtraKeys)) {
throw new InvalidArgumentException(sprintf('Unknown option found : [%s]. Allowed options are [%s].', implode(', ', $optionsExtraKeys), implode(', ', array_keys(self::DEFAULT_OPTIONS))));
}
// check for extra keys in options
$queryExtraKeys = array_diff(array_keys($query), array_keys(self::DEFAULT_OPTIONS));
if (0 < \count($queryExtraKeys)) {
throw new InvalidArgumentException(sprintf('Unknown option found in DSN: [%s]. Allowed options are [%s].', implode(', ', $queryExtraKeys), implode(', ', array_keys(self::DEFAULT_OPTIONS))));
}
return new self($configuration, new SqsClient($clientConfiguration, null, $client));
}
public function get(): ?array
{
if ($this->configuration['auto_setup']) {
$this->setup();
}
foreach ($this->getNextMessages() as $message) {
return $message;
}
return null;
}
/**
* @return array[]
*/
private function getNextMessages(): \Generator
{
yield from $this->getPendingMessages();
yield from $this->getNewMessages();
}
/**
* @return array[]
*/
private function getPendingMessages(): \Generator
{
while (!empty($this->buffer)) {
yield array_shift($this->buffer);
}
}
/**
* @return array[]
*/
private function getNewMessages(): \Generator
{
if (null === $this->currentResponse) {
$this->currentResponse = $this->client->receiveMessage([
'QueueUrl' => $this->getQueueUrl(),
'VisibilityTimeout' => $this->configuration['visibility_timeout'],
'MaxNumberOfMessages' => $this->configuration['buffer_size'],
'MessageAttributeNames' => ['All'],
'WaitTimeSeconds' => $this->configuration['wait_time'],
]);
}
if (!$this->fetchMessage()) {
return;
}
yield from $this->getPendingMessages();
}
private function fetchMessage(): bool
{
if (!$this->currentResponse->resolve($this->configuration['poll_timeout'])) {
return false;
}
foreach ($this->currentResponse->getMessages() as $message) {
$headers = [];
$attributes = $message->getMessageAttributes();
if (isset($attributes[self::MESSAGE_ATTRIBUTE_NAME]) && 'String' === $attributes[self::MESSAGE_ATTRIBUTE_NAME]->getDataType()) {
$headers = \json_decode($attributes[self::MESSAGE_ATTRIBUTE_NAME]->getStringValue(), true);
unset($attributes[self::MESSAGE_ATTRIBUTE_NAME]);
}
foreach ($attributes as $name => $attribute) {
if ('String' !== $attribute->getDataType()) {
continue;
}
$headers[$name] = $attribute->getStringValue();
}
$this->buffer[] = [
'id' => $message->getReceiptHandle(),
'body' => $message->getBody(),
'headers' => $headers,
];
}
$this->currentResponse = null;
return true;
}
public function setup(): void
{
// Set to false to disable setup more than once
$this->configuration['auto_setup'] = false;
if ($this->client->queueExists([
'QueueName' => $this->configuration['queue_name'],
'QueueOwnerAWSAccountId' => $this->configuration['account'],
])->isSuccess()) {
return;
}
if (null !== $this->configuration['account']) {
throw new InvalidArgumentException(sprintf('The Amazon SQS queue "%s" does not exists (or you don\'t have permissions on it), and can\'t be created when an account is provided.', $this->configuration['queue_name']));
}
$parameters = ['QueueName' => $this->configuration['queue_name']];
if (self::isFifoQueue($this->configuration['queue_name'])) {
$parameters['FifoQueue'] = true;
}
$this->client->createQueue($parameters);
$exists = $this->client->queueExists(['QueueName' => $this->configuration['queue_name']]);
// Blocking call to wait for the queue to be created
$exists->wait();
if (!$exists->isSuccess()) {
throw new TransportException(sprintf('Failed to crate the Amazon SQS queue "%s".', $this->configuration['queue_name']));
}
$this->queueUrl = null;
}
public function delete(string $id): void
{
$this->client->deleteMessage([
'QueueUrl' => $this->getQueueUrl(),
'ReceiptHandle' => $id,
]);
}
public function getMessageCount(): int
{
$response = $this->client->getQueueAttributes([
'QueueUrl' => $this->getQueueUrl(),
'AttributeNames' => [QueueAttributeName::APPROXIMATE_NUMBER_OF_MESSAGES],
]);
$attributes = $response->getAttributes();
return (int) ($attributes[QueueAttributeName::APPROXIMATE_NUMBER_OF_MESSAGES] ?? 0);
}
public function send(string $body, array $headers, int $delay = 0, ?string $messageGroupId = null, ?string $messageDeduplicationId = null): void
{
if ($this->configuration['auto_setup']) {
$this->setup();
}
$parameters = [
'QueueUrl' => $this->getQueueUrl(),
'MessageBody' => $body,
'DelaySeconds' => $delay,
'MessageAttributes' => [],
];
$specialHeaders = [];
foreach ($headers as $name => $value) {
if ($name[0] === '.' || $name === self::MESSAGE_ATTRIBUTE_NAME || \strlen($name) > 256 || \substr($name, -1) === '.' || \substr($name, 0, \strlen('AWS.')) === 'AWS.' || \substr($name, 0, \strlen('Amazon.')) === 'Amazon.' || \preg_match('/([^a-zA-Z0-9_\.-]+|\.\.)/', $name)) {
$specialHeaders[$name] = $value;
continue;
}
$parameters['MessageAttributes'][$name] = new MessageAttributeValue([
'DataType' => 'String',
'StringValue' => $value,
]);
}
if (!empty($specialHeaders)) {
$parameters['MessageAttributes'][self::MESSAGE_ATTRIBUTE_NAME] = new MessageAttributeValue([
'DataType' => 'String',
'StringValue' => \json_encode($specialHeaders),
]);
}
dd($parameters);
if (self::isFifoQueue($this->configuration['queue_name'])) {
$parameters['MessageGroupId'] = null !== $messageGroupId ? $messageGroupId : __METHOD__;
$parameters['MessageDeduplicationId'] = null !== $messageDeduplicationId ? $messageDeduplicationId : sha1(json_encode(['body' => $body, 'headers' => $headers]));
}
$this->client->sendMessage($parameters);
}
public function reset(): void
{
if (null !== $this->currentResponse) {
// fetch current response in order to requeue in transit messages
if (!$this->fetchMessage()) {
$this->currentResponse->cancel();
$this->currentResponse = null;
}
}
foreach ($this->getPendingMessages() as $message) {
$this->client->changeMessageVisibility([
'QueueUrl' => $this->getQueueUrl(),
'ReceiptHandle' => $message['id'],
'VisibilityTimeout' => 0,
]);
}
}
private function getQueueUrl(): string
{
if (null !== $this->queueUrl) {
return $this->queueUrl;
}
return $this->queueUrl = $this->client->getQueueUrl([
'QueueName' => $this->configuration['queue_name'],
'QueueOwnerAWSAccountId' => $this->configuration['account'],
])->getQueueUrl();
}
private static function isFifoQueue(string $queueName): bool
{
return self::AWS_SQS_FIFO_SUFFIX === substr($queueName, -\strlen(self::AWS_SQS_FIFO_SUFFIX));
}
}