Skip to content

Wait for fd API #450

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ NGX_ADDON_SRCS="$NGX_ADDON_SRCS \
$ngx_addon_dir/src/ngx_http_lua_config.c \
$ngx_addon_dir/src/ngx_http_lua_worker.c \
$ngx_addon_dir/src/ngx_http_lua_lex.c \
$ngx_addon_dir/src/ngx_http_lua_connection.c \
"

NGX_ADDON_DEPS="$NGX_ADDON_DEPS \
Expand Down Expand Up @@ -405,6 +406,7 @@ NGX_ADDON_DEPS="$NGX_ADDON_DEPS \
$ngx_addon_dir/src/ngx_http_lua_config.h \
$ngx_addon_dir/src/ngx_http_lua_worker.h \
$ngx_addon_dir/src/ngx_http_lua_lex.h \
$ngx_addon_dir/src/ngx_http_lua_connection.h \
"

CFLAGS="$CFLAGS -DNDK_SET_VAR"
Expand Down
353 changes: 353 additions & 0 deletions src/ngx_http_lua_connection.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,353 @@
/*
* Copyright (C) 2014-2015 Daurnimator
*/

/* need to include DDEBUG so that ngx_http_lua_util.h works */
#ifndef DDEBUG
#define DDEBUG 0
#endif
#include "ddebug.h"

#include "ngx_http_lua_connection.h"

#include <math.h> /* HUGE_VAL */
#include <poll.h> /* POLLIN, POLLOUT */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't rely on poll since the underlying events type can be arbitrary. Better use our own constants or borrow nginx's NGX_READ_EVENT and NGX_WRITE_EVENT for this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

most libraries return values POLLIN/POLLOUT. This is for convenience so that you don't need to transfrom from OS standard constants to nginx specific ones.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@daurnimator Use of these constants make the reader think we are actually using poll(). Also please keep in mind we need to support non-POSIX systems where no poll() exists, like Windows.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please keep in mind we need to support non-POSIX systems where no poll() exists, like Windows.

There is a poll on windows. and it uses the same constants: https://msdn.microsoft.com/en-us/library/windows/desktop/ms740094(v=vs.85).aspx

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@daurnimator Are you sure? There's not even a poll.h there on Windows :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@daurnimator I think you've already got my points so I won't repeat them here :)

You code won't even compile on Windows as-is BTW.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, I just looked it up, and NGX_READ_EVENT is a #define to POLLIN on linux anyway.

The problem is that NGX_READ_EVENT is not safe to be used as a bitmask.
e.g. see the select #ifdef:

#define NGX_READ_EVENT     0
#define NGX_WRITE_EVENT    1

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@daurnimator That's why NGINX introduced such indirections in the first place so that nginx's build system can point them to the right things on different platforms :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It sounds like in this case I'll need to invent my own identifiers then? (because there is no NGX_NO_EVENT or NGX_READ_WRITE_EVENT).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@daurnimator Yes, that sounds better to me.


#include <lua.h>
#include <lauxlib.h>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to include these headers here since ngx_http_lua_common.h already has them.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I usually like being explicit here: If you use a declaration in a header; always include that directly (otherwise you get into a mess of implementation specific details).

I can remove it if it's against your style though :(

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@daurnimator I like shorter code so that we can easily override things in 3rd-party or standard headers (if necessary) in a central place.


#include "ngx_core.h"
#include "ngx_alloc.h" /* ngx_alloc, ngx_free */
#include "ngx_http.h" /* ngx_http_request_t, ngx_http_run_posted_requests */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

#include "ngx_http_lua_common.h" /* ngx_http_lua_module, ngx_http_lua_co_ctx_t */

#include "ngx_http_lua_util.h" /* ngx_http_lua_get_lua_vm, ngx_http_lua_run_thread, ngx_http_lua_finalize_request */
#include "ngx_http_lua_contentby.h" /* ngx_http_lua_content_wev_handler */


typedef struct {
ngx_http_request_t *request;
ngx_http_lua_co_ctx_t *co_ctx;
ngx_connection_t *conn;
} ngx_http_lua_udata_t;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ngx_http_lua_udata_t is not very descriptive. Will you be a bit more specific here?



