forked from brg-liuwei/ngx_kafka_module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ngx_http_kafka_module.c
428 lines (347 loc) · 11.9 KB
/
ngx_http_kafka_module.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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*
* nginx kafka module
*
* using librdkafka: https://github.com/edenhill/librdkafka
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <librdkafka/rdkafka.h>
#define KAFKA_TOPIC_MAXLEN 256
#define KAFKA_BROKER_MAXLEN 512
static ngx_int_t ngx_http_kafka_init_worker(ngx_cycle_t *cycle);
static void ngx_http_kafka_exit_worker(ngx_cycle_t *cycle);
static void *ngx_http_kafka_create_main_conf(ngx_conf_t *cf);
static void *ngx_http_kafka_create_loc_conf(ngx_conf_t *cf);
static char *ngx_http_set_kafka(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static char *ngx_http_set_kafka_topic(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static char *ngx_http_set_kafka_broker(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static ngx_int_t ngx_http_kafka_handler(ngx_http_request_t *r);
static void ngx_http_kafka_post_callback_handler(ngx_http_request_t *r);
typedef enum {
ngx_str_push = 0,
ngx_str_pop = 1
} ngx_str_op;
static void ngx_str_helper(ngx_str_t *str, ngx_str_op op);
typedef struct {
rd_kafka_t *rk;
rd_kafka_conf_t *rkc;
size_t broker_size;
size_t nbrokers;
ngx_str_t *brokers;
} ngx_http_kafka_main_conf_t;
static void ngx_http_kafka_main_conf_broker_add(ngx_http_kafka_main_conf_t *cf,
ngx_str_t *broker, ngx_pool_t *pool);
typedef struct {
ngx_log_t *log;
ngx_str_t topic; /* kafka topic */
ngx_str_t broker; /* broker addr (eg: localhost:9092) */
rd_kafka_topic_t *rkt;
rd_kafka_topic_conf_t *rktc;
} ngx_http_kafka_loc_conf_t;
static ngx_command_t ngx_http_kafka_commands[] = {
{
ngx_string("kafka"),
NGX_HTTP_MAIN_CONF|NGX_CONF_NOARGS,
ngx_http_set_kafka,
NGX_HTTP_MAIN_CONF_OFFSET,
0,
NULL },
{
ngx_string("kafka_topic"),
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_http_set_kafka_topic,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_kafka_loc_conf_t, topic),
NULL },
{
ngx_string("kafka_broker"),
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_http_set_kafka_broker,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_kafka_loc_conf_t, broker),
NULL },
ngx_null_command
};
static ngx_http_module_t ngx_http_kafka_module_ctx = {
NULL, /* pre conf */
NULL, /* post conf */
/* create main conf */
ngx_http_kafka_create_main_conf,
NULL, /* init main conf */
NULL, /* create server conf */
NULL, /* init server conf */
/* create local conf */
ngx_http_kafka_create_loc_conf,
NULL, /* merge location conf */
};
ngx_module_t ngx_http_kafka_module = {
NGX_MODULE_V1,
&ngx_http_kafka_module_ctx, /* module context */
ngx_http_kafka_commands, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
ngx_http_kafka_init_worker, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
ngx_http_kafka_exit_worker, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
ngx_int_t ngx_str_equal(ngx_str_t *s1, ngx_str_t *s2)
{
if (s1->len != s1->len) {
return 0;
}
if (ngx_memcmp(s1->data, s2->data, s1->len) != 0) {
return 0;
}
return 1;
}
void ngx_http_kafka_main_conf_broker_add(ngx_http_kafka_main_conf_t *cf,
ngx_str_t *broker, ngx_pool_t *pool)
{
size_t n;
ngx_str_t *new_brokers;
for (n = 0; n != cf->nbrokers; ++n) {
if (ngx_str_equal(broker, &cf->brokers[n])) {
return;
}
}
if (cf->nbrokers == cf->broker_size) {
if (cf->broker_size == 0) {
cf->broker_size = 2;
}
new_brokers = ngx_pcalloc(pool, sizeof(ngx_str_t) * (cf->broker_size * 2));
if (new_brokers == NULL) {
ngx_log_error(NGX_LOG_EMERG, pool->log, 0, "ngx no memory");
return;
}
for (n = 0; n != cf->nbrokers; ++n) {
new_brokers[n].len = cf->brokers[n].len;
new_brokers[n].data = cf->brokers[n].data;
}
cf->brokers = new_brokers;
cf->broker_size *= 2;
}
n = cf->nbrokers;
/* 1 more byte for '\0' */
cf->brokers[n].data = (u_char *)ngx_pcalloc(pool, sizeof(u_char) * (broker->len) + 1);
if (cf->brokers[n].data == NULL) {
ngx_log_error(NGX_LOG_EMERG, pool->log, 0, "ngx no memory");
}
cf->brokers[n].len = broker->len;
ngx_memcpy(cf->brokers[n].data, broker->data, broker->len);
cf->brokers[n].data[broker->len] = '\0';
cf->nbrokers++;
}
void *ngx_http_kafka_create_main_conf(ngx_conf_t *cf)
{
ngx_http_kafka_main_conf_t *conf;
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_kafka_main_conf_t));
if (conf == NULL) {
return NGX_CONF_ERROR;
}
conf->rk = NULL;
conf->rkc = NULL;
conf->broker_size = 0;
conf->nbrokers = 0;
conf->brokers = NULL;
return conf;
}
void *ngx_http_kafka_create_loc_conf(ngx_conf_t *cf)
{
ngx_http_kafka_loc_conf_t *conf;
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_kafka_loc_conf_t));
if (conf == NULL) {
return NGX_CONF_ERROR;
}
conf->log = cf->log;
ngx_str_null(&conf->topic);
ngx_str_null(&conf->broker);
return conf;
}
void kafka_callback_handler(rd_kafka_t *rk, void *msg, size_t len, int err, void *opaque, void *msg_opaque)
{
if (err != 0) {
ngx_log_error(NGX_LOG_ERR, (ngx_log_t *)msg_opaque, 0, rd_kafka_err2str(err));
}
}
char *ngx_http_set_kafka(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
/* we can add more code here to config ngx_http_kafka_main_conf */
return NGX_CONF_OK;
}
char *ngx_http_set_kafka_topic(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_http_core_loc_conf_t *clcf;
ngx_http_kafka_loc_conf_t *local_conf;
/* install ngx_http_kafka_handler */
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
if (clcf == NULL) {
return NGX_CONF_ERROR;
}
clcf->handler = ngx_http_kafka_handler;
/* ngx_http_kafka_loc_conf_t::topic assignment */
if (ngx_conf_set_str_slot(cf, cmd, conf) != NGX_CONF_OK) {
return NGX_CONF_ERROR;
}
local_conf = conf;
local_conf->rktc = rd_kafka_topic_conf_new();
return NGX_CONF_OK;
}
char *ngx_http_set_kafka_broker(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_http_kafka_loc_conf_t *local_conf;
ngx_http_kafka_main_conf_t *main_conf;
/* ngx_http_kafka_loc_conf_t::broker assignment */
if (ngx_conf_set_str_slot(cf, cmd, conf) != NGX_CONF_OK) {
return NGX_CONF_ERROR;
}
local_conf = conf;
main_conf = ngx_http_conf_get_module_main_conf(cf, ngx_http_kafka_module);
if (main_conf == NULL) {
return NGX_CONF_ERROR;
}
ngx_http_kafka_main_conf_broker_add(main_conf, &local_conf->broker, cf->pool);
return NGX_CONF_OK;
}
static ngx_int_t ngx_http_kafka_handler(ngx_http_request_t *r)
{
ngx_int_t rv;
if (!(r->method & NGX_HTTP_POST)) {
return NGX_HTTP_NOT_ALLOWED;
}
rv = ngx_http_read_client_request_body(r, ngx_http_kafka_post_callback_handler);
if (rv >= NGX_HTTP_SPECIAL_RESPONSE) {
return rv;
}
return NGX_DONE;
}
static void ngx_http_kafka_post_callback_handler(ngx_http_request_t *r)
{
static const char rc[] = "ngx_http_kafka_module ok\n";
int nbufs;
u_char *msg;
size_t len;
ngx_buf_t *buf;
ngx_chain_t out;
ngx_chain_t *cl, *in;
ngx_http_request_body_t *body;
ngx_http_kafka_main_conf_t *main_conf;
ngx_http_kafka_loc_conf_t *local_conf;
/* get body */
body = r->request_body;
if (body == NULL || body->bufs == NULL) {
ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
return;
}
main_conf = NULL;
/* calc len and bufs */
len = 0;
nbufs = 0;
in = body->bufs;
for (cl = in; cl != NULL; cl = cl->next) {
nbufs++;
len += (size_t)(cl->buf->last - cl->buf->pos);
}
/* get msg */
if (nbufs == 0) {
goto end;
}
if (nbufs == 1 && ngx_buf_in_memory(in->buf)) {
msg = in->buf->pos;
} else {
if ((msg = ngx_pnalloc(r->pool, len)) == NULL) {
ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
return;
}
for (cl = in; cl != NULL; cl = cl->next) {
if (ngx_buf_in_memory(cl->buf)) {
msg = ngx_copy(msg, cl->buf->pos, cl->buf->last - cl->buf->pos);
} else {
/* TODO: handle buf in file */
ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0,
"ngx_http_kafka_handler cannot handler in-file-post-buf");
goto end;
}
}
msg -= len;
}
/* send to kafka */
main_conf = ngx_http_get_module_main_conf(r, ngx_http_kafka_module);
local_conf = ngx_http_get_module_loc_conf(r, ngx_http_kafka_module);
if (local_conf->rkt == NULL) {
ngx_str_helper(&local_conf->topic, ngx_str_push);
local_conf->rkt = rd_kafka_topic_new(main_conf->rk,
(const char *)local_conf->topic.data, local_conf->rktc);
ngx_str_helper(&local_conf->topic, ngx_str_pop);
}
/*
* the last param should NOT be r->connection->log, for reason that
* the callback handler ( func: kafka_callback_handler) would be called ASYNC-ly
* when some errors being happened. At this time,
* ngx_http_finalize_request may have been invoked, in this case, the object r
* had been destroyed but kafka_callback_handler use pointer r->connection->log.
* DUANG! Worker process CRASH!
*
* Thanks for engineers of www.360buy.com report me this bug.
* */
rd_kafka_produce(local_conf->rkt, RD_KAFKA_PARTITION_UA,
RD_KAFKA_MSG_F_COPY, (void *)msg, len, NULL, 0, local_conf->log);
end:
buf = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
out.buf = buf;
out.next = NULL;
buf->pos = (u_char *)rc;
buf->last = (u_char *)rc + sizeof(rc) - 1;
buf->memory = 1;
buf->last_buf = 1;
ngx_str_set(&(r->headers_out.content_type), "text/html");
r->headers_out.status = NGX_HTTP_OK;
r->headers_out.content_length_n = sizeof(rc) - 1;
ngx_http_send_header(r);
ngx_http_output_filter(r, &out);
ngx_http_finalize_request(r, NGX_OK);
if (main_conf != NULL) {
rd_kafka_poll(main_conf->rk, 0);
}
}
ngx_int_t ngx_http_kafka_init_worker(ngx_cycle_t *cycle)
{
size_t n;
ngx_http_kafka_main_conf_t *main_conf;
main_conf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_kafka_module);
main_conf->rkc = rd_kafka_conf_new();
rd_kafka_conf_set_dr_cb(main_conf->rkc, kafka_callback_handler);
main_conf->rk = rd_kafka_new(RD_KAFKA_PRODUCER, main_conf->rkc, NULL, 0);
for (n = 0; n != main_conf->nbrokers; ++n) {
ngx_str_helper(&main_conf->brokers[n], ngx_str_push);
rd_kafka_brokers_add(main_conf->rk, (const char *)main_conf->brokers[n].data);
ngx_str_helper(&main_conf->brokers[n], ngx_str_pop);
}
return 0;
}
void ngx_http_kafka_exit_worker(ngx_cycle_t *cycle)
{
ngx_http_kafka_main_conf_t *main_conf;
main_conf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_kafka_module);
// TODO: rd_kafka_topic_destroy(each loc conf rkt );
rd_kafka_destroy(main_conf->rk);
}
void ngx_str_helper(ngx_str_t *str, ngx_str_op op)
{
static char backup;
switch (op) {
case ngx_str_push:
backup = str->data[str->len];
str->data[str->len] = 0;
break;
case ngx_str_pop:
str->data[str->len] = backup;
break;
default:
ngx_abort();
}
}