Skip to content

Commit 9d3c4ad

Browse files
author
jiahao
committed
wip: try to implement socket in init_by_lua* phase.
1 parent fc3f1bb commit 9d3c4ad

8 files changed

+355
-25
lines changed

Diff for: src/ngx_http_lua_common.h

+1
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ typedef struct {
141141
#define NGX_HTTP_LUA_CONTEXT_SSL_SESS_FETCH 0x1000
142142
#define NGX_HTTP_LUA_CONTEXT_EXIT_WORKER 0x2000
143143
#define NGX_HTTP_LUA_CONTEXT_SSL_CLIENT_HELLO 0x4000
144+
#define NGX_HTTP_LUA_CONTEXT_INIT 0x8000
144145

145146

146147
#define NGX_HTTP_LUA_FFI_NO_REQ_CTX -100

Diff for: src/ngx_http_lua_event.h

+101-8
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,63 @@ extern int ngx_http_lua_event_inited;
3535
static ngx_inline ngx_int_t
3636
ngx_http_lua_init_event(ngx_cycle_t *cycle)
3737
{
38-
void ***ccf;
39-
ngx_event_conf_t *ecf;
38+
ngx_module_t *module;
39+
ngx_module_t **modules;
40+
ngx_event_module_t *event_module;
41+
ngx_uint_t i;
42+
ngx_connection_t *c, *next;
43+
ngx_event_t *rev, *wev;
44+
45+
module = NULL;
46+
47+
#if (nginx_version >= 1009011)
48+
modules = cycle->modules;
49+
#else
50+
modules = ngx_modules;
51+
#endif
52+
53+
for (i = 0; modules[i]; i++) {
54+
if (modules[i]->type != NGX_EVENT_MODULE) {
55+
continue;
56+
}
57+
58+
event_module = modules[i]->ctx;
59+
if (ngx_strcmp(event_module->name->data, "event_core") == 0)
60+
{
61+
continue;
62+
}
4063

41-
ccf = ngx_get_conf(cycle->conf_ctx, ngx_events_module);
42-
if (ccf == NULL) {
64+
module = modules[i];
65+
break;
66+
}
67+
68+
if (module == NULL) {
69+
ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, "no events module found");
4370
return NGX_ERROR;
4471
}
4572

46-
ecf = (*ccf)[ngx_event_core_module.ctx_index];
73+
event_module = module->ctx;
74+
75+
/* FIXME: should init event_module actions here, like:
76+
* ```
77+
* if (event_module->actions.init(cycle, ngx_timer_resolution) != NGX_OK) {
78+
* ```
79+
*
80+
* but `epcf` is not initial here before
81+
* ```
82+
* static ngx_int_t
83+
* ngx_epoll_init(ngx_cycle_t *cycle, ngx_msec_t timer)
84+
* {
85+
* ngx_epoll_conf_t *epcf;
86+
*
87+
* // Segmentation fault below
88+
* epcf = ngx_event_get_conf(cycle->conf_ctx, ngx_epoll_module);
89+
* ```
90+
*/
4791

4892
#if (NGX_HAVE_EPOLL) && !(NGX_TEST_BUILD_EPOLL)
4993

50-
if (ngx_strcmp(ecf->name, "epoll") == 0) {
94+
if (ngx_strcmp(event_module->name->data, "epoll") == 0) {
5195
ngx_http_lua_event_actions = ngx_http_lua_epoll;
5296

5397
} else
@@ -56,7 +100,7 @@ ngx_http_lua_init_event(ngx_cycle_t *cycle)
56100

57101
#if (NGX_HAVE_POLL)
58102

59-
if (ngx_strcmp(ecf->name, "poll") == 0) {
103+
if (ngx_strcmp(event_module->name->data, "poll") == 0) {
60104
ngx_http_lua_event_actions = ngx_http_lua_poll;
61105

62106
} else
@@ -65,7 +109,7 @@ ngx_http_lua_init_event(ngx_cycle_t *cycle)
65109

66110
#if (NGX_HAVE_KQUEUE)
67111

68-
if (ngx_strcmp(ecf->name, "kqueue") == 0) {
112+
if (ngx_strcmp(event_module->name->data, "kqueue") == 0) {
69113
ngx_http_lua_event_actions = ngx_http_lua_kqueue;
70114

71115
} else
@@ -76,6 +120,55 @@ ngx_http_lua_init_event(ngx_cycle_t *cycle)
76120
return NGX_ERROR;
77121
}
78122

123+
cycle->connection_n = 128;
124+
cycle->connections =
125+
ngx_alloc(sizeof(ngx_connection_t) * cycle->connection_n, cycle->log);
126+
if (cycle->connections == NULL) {
127+
return NGX_ERROR;
128+
}
129+
130+
c = cycle->connections;
131+
132+
cycle->read_events = ngx_alloc(sizeof(ngx_event_t) * cycle->connection_n,
133+
cycle->log);
134+
if (cycle->read_events == NULL) {
135+
return NGX_ERROR;
136+
}
137+
138+
rev = cycle->read_events;
139+
for (i = 0; i < cycle->connection_n; i++) {
140+
rev[i].closed = 1;
141+
rev[i].instance = 1;
142+
}
143+
144+
cycle->write_events = ngx_alloc(sizeof(ngx_event_t) * cycle->connection_n,
145+
cycle->log);
146+
if (cycle->write_events == NULL) {
147+
return NGX_ERROR;
148+
}
149+
150+
wev = cycle->write_events;
151+
for (i = 0; i < cycle->connection_n; i++) {
152+
wev[i].closed = 1;
153+
}
154+
155+
i = cycle->connection_n;
156+
next = NULL;
157+
158+
do {
159+
i--;
160+
161+
c[i].data = next;
162+
c[i].read = &cycle->read_events[i];
163+
c[i].write = &cycle->write_events[i];
164+
c[i].fd = (ngx_socket_t) -1;
165+
166+
next = &c[i];
167+
} while (i);
168+
169+
cycle->free_connections = next;
170+
cycle->free_connection_n = cycle->connection_n;
171+
79172
return ngx_http_lua_event_actions.init_event(cycle);
80173
}
81174

Diff for: src/ngx_http_lua_initworkerby.c

-9
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,8 @@
1313
#include "ngx_http_lua_initworkerby.h"
1414
#include "ngx_http_lua_util.h"
1515
#include "ngx_http_lua_pipe.h"
16-
#include "ngx_http_lua_event.h"
1716

1817

19-
int ngx_http_lua_event_inited = 0;
20-
21-
ngx_http_lua_event_actions_t ngx_http_lua_event_actions;
22-
2318
static u_char *ngx_http_lua_log_init_worker_error(ngx_log_t *log,
2419
u_char *buf, size_t len);
2520

@@ -51,10 +46,6 @@ ngx_http_lua_init_worker(ngx_cycle_t *cycle)
5146
return NGX_OK;
5247
}
5348

54-
if (ngx_http_lua_init_event(cycle) == NGX_OK) {
55-
ngx_http_lua_event_inited = 1;
56-
}
57-
5849
/* lmcf != NULL && lmcf->lua != NULL */
5950

6051
#if !(NGX_WIN32)

0 commit comments

Comments
 (0)