static ngx_int_t
ngx_http_lua_fd_resume_request(ngx_http_request_t *r)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think C function prefixes like ngx_http_lua_conn_ make more sense than ngx_http_lua_fd for the current file name, ngx_http_lua_connection.c.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this once missed the rename spree: will rename to ngx_http_lua_connection_resume_request

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@daurnimator Ah, I prefer something shorter like conn since seeing long prefixes like ngx_http_lua_conneciton_ all over the place would make me crazy ;)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seeing long prefixes like ngx_http_lua_conneciton_ all over the place would make me crazy ;)

All the other functions in this file have ngx_http_lua_connection_. I like it to match the filename. Would you like me to rename the file to ngx_http_lua_conn.c?

Aside: ngx_http_lua really seems like the long prefix here: if you're worried about long prefixes, why not rename to e.g. nhl_ across all files?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@daurnimator I'm fine with the file name change.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@daurnimator Lua module names should be a bit more descriptive. I think ngx.connection is better than ngx.conn given that we already have ngx.semaphore.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@daurnimator I think it's fine to use shorter names for different contexts. Huffman's principle are in play :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@daurnimator For instance, NGINX does not use nginx_http_blah_blah_blah but use ngx_http_blah_blah_blah itself. So we are consistent with NGINX itself here anyway. It's all about balance.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's all about balance.

I don't think I'll rename the file then.
If there's going to be inconsistency; lets just shorten the prefix but leave it in connection.c

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@daurnimator Fine.

{
ngx_http_lua_ctx_t *ctx;
lua_State *vm;
ngx_int_t rc;
if ((ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module)) == NULL) {
return NGX_ERROR;
}
/* restore normal resume handler */
ctx->resume_handler = ngx_http_lua_wev_handler;
/* resume lua thread */
vm = ngx_http_lua_get_lua_vm(r, ctx);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How will you handle repeated event firing when the user code does not completely consume the events under LT (Level-Triggered) mode? You should call ngx_handle_read_event and ngx_handle_write_event at right places I think.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The whole point of this API is that it will only wait for the event. it's up to the libraries to consume the event.

It's cleaned up eventually by calling ngx_del_con in the cleanup handler.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@daurnimator The libraries do not always "consume" the event (like only having 2 bytes to write and only reading 3 bytes while 5 bytes are in the recv buffer), in which case, it is our responsibility to de-activate the event for LT events otherwise we're looping here like a hell and there's no way for the "libraries" to manipulate the events directly since its agnostic.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@daurnimator The ngx_del_con thing is not enough for such purposes since you should re-create and destroy the ngx.connection object upon every event, which is just too expensive, defeating the purpose of this OO API.

lua_pushboolean(ctx->cur_co_ctx->co, 1);
/* resume coroutine */
rc = ngx_http_lua_run_thread(vm, r, ctx, 1 /*nret*/);
switch (rc) {
case NGX_DONE: /* coroutine finished */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style: case statement should align up with switch.

ngx_http_lua_finalize_request(r, NGX_DONE);
/* fall-through */
case NGX_AGAIN: /* coroutine yielded */
return ngx_http_lua_run_posted_threads(r->connection, vm, r, ctx);
default: /* NGX_ERROR: coroutine failed */
if (ctx->entered_content_phase) {
ngx_http_lua_finalize_request(r, rc);
return NGX_DONE;
}
return rc;
}
}


static void ngx_http_lua_fd_rev_handler(ngx_event_t *ev) {
ngx_connection_t *conn;
ngx_http_lua_udata_t *u;
ngx_http_request_t *r;
ngx_http_lua_co_ctx_t *co_ctx;
ngx_http_lua_ctx_t *ctx;

conn = ev->data;
u = conn->data;
r = u->request;
co_ctx = u->co_ctx;

ngx_http_lua_cleanup_pending_operation(co_ctx);
ev = NULL, u = NULL; /* now invalidated */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style: we usually avoid such comma expressions in contexts outside the for (;;) loop in the NGINX world.


if ((ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module)) != NULL) {
/* set current coroutine to the one that had the event */
ctx->cur_co_ctx = co_ctx;
ctx->resume_handler = ngx_http_lua_fd_resume_request;
/* queue/fire off handler */
r->write_event_handler(r);
}
}


