Skip to content

Commit b923d2c

Browse files
authored
Merge pull request #108 from php-enqueue/amqp-prefetch-support
[amqp] Add pre_fetch_count, pre_fetch_size options.
2 parents 65c5b9c + 1c6d283 commit b923d2c

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

Diff for: pkg/amqp-ext/AmqpConnectionFactory.php

+24-2
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,30 @@ public function createContext()
6161
{
6262
if ($this->config['lazy']) {
6363
return new AmqpContext(function () {
64-
return new \AMQPChannel($this->establishConnection());
64+
return $this->createExtContext($this->establishConnection());
6565
});
6666
}
6767

68-
return new AmqpContext(new \AMQPChannel($this->establishConnection()));
68+
return new AmqpContext($this->createExtContext($this->establishConnection()));
69+
}
70+
71+
/**
72+
* @param \AMQPConnection $extConnection
73+
*
74+
* @return \AMQPChannel
75+
*/
76+
private function createExtContext(\AMQPConnection $extConnection)
77+
{
78+
$channel = new \AMQPChannel($extConnection);
79+
if (false == empty($this->config['pre_fetch_count'])) {
80+
$channel->setPrefetchCount((int) $this->config['pre_fetch_count']);
81+
}
82+
83+
if (false == empty($this->config['pre_fetch_size'])) {
84+
$channel->setPrefetchSize((int) $this->config['pre_fetch_size']);
85+
}
86+
87+
return $channel;
6988
}
7089

7190
/**
@@ -118,6 +137,7 @@ private function parseDsn($dsn)
118137
if ($dsnConfig['query']) {
119138
$query = [];
120139
parse_str($dsnConfig['query'], $query);
140+
121141
$dsnConfig = array_replace($query, $dsnConfig);
122142
}
123143

@@ -149,6 +169,8 @@ private function defaultConfig()
149169
'connect_timeout' => null,
150170
'persisted' => false,
151171
'lazy' => true,
172+
'pre_fetch_count' => null,
173+
'pre_fetch_size' => null,
152174
];
153175
}
154176
}

Diff for: pkg/amqp-ext/Tests/AmqpConnectionFactoryConfigTest.php

+50
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ public static function provideConfigs()
6565
'connect_timeout' => null,
6666
'persisted' => false,
6767
'lazy' => true,
68+
'pre_fetch_count' => null,
69+
'pre_fetch_size' => null,
6870
],
6971
];
7072

@@ -83,6 +85,8 @@ public static function provideConfigs()
8385
'connect_timeout' => null,
8486
'persisted' => false,
8587
'lazy' => true,
88+
'pre_fetch_count' => null,
89+
'pre_fetch_size' => null,
8690
],
8791
];
8892

@@ -99,6 +103,8 @@ public static function provideConfigs()
99103
'connect_timeout' => null,
100104
'persisted' => false,
101105
'lazy' => true,
106+
'pre_fetch_count' => null,
107+
'pre_fetch_size' => null,
102108
],
103109
];
104110

@@ -115,6 +121,8 @@ public static function provideConfigs()
115121
'connect_timeout' => null,
116122
'persisted' => false,
117123
'lazy' => true,
124+
'pre_fetch_count' => null,
125+
'pre_fetch_size' => null,
118126
],
119127
];
120128

@@ -131,6 +139,8 @@ public static function provideConfigs()
131139
'connect_timeout' => '2',
132140
'persisted' => false,
133141
'lazy' => '',
142+
'pre_fetch_count' => null,
143+
'pre_fetch_size' => null,
134144
],
135145
];
136146

@@ -147,6 +157,8 @@ public static function provideConfigs()
147157
'connect_timeout' => null,
148158
'persisted' => false,
149159
'lazy' => true,
160+
'pre_fetch_count' => null,
161+
'pre_fetch_size' => null,
150162
],
151163
];
152164

@@ -163,6 +175,44 @@ public static function provideConfigs()
163175
'connect_timeout' => null,
164176
'persisted' => false,
165177
'lazy' => false,
178+
'pre_fetch_count' => null,
179+
'pre_fetch_size' => null,
180+
],
181+
];
182+
183+
yield [
184+
['pre_fetch_count' => 123, 'pre_fetch_size' => 321],
185+
[
186+
'host' => 'localhost',
187+
'port' => 5672,
188+
'vhost' => '/',
189+
'user' => 'guest',
190+
'pass' => 'guest',
191+
'read_timeout' => null,
192+
'write_timeout' => null,
193+
'connect_timeout' => null,
194+
'persisted' => false,
195+
'lazy' => true,
196+
'pre_fetch_count' => 123,
197+
'pre_fetch_size' => 321,
198+
],
199+
];
200+
201+
yield [
202+
'amqp://user:pass@host:10000/vhost?pre_fetch_count=123&pre_fetch_size=321',
203+
[
204+
'host' => 'host',
205+
'port' => '10000',
206+
'vhost' => 'vhost',
207+
'user' => 'user',
208+
'pass' => 'pass',
209+
'read_timeout' => null,
210+
'write_timeout' => null,
211+
'connect_timeout' => null,
212+
'persisted' => false,
213+
'lazy' => true,
214+
'pre_fetch_count' => 123,
215+
'pre_fetch_size' => 321,
166216
],
167217
];
168218
}

0 commit comments

Comments
 (0)