Skip to content

Commit e71133a

Browse files
optimize: reimplemented the coroutine wrapper using the ffi api. (openresty#383)
1 parent cb07581 commit e71133a

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ services:
5353

5454
before_install:
5555
- sudo luarocks install luacheck $LUACHECK_VER
56-
- luacheck -q .
56+
- luacheck --globals coroutine -q .
5757
- '! grep -n -P ''(?<=.{80}).+'' --color `find . -name ''*.lua''` || (echo "ERROR: Found Lua source lines exceeding 80 columns." > /dev/stderr; exit 1)'
5858
- '! grep -n -P ''\t+'' --color `find . -name ''*.lua''` || (echo "ERROR: Cannot use tabs." > /dev/stderr; exit 1)'
5959
- sudo cpanm --notest Test::Nginx IPC::Run > build.log 2>&1 || (cat build.log && exit 1)

lib/resty/core.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ if subsystem == 'http' then
2020
require "resty.core.phase"
2121
require "resty.core.ndk"
2222
require "resty.core.socket"
23+
require "resty.core.coroutine"
2324
end
2425

2526

lib/resty/core/coroutine.lua

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
local base = require "resty.core.base"
2+
local get_request = base.get_request
3+
4+
do
5+
local keys = {'create', 'yield', 'resume', 'status', 'wrap'}
6+
local errmsg = base.get_errmsg_ptr()
7+
local get_raw_phase = ngx.get_raw_phase
8+
9+
for _, key in ipairs(keys) do
10+
local std = coroutine['_' .. key]
11+
local ours = coroutine['__' .. key]
12+
coroutine[key] = function (...)
13+
local r = get_request()
14+
if r ~= nil then
15+
local ctx = get_raw_phase(r, errmsg)
16+
if ctx ~= 0x020 and ctx ~= 0x040 then
17+
return ours(...)
18+
end
19+
end
20+
return std(...)
21+
end
22+
end
23+
24+
package.loaded.coroutine = coroutine
25+
end
26+
27+
return {
28+
version = base.version
29+
}

lib/resty/core/phase.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ function ngx.get_phase()
5555
end
5656

5757

58+
function ngx.get_raw_phase(r)
59+
local context = C.ngx_http_lua_ffi_get_phase(r, errmsg)
60+
if context == FFI_ERROR then -- NGX_ERROR
61+
error(errmsg, 2)
62+
end
63+
64+
return context
65+
end
66+
67+
5868
return {
5969
version = base.version
6070
}

0 commit comments

Comments
 (0)