Skip to content
This repository was archived by the owner on Jul 17, 2024. It is now read-only.

Commit f91d8e9

Browse files
switch to high level kafka consumer, manually commit offsets & fix travis builds (#1)
1 parent 38c7e6c commit f91d8e9

File tree

9 files changed

+197
-295
lines changed

9 files changed

+197
-295
lines changed

.travis.yml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
11
language: php
22

3+
sudo: required
4+
35
php:
46
- 5.6
57
- 7.0
68
- 7.1
79
- hhvm
810
- nightly
911

10-
before_script:
11-
- composer install
12+
install:
13+
- ./travis-install.sh
14+
- travis_retry composer install --no-interaction
15+
16+
matrix:
17+
fast_finish: true
18+
allow_failures:
19+
- php: 7.1
20+
- php: hhvm
21+
- php: nightly
22+
23+
notifications:
24+
slack:
25+
secure: LCF/2QlcsU0V5HmR5gJx1/SAmoZQ39zxG3jrRVOFE6itPk4au8Aal6b1l6HSlhLYhzyv84pmobMhy/Cjm6lePZyE9QalMcsRzLBQ1oZzF7fVB+ypO3W7dG6V0CnGGtkGO4MsSTwMnQ9X/GEWxNBehcmo0kRnvQPGWtEFSHbAqV+86yq/lpfBW3sXuv3TV6mtCwzfaTkermlMC63i6p3rXwwLgf19kAJ//Gp7d8/eVnrq+CbyGOD6+pAHbCFWxEHr2o6P1SMS8mnPRsgBQ+qCNICWRrmb+8gOUUS5JgnPJSAWLI/n0Q+n8CkJTjIfRK+N352n+CitCRcq+76alT05ogW8CY4mJDp2Qn0nEs5h+6NGGIcwveF3kcXmBpVDz2N2J5zpjzk4mybXX8gilxJ1WnPVGRD0cpJSazmpaNV6Y7lRMM1LqvIiY1LNzFQdp1CJjb0n6MQPaSnUF1w/e2k/UfLh1ZGTDy1US0p7RtY1RibbEFVUbtneEENRTYWoVQt+coBF+DFdsDYX/HoEuTkWQeKGmHBrtSX3UEz7v50QkQkr4jvk151JFi7fqJZLazOTbP59g0WSEggTXfauVw/S14S1Ir2+MA/glAYZXzFneKyM9fKiVbxMFrpJNwyurM5ODE72iDKJ5ejkYZLe9EpTRYbW/1p5/1yOK+s49cqGP2w=
26+
on_success: change
27+
on_failure: always

Dockerfile

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,17 @@ RUN apt-get update \
99
&& DEBIAN_FRONTEND=noninteractive apt-get install -y \
1010
git \
1111
zlib1g-dev \
12-
librdkafka-dev \
1312
unzip \
13+
python \
14+
&& ( \
15+
cd /tmp \
16+
&& mkdir librdkafka \
17+
&& cd librdkafka \
18+
&& git clone https://github.com/edenhill/librdkafka.git . \
19+
&& ./configure \
20+
&& make \
21+
&& make install \
22+
) \
1423
&& rm -r /var/lib/apt/lists/*
1524

1625
# PHP Extensions

README.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,24 @@ A Kafka adapter for the [php-pubsub](https://github.com/Superbalist/php-pubsub)
4444
## Usage
4545

4646
```php
47-
// use this topic config for both the producer and consumer
48-
$topicConfig = new \RdKafka\TopicConf();
49-
$topicConfig->set('auto.offset.reset', 'smallest');
50-
$topicConfig->set('auto.commit.interval.ms', 300);
47+
// create consumer
48+
$topicConf = new \RdKafka\TopicConf();
49+
$topicConf->set('auto.offset.reset', 'smallest');
50+
51+
$conf = new \RdKafka\Conf();
52+
$conf->set('group.id', 'php-pubsub');
53+
$conf->set('metadata.broker.list', '127.0.0.1');
54+
$conf->set('enable.auto.commit', 'false');
55+
$conf->set('offset.store.method', 'broker');
56+
$conf->setDefaultTopicConf($topicConf);
57+
58+
$consumer = new \RdKafka\KafkaConsumer($conf);
5159
5260
// create producer
5361
$producer = new \RdKafka\Producer();
5462
$producer->addBrokers('127.0.0.1');
5563
56-
// create consumer
57-
// see https://arnaud-lb.github.io/php-rdkafka/phpdoc/rdkafka.examples-high-level-consumer.html
58-
$config = new \RdKafka\Conf();
59-
$config->set('group.id', 'php-pubsub');
60-
61-
$consumer = new \RdKafka\Consumer($config);
62-
$consumer->addBrokers('127.0.0.1');
63-
64-
$adapter = new \Superbalist\PubSub\Kafka\KafkaPubSubAdapter($producer, $consumer, $topicConfig);
64+
$adapter = new \Superbalist\PubSub\Kafka\KafkaPubSubAdapter($producer, $consumer);
6565
6666
// consume messages
6767
// note: this is a blocking call

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ services:
88
- ./src:/opt/php-pubsub/src
99
- ./examples:/opt/php-pubsub/examples
1010
kafka:
11-
image: spotify/kafka
11+
image: flozano/kafka
1212
environment:
1313
- ADVERTISED_HOST=HOSTIP
1414
- ADVERTISED_PORT=9092

examples/KafkaConsumerExample.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@
22

33
include __DIR__ . '/../vendor/autoload.php';
44

5-
// use this topic config for both the producer and consumer
6-
$topicConfig = new \RdKafka\TopicConf();
7-
$topicConfig->set('auto.offset.reset', 'smallest');
8-
$topicConfig->set('auto.commit.interval.ms', 300);
5+
// create consumer
6+
$topicConf = new \RdKafka\TopicConf();
7+
$topicConf->set('auto.offset.reset', 'smallest');
8+
9+
$conf = new \RdKafka\Conf();
10+
$conf->set('group.id', 'php-pubsub');
11+
$conf->set('metadata.broker.list', 'kafka');
12+
$conf->set('enable.auto.commit', 'false');
13+
$conf->set('offset.store.method', 'broker');
14+
$conf->setDefaultTopicConf($topicConf);
15+
16+
$consumer = new \RdKafka\KafkaConsumer($conf);
917

1018
// create producer
1119
$producer = new \RdKafka\Producer();
1220
$producer->addBrokers('kafka');
1321

14-
// create consumer
15-
// see https://arnaud-lb.github.io/php-rdkafka/phpdoc/rdkafka.examples-high-level-consumer.html
16-
$config = new \RdKafka\Conf();
17-
$config->set('group.id', 'php-pubsub');
18-
19-
$consumer = new \RdKafka\Consumer($config);
20-
$consumer->addBrokers('kafka');
21-
22-
$adapter = new \Superbalist\PubSub\Kafka\KafkaPubSubAdapter($producer, $consumer, $topicConfig);
22+
$adapter = new \Superbalist\PubSub\Kafka\KafkaPubSubAdapter($producer, $consumer);
2323

2424
$adapter->subscribe('my_channel', function ($message) {
2525
var_dump($message);

examples/KafkaPublishExample.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@
22

33
include __DIR__ . '/../vendor/autoload.php';
44

5-
// use this topic config for both the producer and consumer
6-
$topicConfig = new \RdKafka\TopicConf();
7-
$topicConfig->set('auto.offset.reset', 'smallest');
8-
$topicConfig->set('auto.commit.interval.ms', 300);
5+
// create consumer
6+
$topicConf = new \RdKafka\TopicConf();
7+
$topicConf->set('auto.offset.reset', 'smallest');
8+
9+
$conf = new \RdKafka\Conf();
10+
$conf->set('group.id', 'php-pubsub');
11+
$conf->set('metadata.broker.list', 'kafka');
12+
$conf->set('enable.auto.commit', 'false');
13+
$conf->set('offset.store.method', 'broker');
14+
$conf->setDefaultTopicConf($topicConf);
15+
16+
$consumer = new \RdKafka\KafkaConsumer($conf);
917

1018
// create producer
1119
$producer = new \RdKafka\Producer();
1220
$producer->addBrokers('kafka');
1321

14-
// create consumer
15-
// see https://arnaud-lb.github.io/php-rdkafka/phpdoc/rdkafka.examples-high-level-consumer.html
16-
$config = new \RdKafka\Conf();
17-
$config->set('group.id', 'php-pubsub');
18-
19-
$consumer = new \RdKafka\Consumer($config);
20-
$consumer->addBrokers('kafka');
21-
22-
$adapter = new \Superbalist\PubSub\Kafka\KafkaPubSubAdapter($producer, $consumer, $topicConfig);
22+
$adapter = new \Superbalist\PubSub\Kafka\KafkaPubSubAdapter($producer, $consumer);
2323

2424
$adapter->publish('my_channel', 'HELLO WORLD');
2525
$adapter->publish('my_channel', json_encode(['hello' => 'world']));

src/KafkaPubSubAdapter.php

Lines changed: 12 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -13,37 +13,18 @@ class KafkaPubSubAdapter implements PubSubAdapterInterface
1313
protected $producer;
1414

1515
/**
16-
* @var \RdKafka\Consumer
16+
* @var \RdKafka\KafkaConsumer
1717
*/
1818
protected $consumer;
1919

20-
/**
21-
* @var \RdKafka\TopicConf
22-
*/
23-
protected $topicConfig;
24-
25-
/**
26-
* @var mixed
27-
*/
28-
protected $consumerOffset;
29-
3020
/**
3121
* @param \RdKafka\Producer $producer
32-
* @param \RdKafka\Consumer $consumer
33-
* @param \RdKafka\TopicConf $topicConfig
34-
* @param mixed $consumerOffset The offset at which to start consumption
35-
* (RD_KAFKA_OFFSET_BEGINNING, RD_KAFKA_OFFSET_END, RD_KAFKA_OFFSET_STORED)
22+
* @param \RdKafka\KafkaConsumer $consumer
3623
*/
37-
public function __construct(
38-
\RdKafka\Producer $producer,
39-
\RdKafka\Consumer $consumer,
40-
\RdKafka\TopicConf $topicConfig,
41-
$consumerOffset = RD_KAFKA_OFFSET_END
42-
) {
24+
public function __construct(\RdKafka\Producer $producer, \RdKafka\KafkaConsumer $consumer)
25+
{
4326
$this->producer = $producer;
4427
$this->consumer = $consumer;
45-
$this->topicConfig = $topicConfig;
46-
$this->consumerOffset = $consumerOffset;
4728
}
4829

4930
/**
@@ -59,45 +40,13 @@ public function getProducer()
5940
/**
6041
* Return the Kafka consumer.
6142
*
62-
* @return \RdKafka\Consumer
43+
* @return \RdKafka\KafkaConsumer
6344
*/
6445
public function getConsumer()
6546
{
6647
return $this->consumer;
6748
}
6849

69-
/**
70-
* Return the Kafka TopicConfig.
71-
*
72-
* @return \RdKafka\TopicConf
73-
*/
74-
public function getTopicConfig()
75-
{
76-
return $this->topicConfig;
77-
}
78-
79-
/**
80-
* Return the Kafka consumer offset at which `subscribe()` calls begin consumption.
81-
*
82-
* @return mixed
83-
*/
84-
public function getConsumerOffset()
85-
{
86-
return $this->consumerOffset;
87-
}
88-
89-
/**
90-
* Set the Kafka consumer offset at which `subscribe()` calls begin consumption.
91-
*
92-
* This can be one of `RD_KAFKA_OFFSET_BEGINNING`, `RD_KAFKA_OFFSET_END` or `RD_KAFKA_OFFSET_STORED`
93-
*
94-
* @param mixed $consumerOffset
95-
*/
96-
public function setConsumerOffset($consumerOffset)
97-
{
98-
$this->consumerOffset = $consumerOffset;
99-
}
100-
10150
/**
10251
* Subscribe a handler to a channel.
10352
*
@@ -107,14 +56,12 @@ public function setConsumerOffset($consumerOffset)
10756
*/
10857
public function subscribe($channel, callable $handler)
10958
{
110-
$topic = $this->consumer->newTopic($channel, $this->topicConfig);
111-
112-
$topic->consumeStart(0, $this->consumerOffset);
59+
$this->consumer->subscribe([$channel]);
11360

11461
$isSubscriptionLoopActive = true;
11562

11663
while ($isSubscriptionLoopActive) {
117-
$message = $topic->consume(0, 1000);
64+
$message = $this->consumer->consume(300);
11865

11966
if ($message === null) {
12067
continue;
@@ -126,10 +73,12 @@ public function subscribe($channel, callable $handler)
12673

12774
if ($payload === 'unsubscribe') {
12875
$isSubscriptionLoopActive = false;
129-
break;
76+
} else {
77+
call_user_func($handler, $payload);
13078
}
13179

132-
call_user_func($handler, $payload);
80+
$this->consumer->commitAsync($message);
81+
13382
break;
13483
case RD_KAFKA_RESP_ERR__PARTITION_EOF:
13584
case RD_KAFKA_RESP_ERR__TIMED_OUT:
@@ -148,7 +97,7 @@ public function subscribe($channel, callable $handler)
14897
*/
14998
public function publish($channel, $message)
15099
{
151-
$topic = $this->producer->newTopic($channel, $this->topicConfig);
100+
$topic = $this->producer->newTopic($channel);
152101
$topic->produce(RD_KAFKA_PARTITION_UA, 0, Utils::serializeMessage($message));
153102
}
154103
}

0 commit comments

Comments
 (0)