Skip to content

Commit 6a80a7c

Browse files
committed
[BC break][amqp] Introduce connection config. Make it same accross all transports.
1 parent b1cb420 commit 6a80a7c

11 files changed

Lines changed: 913 additions & 1336 deletions

‎pkg/amqp-bunny/AmqpConnectionFactory.php‎

Lines changed: 50 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Enqueue\AmqpBunny;
44

55
use Bunny\Client;
6+
use Enqueue\AmqpTools\ConnectionConfig;
67
use Enqueue\AmqpTools\DelayStrategyAware;
78
use Enqueue\AmqpTools\DelayStrategyAwareTrait;
89
use Interop\Amqp\AmqpConnectionFactory as InteropAmqpConnectionFactory;
@@ -12,7 +13,7 @@ class AmqpConnectionFactory implements InteropAmqpConnectionFactory, DelayStrate
1213
use DelayStrategyAwareTrait;
1314

1415
/**
15-
* @var array
16+
* @var ConnectionConfig
1617
*/
1718
private $config;
1819

@@ -22,65 +23,27 @@ class AmqpConnectionFactory implements InteropAmqpConnectionFactory, DelayStrate
2223
private $client;
2324

2425
/**
25-
* The config could be an array, string DSN or null. In case of null it will attempt to connect to localhost with default credentials.
26+
* @see \Enqueue\AmqpTools\ConnectionConfig for possible config formats and values
2627
*
27-
* [
28-
* 'host' => 'amqp.host The host to connect too. Note: Max 1024 characters.',
29-
* 'port' => 'amqp.port Port on the host.',
30-
* 'vhost' => 'amqp.vhost The virtual host on the host. Note: Max 128 characters.',
31-
* 'user' => 'amqp.user The user name to use. Note: Max 128 characters.',
32-
* 'pass' => 'amqp.password Password. Note: Max 128 characters.',
33-
* 'lazy' => 'the connection will be performed as later as possible, if the option set to true',
34-
* 'receive_method' => 'Could be either basic_get or basic_consume',
35-
* 'qos_prefetch_size' => 'The server will send a message in advance if it is equal to or smaller in size than the available prefetch size. May be set to zero, meaning "no specific limit"',
36-
* 'qos_prefetch_count' => 'Specifies a prefetch window in terms of whole messages.',
37-
* 'qos_global' => 'If "false" the QoS settings apply to the current channel only. If this field is "true", they are applied to the entire connection.',
38-
* ]
28+
* In addition this factory accepts next options:
29+
* receive_method - Could be either basic_get or basic_consume
3930
*
40-
* or
41-
*
42-
* amqp://user:pass@host:10000/vhost?lazy=true&socket=true
43-
*
44-
* @param array|string $config
31+
* @param array|string|null $config
4532
*/
4633
public function __construct($config = 'amqp:')
4734
{
48-
if (is_string($config) && 0 === strpos($config, 'amqp+bunny:')) {
49-
$config = str_replace('amqp+bunny:', 'amqp:', $config);
50-
}
51-
52-
if (empty($config) || 'amqp:' === $config) {
53-
$config = [];
54-
} elseif (is_string($config)) {
55-
$config = $this->parseDsn($config);
56-
} elseif (is_array($config)) {
57-
} else {
58-
throw new \LogicException('The config must be either an array of options, a DSN string or null');
59-
}
60-
61-
$config = array_replace($this->defaultConfig(), $config);
62-
63-
$config = array_replace($this->defaultConfig(), $config);
64-
if (array_key_exists('qos_global', $config)) {
65-
$config['qos_global'] = (bool) $config['qos_global'];
66-
}
67-
if (array_key_exists('qos_prefetch_count', $config)) {
68-
$config['qos_prefetch_count'] = (int) $config['qos_prefetch_count'];
69-
}
70-
if (array_key_exists('qos_prefetch_size', $config)) {
71-
$config['qos_prefetch_size'] = (int) $config['qos_prefetch_size'];
72-
}
73-
if (array_key_exists('lazy', $config)) {
74-
$config['lazy'] = (bool) $config['lazy'];
75-
}
76-
77-
$this->config = $config;
35+
$this->config = (new ConnectionConfig($config))
36+
->addSupportedSchemes('amqp+bunny')
37+
->addDefaultOption('receive_method', 'basic_get')
38+
->addDefaultOption('tcp_nodelay', null)
39+
->parse()
40+
;
7841

7942
$supportedMethods = ['basic_get', 'basic_consume'];
80-
if (false == in_array($this->config['receive_method'], $supportedMethods, true)) {
43+
if (false == in_array($this->config->getOption('receive_method'), $supportedMethods, true)) {
8144
throw new \LogicException(sprintf(
8245
'Invalid "receive_method" option value "%s". It could be only "%s"',
83-
$this->config['receive_method'],
46+
$this->config->getOption('receive_method'),
8447
implode('", "', $supportedMethods)
8548
));
8649
}
@@ -91,99 +54,65 @@ public function __construct($config = 'amqp:')
9154
*/
9255
public function createContext()
9356
{
94-
if ($this->config['lazy']) {
57+
if ($this->config->isLazy()) {
9558
$context = new AmqpContext(function () {
9659
$channel = $this->establishConnection()->channel();
97-
$channel->qos($this->config['qos_prefetch_size'], $this->config['qos_prefetch_count'], $this->config['qos_global']);
60+
$channel->qos($this->config->getQosPrefetchSize(), $this->config->getQosPrefetchCount(), $this->config->isQosGlobal());
9861

9962
return $channel;
100-
}, $this->config);
63+
}, $this->config->getConfig());
10164
$context->setDelayStrategy($this->delayStrategy);
10265

10366
return $context;
10467
}
10568

106-
$context = new AmqpContext($this->establishConnection()->channel(), $this->config);
69+
$context = new AmqpContext($this->establishConnection()->channel(), $this->config->getConfig());
10770
$context->setDelayStrategy($this->delayStrategy);
108-
$context->setQos($this->config['qos_prefetch_size'], $this->config['qos_prefetch_count'], $this->config['qos_global']);
71+
$context->setQos($this->config->getQosPrefetchSize(), $this->config->getQosPrefetchCount(), $this->config->isQosGlobal());
10972

11073
return $context;
11174
}
11275

11376
/**
114-
* @return Client
77+
* @return ConnectionConfig
11578
*/
116-
private function establishConnection()
79+
public function getConfig()
11780
{
118-
if (false == $this->client) {
119-
$this->client = new Client($this->config);
120-
$this->client->connect();
121-
}
122-
123-
return $this->client;
81+
return $this->config;
12482
}
12583

12684
/**
127-
* @param string $dsn
128-
*
129-
* @return array
85+
* @return Client
13086
*/
131-
private function parseDsn($dsn)
87+
private function establishConnection()
13288
{
133-
$dsnConfig = parse_url($dsn);
134-
if (false === $dsnConfig) {
135-
throw new \LogicException(sprintf('Failed to parse DSN "%s"', $dsn));
136-
}
137-
138-
$dsnConfig = array_replace([
139-
'scheme' => null,
140-
'host' => null,
141-
'port' => null,
142-
'user' => null,
143-
'pass' => null,
144-
'path' => null,
145-
'query' => null,
146-
], $dsnConfig);
147-
148-
if ('amqp' !== $dsnConfig['scheme']) {
149-
throw new \LogicException(sprintf('The given DSN scheme "%s" is not supported. Could be "amqp" only.', $dsnConfig['scheme']));
150-
}
151-
152-
if ($dsnConfig['query']) {
153-
$query = [];
154-
parse_str($dsnConfig['query'], $query);
155-
156-
$dsnConfig = array_replace($query, $dsnConfig);
89+
if (false == $this->client) {
90+
$bunnyConfig = [];
91+
$bunnyConfig['host'] = $this->config->getHost();
92+
$bunnyConfig['port'] = $this->config->getPort();
93+
$bunnyConfig['vhost'] = $this->config->getVHost();
94+
$bunnyConfig['user'] = $this->config->getUser();
95+
$bunnyConfig['password'] = $this->config->getPass();
96+
$bunnyConfig['read_write_timeout'] = min($this->config->getReadTimeout(), $this->config->getWriteTimeout());
97+
$bunnyConfig['timeout'] = $this->config->getConnectionTimeout();
98+
99+
// $bunnyConfig['persistent'] = $this->config->isPersisted();
100+
// if ($this->config->isPersisted()) {
101+
// $bunnyConfig['path'] = $this->config->getOption('path', $this->config->getOption('vhost'));
102+
// }
103+
104+
if ($this->config->getHeartbeat()) {
105+
$bunnyConfig['heartbeat'] = $this->config->getHeartbeat();
106+
}
107+
108+
if (null !== $this->config->getOption('tcp_nodelay')) {
109+
$bunnyConfig['tcp_nodelay'] = $this->config->getOption('tcp_nodelay');
110+
}
111+
112+
$this->client = new Client($bunnyConfig);
113+
$this->client->connect();
157114
}
158115

159-
$dsnConfig['vhost'] = ltrim($dsnConfig['path'], '/');
160-
161-
unset($dsnConfig['scheme'], $dsnConfig['query'], $dsnConfig['fragment'], $dsnConfig['path']);
162-
163-
$dsnConfig = array_map(function ($value) {
164-
return urldecode($value);
165-
}, $dsnConfig);
166-
167-
return $dsnConfig;
168-
}
169-
170-
/**
171-
* @return array
172-
*/
173-
private function defaultConfig()
174-
{
175-
return [
176-
'host' => 'localhost',
177-
'port' => 5672,
178-
'user' => 'guest',
179-
'pass' => 'guest',
180-
'lazy' => true,
181-
'vhost' => '/',
182-
'heartbeat' => 0,
183-
'receive_method' => 'basic_get',
184-
'qos_prefetch_size' => 0,
185-
'qos_prefetch_count' => 1,
186-
'qos_global' => false,
187-
];
116+
return $this->client;
188117
}
189118
}

0 commit comments

Comments
 (0)