Skip to content

Commit cf8a31d

Browse files
committed
feature: support socket in some block phase
1 parent fdf752d commit cf8a31d

File tree

5 files changed

+291
-5
lines changed

5 files changed

+291
-5
lines changed

Diff for: config

+2
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ HTTP_LUA_SRCS=" \
296296
$ngx_addon_dir/src/ngx_http_lua_log_ringbuf.c \
297297
$ngx_addon_dir/src/ngx_http_lua_input_filters.c \
298298
$ngx_addon_dir/src/ngx_http_lua_pipe.c \
299+
$ngx_addon_dir/src/event/ngx_http_lua_kqueue.c \
299300
"
300301

301302
HTTP_LUA_DEPS=" \
@@ -355,6 +356,7 @@ HTTP_LUA_DEPS=" \
355356
$ngx_addon_dir/src/ngx_http_lua_log_ringbuf.h \
356357
$ngx_addon_dir/src/ngx_http_lua_input_filters.h \
357358
$ngx_addon_dir/src/ngx_http_lua_pipe.h \
359+
$ngx_addon_dir/src/ngx_http_lua_event.h \
358360
"
359361

360362
# ----------------------------------------

Diff for: src/event/ngx_http_lua_kqueue.c

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
2+
/*
3+
* Copyright (C) Yichun Zhang (agentzh)
4+
*/
5+
6+
7+
#include <ngx_core.h>
8+
#include <ngx_event.h>
9+
#include <ngx_http.h>
10+
11+
int ngx_lua_kqueue = -1;
12+
struct kevent change_list[1];
13+
struct kevent event_list[1];
14+
15+
ngx_int_t
16+
ngx_http_lua_kqueue_init(ngx_conf_t *cf)
17+
{
18+
if (ngx_lua_kqueue == -1) {
19+
ngx_lua_kqueue = kqueue();
20+
21+
if (ngx_lua_kqueue == -1) {
22+
ngx_conf_log_error(NGX_LOG_ALERT, cf, 0, "kqueue() failed");
23+
24+
return NGX_ERROR;
25+
}
26+
}
27+
28+
return NGX_OK;
29+
}
30+
31+
32+
void
33+
ngx_http_lua_kqueue_set_event(ngx_event_t *ev, ngx_int_t filter)
34+
{
35+
struct kevent *kev;
36+
ngx_connection_t *c;
37+
38+
c = ev->data;
39+
40+
kev = &change_list[0];
41+
42+
kev->ident = c->fd;
43+
kev->filter = (short) filter;
44+
kev->flags = EV_ADD|EV_ENABLE;
45+
kev->udata = NGX_KQUEUE_UDATA_T ((uintptr_t) ev | ev->instance);
46+
}
47+
48+
49+
ngx_int_t
50+
ngx_http_lua_kqueue_process_events(ngx_http_request_t *r, ngx_msec_t timer)
51+
{
52+
int events;
53+
struct timespec ts;
54+
ngx_event_t *ev;
55+
ngx_int_t instance;
56+
ngx_err_t err;
57+
58+
ts.tv_sec = timer / 1000;
59+
ts.tv_nsec = (timer % 1000) * 1000000;
60+
61+
events = kevent(ngx_lua_kqueue, change_list, 1, event_list, 1, &ts);
62+
63+
err = (events == -1) ? ngx_errno : 0;
64+
65+
if (err) {
66+
ngx_log_error(NGX_LOG_ALERT, r->connection->log, err,
67+
"kevent() failed");
68+
69+
return NGX_ERROR;
70+
}
71+
72+
if (events == 0) {
73+
ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
74+
"kevent() returned no events without timeout");
75+
76+
return NGX_ERROR;
77+
}
78+
79+
ev = (ngx_event_t *) event_list[0].udata;
80+
instance = (uintptr_t) ev & 1;
81+
ev = (ngx_event_t *) ((uintptr_t) ev & (uintptr_t) ~1);
82+
83+
ev->available = event_list[0].data;
84+
ev->ready = 1;
85+
86+
return NGX_OK;
87+
}

Diff for: src/ngx_http_lua_event.h

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
/*
3+
* Copyright (C) Yichun Zhang (agentzh)
4+
*/
5+
6+
7+
#ifndef _NGX_HTTP_LUA_EVENT_H_INCLUDED_
8+
#define _NGX_HTTP_LUA_EVENT_H_INCLUDED_
9+
10+
11+
#include "ngx_http_lua_common.h"
12+
13+
14+
ngx_int_t ngx_http_lua_kqueue_init(ngx_conf_t *cf);
15+
16+
void ngx_http_lua_kqueue_set_event(ngx_event_t *ev, ngx_int_t filter);
17+
18+
ngx_int_t ngx_http_lua_kqueue_process_events(ngx_http_request_t *r,
19+
ngx_msec_t timer);
20+
21+
22+
#endif /* _NGX_HTTP_LUA_EVENT_H_INCLUDED_ */
23+
24+
/* vi:set ft=c ts=4 sw=4 et fdm=marker: */

Diff for: src/ngx_http_lua_module.c

+6
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "ngx_http_lua_ssl_session_fetchby.h"
3232
#include "ngx_http_lua_headers.h"
3333
#include "ngx_http_lua_pipe.h"
34+
#include "ngx_http_lua_event.h"
3435

3536

3637
static void *ngx_http_lua_create_main_conf(ngx_conf_t *cf);
@@ -786,6 +787,11 @@ ngx_http_lua_init(ngx_conf_t *cf)
786787
cln->handler = ngx_http_lua_ngx_raw_header_cleanup;
787788
#endif
788789

790+
rc = ngx_http_lua_kqueue_init(cf);
791+
if (rc == NGX_ERROR) {
792+
return rc;
793+
}
794+
789795
if (lmcf->lua == NULL) {
790796
dd("initializing lua vm");
791797

0 commit comments

Comments
 (0)