forked from php-enqueue/rdkafka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RdKafkaContext.php
223 lines (180 loc) · 6.28 KB
/
RdKafkaContext.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
<?php
declare(strict_types=1);
namespace Enqueue\RdKafka;
use Interop\Queue\Consumer;
use Interop\Queue\Context;
use Interop\Queue\Destination;
use Interop\Queue\Exception\InvalidDestinationException;
use Interop\Queue\Exception\PurgeQueueNotSupportedException;
use Interop\Queue\Exception\SubscriptionConsumerNotSupportedException;
use Interop\Queue\Exception\TemporaryQueueNotSupportedException;
use Interop\Queue\Message;
use Interop\Queue\Producer;
use Interop\Queue\Queue;
use Interop\Queue\SubscriptionConsumer;
use Interop\Queue\Topic;
use RdKafka\Conf;
use RdKafka\KafkaConsumer;
use RdKafka\Producer as VendorProducer;
class RdKafkaContext implements Context
{
use SerializerAwareTrait;
/**
* @var array
*/
private $config;
/**
* @var Conf
*/
private $conf;
/**
* @var RdKafkaProducer
*/
private $producer;
/**
* @var KafkaConsumer[]
*/
private $kafkaConsumers;
/**
* @var RdKafkaConsumer[]
*/
private $rdKafkaConsumers;
public function __construct(array $config)
{
$this->config = $config;
$this->kafkaConsumers = [];
$this->rdKafkaConsumers = [];
$this->setSerializer(new JsonSerializer());
}
/**
* @return RdKafkaMessage
*/
public function createMessage(string $body = '', array $properties = [], array $headers = []): Message
{
return new RdKafkaMessage($body, $properties, $headers);
}
/**
* @return RdKafkaTopic
*/
public function createTopic(string $topicName): Topic
{
return new RdKafkaTopic($topicName);
}
/**
* @return RdKafkaTopic
*/
public function createQueue(string $queueName): Queue
{
return new RdKafkaTopic($queueName);
}
public function createTemporaryQueue(): Queue
{
throw TemporaryQueueNotSupportedException::providerDoestNotSupportIt();
}
/**
* @return RdKafkaProducer
*/
public function createProducer(): Producer
{
if (!isset($this->producer)) {
$producer = new VendorProducer($this->getConf());
if (isset($this->config['log_level'])) {
$producer->setLogLevel($this->config['log_level']);
}
$this->producer = new RdKafkaProducer($producer, $this->getSerializer());
// Once created RdKafkaProducer can store messages internally that need to be delivered before PHP shuts
// down. Otherwise, we are bound to lose messages in transit.
// Note that it is generally preferable to call "close" method explicitly before shutdown starts, since
// otherwise we might not have access to some objects, like database connections.
register_shutdown_function([$this->producer, 'flush'], $this->config['shutdown_timeout'] ?? -1);
}
return $this->producer;
}
/**
* @param RdKafkaTopic $destination
*
* @return RdKafkaConsumer
*/
public function createConsumer(Destination $destination): Consumer
{
InvalidDestinationException::assertDestinationInstanceOf($destination, RdKafkaTopic::class);
$queueName = $destination->getQueueName();
if (!isset($this->rdKafkaConsumers[$queueName])) {
$this->kafkaConsumers[] = $kafkaConsumer = new KafkaConsumer($this->getConf());
$consumer = new RdKafkaConsumer(
$kafkaConsumer,
$this,
$destination,
$this->getSerializer()
);
if (isset($this->config['commit_async'])) {
$consumer->setCommitAsync($this->config['commit_async']);
}
$this->rdKafkaConsumers[$queueName] = $consumer;
}
return $this->rdKafkaConsumers[$queueName];
}
public function close(): void
{
$kafkaConsumers = $this->kafkaConsumers;
$this->kafkaConsumers = [];
$this->rdKafkaConsumers = [];
foreach ($kafkaConsumers as $kafkaConsumer) {
$kafkaConsumer->unsubscribe();
}
// Compatibility with phprdkafka 4.0.
if (isset($this->producer)) {
$this->producer->flush($this->config['shutdown_timeout'] ?? -1);
}
}
public function createSubscriptionConsumer(): SubscriptionConsumer
{
throw SubscriptionConsumerNotSupportedException::providerDoestNotSupportIt();
}
public function purgeQueue(Queue $queue): void
{
throw PurgeQueueNotSupportedException::providerDoestNotSupportIt();
}
public static function getLibrdKafkaVersion(): string
{
if (!defined('RD_KAFKA_VERSION')) {
throw new \RuntimeException('RD_KAFKA_VERSION constant is not defined. Phprdkafka is probably not installed');
}
$major = (RD_KAFKA_VERSION & 0xFF000000) >> 24;
$minor = (RD_KAFKA_VERSION & 0x00FF0000) >> 16;
$patch = (RD_KAFKA_VERSION & 0x0000FF00) >> 8;
return "$major.$minor.$patch";
}
private function getConf(): Conf
{
if (null === $this->conf) {
$this->conf = new Conf();
if (isset($this->config['topic']) && is_array($this->config['topic'])) {
foreach ($this->config['topic'] as $key => $value) {
$this->conf->set($key, $value);
}
}
if (isset($this->config['partitioner'])) {
$this->conf->set('partitioner', $this->config['partitioner']);
}
if (isset($this->config['global']) && is_array($this->config['global'])) {
foreach ($this->config['global'] as $key => $value) {
$this->conf->set($key, $value);
}
}
if (isset($this->config['dr_msg_cb'])) {
$this->conf->setDrMsgCb($this->config['dr_msg_cb']);
}
if (isset($this->config['error_cb'])) {
$this->conf->setErrorCb($this->config['error_cb']);
}
if (isset($this->config['rebalance_cb'])) {
$this->conf->setRebalanceCb($this->config['rebalance_cb']);
}
if (isset($this->config['stats_cb'])) {
$this->conf->setStatsCb($this->config['stats_cb']);
}
}
return $this->conf;
}
}