-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: support socket in some block phase
- Loading branch information
1 parent
fdf752d
commit cf8a31d
Showing
5 changed files
with
291 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.