static void
ngx_http_lua_fd_cleanup(ngx_http_lua_co_ctx_t *co_ctx)
{
ngx_http_lua_udata_t *u = co_ctx->data;
if (u->conn->data) {
/* remove from mainloop; do not pass CLOSE_SOCKET flag */
ngx_del_conn(u->conn, 0);
u->conn->data = NULL;
}
ngx_free(u);
co_ctx->data = NULL;
}


int
ngx_http_lua_connection_init(ngx_connection_t **p, ngx_socket_t fd, const char **err)
{
ngx_connection_t *conn;
if ((conn = ngx_get_connection(fd, ngx_cycle->log)) == NULL) {
*err = "unable to get nginx connection";
return NGX_ERROR;
}
conn->data = NULL;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style: need a blank line before this line. Please fix other similar places as well.

conn->read->handler = ngx_http_lua_fd_rev_handler;
conn->read->log = conn->log;
conn->write->handler = ngx_http_lua_fd_rev_handler;
conn->write->log = conn->log;
conn->number = ngx_atomic_fetch_add(ngx_connection_counter, 1);
*p = conn;
return NGX_OK;
}


void
ngx_http_lua_connection_release(ngx_connection_t *conn)
{
/* can't use ngx_close_connection here,
as it closes the file descriptor unconditionally */

/* cancel timeout timer */
if (conn->read->timer_set) {
ngx_del_timer(conn->read);
}

if (conn->data) {
/* remove from mainloop; do not pass CLOSE_SOCKET flag */
ngx_del_conn(conn, 0);
}

/* delete any pending but not handled events */
#if defined(nginx_version) && nginx_version >= 1007005
if (conn->read->posted) {
ngx_delete_posted_event(conn->read);
}
if (conn->write->posted) {
ngx_delete_posted_event(conn->write);
}
#else
if (conn->read->prev) {
ngx_delete_posted_event(conn->read);
}
if (conn->write->prev) {
ngx_delete_posted_event(conn->write);
}
#endif

/* mark as non-reusable */
ngx_reusable_connection(conn, 0);

/* invalidate connection object */
conn->fd = -1;
conn->data = NULL;
ngx_free_connection(conn);
}


int
ngx_http_lua_connection_prep(ngx_http_request_t *r, ngx_connection_t *conn,
int poll_mask, ngx_msec_t wait_ms, const char **err)
{
ngx_http_lua_ctx_t *ctx;
ngx_http_lua_co_ctx_t *co_ctx;
ngx_http_lua_udata_t *u;
if ((ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module)) == NULL) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style: we need a blank line before this line. Please also fix other similar places.

*err ="no request ctx found";
return NGX_ERROR;
}
if ((co_ctx = ctx->cur_co_ctx) == NULL) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style: we need a blank line before this line. Please fix other similar places.

*err ="no co ctx found";
return NGX_ERROR;
}
if ((u = ngx_alloc(sizeof(*u), r->connection->log)) == NULL) {
*err ="no memory";
return NGX_ERROR;
}
/* cleanup old events before adding new ones */
ngx_http_lua_cleanup_pending_operation(co_ctx);
if ((poll_mask & POLLIN) && ngx_add_event(conn->read, NGX_READ_EVENT, NGX_LEVEL_EVENT) != NGX_OK) {
ngx_free(u);
*err ="unable to add to nginx main loop";
return NGX_ERROR;
}
if ((poll_mask & POLLOUT) && ngx_add_event(conn->write, NGX_WRITE_EVENT, NGX_LEVEL_EVENT) != NGX_OK) {
if (poll_mask & POLLIN) {
ngx_del_event(conn->read, NGX_READ_EVENT, 0);
}
ngx_free(u);
*err ="unable to add to nginx main loop";
return NGX_ERROR;
}
conn->data = u;
u->request = r;
u->co_ctx = co_ctx;
u->conn = conn;
co_ctx->cleanup = (ngx_http_cleanup_pt)&ngx_http_lua_fd_cleanup;
co_ctx->data = u;

