Skip to content

Commit 700e962

Browse files
committed
fd module: Add support for 'event type' argument
the event string may contain "r" and/or "w"
1 parent 34db43f commit 700e962

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

src/ngx_http_lua_fd.c

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include <math.h> /* HUGE_VAL */
1414
#include <stdlib.h> /* calloc, free */
15+
#include <poll.h> /* POLLIN, POLLOUT */
1516

1617
#include <lua.h>
1718
#include <lauxlib.h>
@@ -100,14 +101,23 @@ static void ngx_http_lua_fd_cleanup(ngx_http_lua_co_ctx_t *co_ctx) {
100101
#endif
101102
#if defined(nginx_version) && nginx_version >= 1007005
102103
if (u->conn->read->posted) {
104+
ngx_delete_posted_event(u->conn->read);
105+
}
106+
if (u->conn->write->posted) {
107+
ngx_delete_posted_event(u->conn->write);
108+
}
103109
#else
104110
if (u->conn->read->prev) {
105-
#endif
106111
ngx_delete_posted_event(u->conn->read);
107112
}
113+
if (u->conn->write->posted) {
114+
ngx_delete_posted_event(u->conn->write);
115+
}
116+
#endif
108117
#if (NGX_THREADS)
109118
ngx_unlock(&u->conn->lock);
110119
u->conn->read->locked = 0;
120+
u->conn->write->locked = 0;
111121

112122
ngx_mutex_unlock(ngx_posted_events_mutex);
113123
#endif
@@ -132,8 +142,17 @@ static int ngx_http_lua_fd_wait(lua_State *L) {
132142
ngx_http_lua_udata_t *u;
133143

134144
ngx_socket_t fd = luaL_optint(L, 1, -1); /* -1 is invalid fd */
135-
double timeout = luaL_optnumber(L, 2, HUGE_VAL); /* default to infinite timeout */
136-
if (fd < 0 && timeout == HUGE_VAL) {
145+
const char *events = luaL_optstring(L, 2, "");
146+
int poll_mask = 0;
147+
double timeout = luaL_optnumber(L, 3, HUGE_VAL); /* default to infinite timeout */
148+
while (*events) {
149+
if (*events == 'r')
150+
poll_mask |= POLLIN;
151+
else if (*events == 'w')
152+
poll_mask |= POLLOUT;
153+
events++;
154+
}
155+
if ((fd < 0 || !poll_mask) && timeout == HUGE_VAL) {
137156
return luaL_error(L, "must provide a valid file descriptor or timeout");
138157
}
139158
if ((r = ngx_http_lua_get_req(L)) == NULL) {
@@ -158,10 +177,18 @@ static int ngx_http_lua_fd_wait(lua_State *L) {
158177
u->conn->data = u;
159178
u->conn->read->handler = ngx_http_lua_fd_rev_handler;
160179
u->conn->read->log = u->conn->log;
180+
u->conn->write->handler = ngx_http_lua_fd_rev_handler;
181+
u->conn->write->log = u->conn->log;
161182
u->conn->number = ngx_atomic_fetch_add(ngx_connection_counter, 1);
162183

163184
if (fd >= 0) {
164-
if (ngx_handle_read_event(u->conn->read, NGX_LEVEL_EVENT) != NGX_OK) {
185+
if ((poll_mask & POLLIN) && ngx_handle_read_event(u->conn->read, NGX_LEVEL_EVENT) != NGX_OK) {
186+
ngx_free_connection(u->conn);
187+
free(u);
188+
return luaL_error(L, "unable to add to nginx main loop");
189+
}
190+
if ((poll_mask & POLLOUT) && ngx_handle_write_event(u->conn->write, NGX_LEVEL_EVENT) != NGX_OK) {
191+
if (poll_mask & POLLIN) ngx_del_event(u->conn->read, NGX_READ_EVENT, 0);
165192
ngx_free_connection(u->conn);
166193
free(u);
167194
return luaL_error(L, "unable to add to nginx main loop");

0 commit comments

Comments
 (0)