Skip to content

Commit

Permalink
feature: support socket in some block phase
Browse files Browse the repository at this point in the history
  • Loading branch information
rainingmaster committed Nov 9, 2020
1 parent fdf752d commit cf8a31d
Show file tree
Hide file tree
Showing 5 changed files with 291 additions and 5 deletions.
2 changes: 2 additions & 0 deletions config
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ HTTP_LUA_SRCS=" \
$ngx_addon_dir/src/ngx_http_lua_log_ringbuf.c \
$ngx_addon_dir/src/ngx_http_lua_input_filters.c \
$ngx_addon_dir/src/ngx_http_lua_pipe.c \
$ngx_addon_dir/src/event/ngx_http_lua_kqueue.c \
"

HTTP_LUA_DEPS=" \
Expand Down Expand Up @@ -355,6 +356,7 @@ HTTP_LUA_DEPS=" \
$ngx_addon_dir/src/ngx_http_lua_log_ringbuf.h \
$ngx_addon_dir/src/ngx_http_lua_input_filters.h \
$ngx_addon_dir/src/ngx_http_lua_pipe.h \
$ngx_addon_dir/src/ngx_http_lua_event.h \
"

# ----------------------------------------
Expand Down
87 changes: 87 additions & 0 deletions src/event/ngx_http_lua_kqueue.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@

/*
* Copyright (C) Yichun Zhang (agentzh)
*/


#include <ngx_core.h>
#include <ngx_event.h>
#include <ngx_http.h>

int ngx_lua_kqueue = -1;
struct kevent change_list[1];
struct kevent event_list[1];

ngx_int_t
ngx_http_lua_kqueue_init(ngx_conf_t *cf)
{
if (ngx_lua_kqueue == -1) {
ngx_lua_kqueue = kqueue();

if (ngx_lua_kqueue == -1) {
ngx_conf_log_error(NGX_LOG_ALERT, cf, 0, "kqueue() failed");

return NGX_ERROR;
}
}

return NGX_OK;
}


void
ngx_http_lua_kqueue_set_event(ngx_event_t *ev, ngx_int_t filter)
{
struct kevent *kev;
ngx_connection_t *c;

c = ev->data;

kev = &change_list[0];

kev->ident = c->fd;
kev->filter = (short) filter;
kev->flags = EV_ADD|EV_ENABLE;
kev->udata = NGX_KQUEUE_UDATA_T ((uintptr_t) ev | ev->instance);
}


ngx_int_t
ngx_http_lua_kqueue_process_events(ngx_http_request_t *r, ngx_msec_t timer)
{
int events;
struct timespec ts;
ngx_event_t *ev;
ngx_int_t instance;
ngx_err_t err;

ts.tv_sec = timer / 1000;
ts.tv_nsec = (timer % 1000) * 1000000;

events = kevent(ngx_lua_kqueue, change_list, 1, event_list, 1, &ts);

err = (events == -1) ? ngx_errno : 0;

if (err) {
ngx_log_error(NGX_LOG_ALERT, r->connection->log, err,
"kevent() failed");

return NGX_ERROR;
}

if (events == 0) {
ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
"kevent() returned no events without timeout");

return NGX_ERROR;
}

ev = (ngx_event_t *) event_list[0].udata;
instance = (uintptr_t) ev & 1;
ev = (ngx_event_t *) ((uintptr_t) ev & (uintptr_t) ~1);

ev->available = event_list[0].data;
ev->ready = 1;

return NGX_OK;
}
24 changes: 24 additions & 0 deletions src/ngx_http_lua_event.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

/*
* Copyright (C) Yichun Zhang (agentzh)
*/


#ifndef _NGX_HTTP_LUA_EVENT_H_INCLUDED_
#define _NGX_HTTP_LUA_EVENT_H_INCLUDED_


#include "ngx_http_lua_common.h"


ngx_int_t ngx_http_lua_kqueue_init(ngx_conf_t *cf);

void ngx_http_lua_kqueue_set_event(ngx_event_t *ev, ngx_int_t filter);

ngx_int_t ngx_http_lua_kqueue_process_events(ngx_http_request_t *r,
ngx_msec_t timer);


#endif /* _NGX_HTTP_LUA_EVENT_H_INCLUDED_ */

/* vi:set ft=c ts=4 sw=4 et fdm=marker: */
6 changes: 6 additions & 0 deletions src/ngx_http_lua_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "ngx_http_lua_ssl_session_fetchby.h"
#include "ngx_http_lua_headers.h"
#include "ngx_http_lua_pipe.h"
#include "ngx_http_lua_event.h"


static void *ngx_http_lua_create_main_conf(ngx_conf_t *cf);
Expand Down Expand Up @@ -786,6 +787,11 @@ ngx_http_lua_init(ngx_conf_t *cf)
cln->handler = ngx_http_lua_ngx_raw_header_cleanup;
#endif

rc = ngx_http_lua_kqueue_init(cf);
if (rc == NGX_ERROR) {
return rc;
}

if (lmcf->lua == NULL) {
dd("initializing lua vm");

Expand Down
Loading

0 comments on commit cf8a31d

Please sign in to comment.