if (wait_ms != (ngx_msec_t)-1) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style: need a space after the type cast expression (...).

ngx_add_timer(conn->read, wait_ms);
}

/* make sure nginx knows what to do next */
if (ctx->entered_content_phase) {
r->write_event_handler = ngx_http_lua_content_wev_handler;
} else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style: need a blank line before this } else { line.

r->write_event_handler = ngx_http_core_run_phases;
}

return NGX_OK;
}


/* Lua C API bindings */

static int
ngx_http_lua_connection_new(lua_State *L)
{
ngx_connection_t **pconn;
ngx_socket_t fd = luaL_checkinteger(L, 1);
const char *errmsg;
luaL_argcheck(L, fd >= 0, 1, "invalid file descriptor");
pconn = lua_newuserdata(L, sizeof(*pconn));
*pconn = NULL;
luaL_getmetatable(L, NGX_HTTP_LUA_CONNECTION_KEY);
lua_setmetatable(L, -2);
if (NGX_ERROR == ngx_http_lua_connection_init(pconn, fd, &errmsg)) {
return luaL_error(L, errmsg);
}
(*pconn)->data = NULL;
return 1;
}


static ngx_connection_t*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style: need a space before *.

ngx_http_lua_connection_check(lua_State *L, int idx)
{
ngx_connection_t **pconn = luaL_checkudata(L, idx, NGX_HTTP_LUA_CONNECTION_KEY);
luaL_argcheck(L, *pconn != NULL, idx, "ngx_connection_t has been freed");
return *pconn;
}


static int
ngx_http_lua_connection_gc(lua_State *L)
{
ngx_connection_t **pconn = luaL_checkudata(L, 1, NGX_HTTP_LUA_CONNECTION_KEY);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should avoid such C99 feature that can upset older compilers. Please fix similar places too. Thank you.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the C99 feature used here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@daurnimator Sorry, it's not really a C standard thing. It's really an NGINX coding style thing. We should use constant expressions in variable initializations.

if (NULL != *pconn) {
ngx_http_lua_connection_release(*pconn);
*pconn = NULL;
}
return 0;
}


static int
ngx_http_lua_connection_tostring(lua_State *L)
{
ngx_connection_t *conn = ngx_http_lua_connection_check(L, 1);
lua_pushfstring(L, "ngx_connection*: %p", conn);
return 1;
}


static int
ngx_http_lua_connection_wait(lua_State *L)
{
ngx_connection_t *conn = ngx_http_lua_connection_check(L, 1);
int poll_mask;
double timeout;
const char *events, *err;
ngx_http_request_t *r;

switch(lua_type(L, 2)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Such tests are a bit wasteful. I suggest we expose specialized wait_read and wait_write methods to save code branches.

case LUA_TSTRING:
events = lua_tostring(L, 2);
poll_mask = 0;
while (*events) {
if (*events == 'r') {
poll_mask |= POLLIN;
} else if (*events == 'w') {
poll_mask |= POLLOUT;
}
events++;
}
break;
case LUA_TNUMBER:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style: need a blank line before each case statement.

poll_mask = lua_tointeger(L, 2);
if (!(poll_mask & ~(POLLIN|POLLOUT))) {
break;
}
/* fall through on invalid poll_mask */
default:
return luaL_argerror(L, 2, "expected bitwise 'or' of POLLIN|POLLOUT or characters from set 'rw'");
}

timeout = luaL_optnumber(L, 3, HUGE_VAL); /* default to infinite timeout */
if (!poll_mask && timeout == HUGE_VAL) {
return luaL_error(L, "must provide a valid events mask or finite timeout");
}
if ((r = ngx_http_lua_get_req(L)) == NULL) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style: please avoid using assignments in if conditions. Please also take care of other similar places.

return luaL_error(L, "no request found");
}

