forked from salebab/phpkafka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
library.c
107 lines (90 loc) · 3.04 KB
/
library.c
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
#include <ctype.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <syslog.h>
#include <sys/time.h>
#include <errno.h>
#include <syslog.h>
#include <time.h>
#include "library.h"
#include "librdkafka/rdkafka.h"
static int run = 1;
static rd_kafka_t *rk;
void kafka_stop(int sig) {
run = 0;
fclose(stdin); /* abort fgets() */
rd_kafka_destroy(rk);
rk = NULL;
}
void kafka_err_cb (rd_kafka_t *rk, int err, const char *reason, void *opaque) {
openlog("phpkafka", 0, LOG_USER);
syslog(LOG_INFO, "phpkafka - ERROR CALLBACK: %s: %s: %s\n",
rd_kafka_name(rk), rd_kafka_err2str(err), reason);
kafka_stop(err);
}
void kafka_msg_delivered (rd_kafka_t *rk,
void *payload, size_t len,
int error_code,
void *opaque, void *msg_opaque) {
if (error_code) {
openlog("phpkafka", 0, LOG_USER);
syslog(LOG_INFO, "phpkafka - Message delivery failed: %s",
rd_kafka_err2str(error_code));
}
}
void kafka_setup(char* brokers)
{
if(rk == NULL) {
char errstr[512];
rd_kafka_conf_t *conf;
/* Kafka configuration */
conf = rd_kafka_conf_new();
/* Set up a message delivery report callback.
* It will be called once for each message, either on successful
* delivery to broker, or upon failure to deliver to broker. */
rd_kafka_conf_set_dr_cb(conf, kafka_msg_delivered);
rd_kafka_conf_set_error_cb(conf, kafka_err_cb);
if (!(rk = rd_kafka_new(RD_KAFKA_PRODUCER, conf, errstr, sizeof(errstr)))) {
openlog("phpkafka", 0, LOG_USER);
syslog(LOG_INFO, "phpkafka - failed to create new producer: %s", errstr);
exit(1);
}
/* Add brokers */
if (rd_kafka_brokers_add(rk, brokers) == 0) {
openlog("phpkafka", 0, LOG_USER);
syslog(LOG_INFO, "php kafka - No valid brokers specified");
exit(1);
}
}
}
void kafka_destroy()
{
if(rk != NULL) {
rd_kafka_destroy(rk);
rd_kafka_wait_destroyed(1000);
rk = NULL;
}
}
void kafka_produce(char* topic, char* msg, int msg_len)
{
signal(SIGINT, kafka_stop);
signal(SIGPIPE, kafka_stop);
rd_kafka_topic_t *rkt;
int partition = 0;
/* Create topic */
rkt = rd_kafka_topic_new(rk, topic, NULL);
rd_kafka_produce(rkt, partition,
RD_KAFKA_MSG_F_COPY,
/* Payload and length */
msg, msg_len,
/* Optional key and its length */
NULL, 0,
/* Message opaque, provided in
* delivery report callback as
* msg_opaque. */
NULL);
rd_kafka_poll(rk, 0);
rd_kafka_topic_destroy(rkt);
}