if (NGX_ERROR == ngx_http_lua_connection_prep(r, conn, poll_mask,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style: please put the constant on the right hand side of comparison operators. (I do understand the rationale before this style but 1) modern C compilers already emit warnings for such misuses and 2) we should follow nginx's coding style).

(timeout < 0)? 0 : (timeout == HUGE_VAL)? ((ngx_msec_t)-1) : timeout*1000, &err)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style: { should be in its own line when the if condition has been splitted into multiple lines. Please fix other similar places (if any).

return luaL_error(L, err);
}

return lua_yield(L, 0);
}


void
ngx_http_lua_inject_connection(lua_State *L)
{
luaL_newmetatable(L, NGX_HTTP_LUA_CONNECTION_KEY);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest hooking the metatable onto the ngx.connection module table. So the user can speed up method invocations on hot code paths by writing something like this:

local ngx_connection = require "ngx.connection"
local wait_read = ngx_connection.wait_read
...
-- on hot code paths:
local ok, err = wait_read(c)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It already is... see the following ~10 lines.

lua_pushcfunction(L, ngx_http_lua_connection_gc);
lua_setfield(L, -2, "__gc");
lua_pushcfunction(L, ngx_http_lua_connection_tostring);
lua_setfield(L, -2, "__tostring");
lua_newtable(L);
lua_pushcfunction(L, ngx_http_lua_connection_wait);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lua_CFunction cannot be JIT compiled. I suggest we expose pure C API in the ngx_lua core and do the Lua wrapper API via FFI in lua-resty-core instead. I don't want to support the standard Lua interpreter for these new APIs.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please take a look at how the ngx.semaphore API does this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have made the functions available for ffi. Notice all the functions that were not declared static (are are in headers)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@daurnimator I don't want to keep compatibility with the standard Lua interpreter for new nontrivial APIs now since it's quite a maintenance burden.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need them myself.... e.g. I debug using standard lua; then once it works there, I go debug with luajit.
Please reconsider this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@daurnimator That's a reasonable approach but unfortunately we are accumulating features exclusive for LuaJIT.

lua_setfield(L, -2, "wait");
lua_pushcfunction(L, ngx_http_lua_connection_gc);
lua_setfield(L, -2, "free");
lua_setfield(L, -2, "__index");
lua_pop(L, 1);

lua_newtable(L);
lua_pushcfunction(L, ngx_http_lua_connection_new);
lua_setfield(L, -2, "new");
lua_pushcfunction(L, ngx_http_lua_connection_wait);
lua_setfield(L, -2, "wait");
lua_pushcfunction(L, ngx_http_lua_connection_gc);
lua_setfield(L, -2, "free");
lua_pushinteger(L, POLLIN);
lua_setfield(L, -2, "POLLIN");
lua_pushinteger(L, POLLOUT);
lua_setfield(L, -2, "POLLOUT");

lua_setfield(L, -2, "connection");
}
20 changes: 20 additions & 0 deletions src/ngx_http_lua_connection.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (C) 2014 Daurnimator
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@daurnimator Will you please state that "I hereby assign copyright in this code to the lua-nginx-module project, to be licensed under the same terms as the rest of the code"? This is just for legal resolution. Thanks for your understanding.

*/

#ifndef _NGX_HTTP_LUA_CONNECTION_H_INCLUDED_
#define _NGX_HTTP_LUA_CONNECTION_H_INCLUDED_

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style: need 2 blank lines here. Please fix other similar places. Thank you.

#include <lua.h>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, no need to explicitly include lua.h here.

#include "ngx_http_lua_common.h"

#define NGX_HTTP_LUA_CONNECTION_KEY "ngx_connection_t*"

int ngx_http_lua_connection_init(ngx_connection_t **p, ngx_socket_t fd, const char **err);
void ngx_http_lua_connection_release(ngx_connection_t *conn);
int ngx_http_lua_connection_prep(ngx_http_request_t *r, ngx_connection_t *conn,
int poll_mask, ngx_msec_t wait_ms, const char **err);
void ngx_http_lua_inject_connection(lua_State *L);


#endif /* _NGX_HTTP_LUA_CONNECTION_H_INCLUDED_ */
Loading