Skip to content

Commit 13fd856

Browse files
committed
feature: enabled the FFI-based API for all 'ngx.worker.*' APIs in the stream subsystem.
1 parent 67ac13b commit 13fd856

File tree

2 files changed

+39
-12
lines changed

2 files changed

+39
-12
lines changed

lib/resty/core.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ local subsystem = ngx.config.subsystem
55

66
require "resty.core.ctx"
77
require "resty.core.var"
8+
require "resty.core.worker"
89
require "resty.core.regex"
910
require "resty.core.shdict"
1011
require "resty.core.time"
@@ -18,7 +19,6 @@ require "resty.core.base64"
1819
if subsystem == 'http' then
1920
require "resty.core.request"
2021
require "resty.core.response"
21-
require "resty.core.worker"
2222
require "resty.core.phase"
2323
require "resty.core.ndk"
2424
end

lib/resty/core/worker.lua

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,64 @@
11
-- Copyright (C) Yichun Zhang (agentzh)
22

33

4-
local ffi = require 'ffi'
4+
local ffi = require "ffi"
55
local base = require "resty.core.base"
66

77

88
local C = ffi.C
99
local new_tab = base.new_tab
10+
local subsystem = ngx.config.subsystem
11+
12+
13+
local ngx_lua_ffi_worker_id
14+
local ngx_lua_ffi_worker_pid
15+
local ngx_lua_ffi_worker_count
16+
local ngx_lua_ffi_worker_exiting
1017

1118

1219
ngx.worker = new_tab(0, 4)
1320

1421

15-
ffi.cdef[[
16-
int ngx_http_lua_ffi_worker_pid(void);
17-
int ngx_http_lua_ffi_worker_exiting(void);
18-
int ngx_http_lua_ffi_worker_id(void);
19-
int ngx_http_lua_ffi_worker_count(void);
20-
]]
22+
if subsystem == "http" then
23+
ffi.cdef[[
24+
int ngx_http_lua_ffi_worker_id(void);
25+
int ngx_http_lua_ffi_worker_pid(void);
26+
int ngx_http_lua_ffi_worker_count(void);
27+
int ngx_http_lua_ffi_worker_exiting(void);
28+
]]
29+
30+
ngx_lua_ffi_worker_id = C.ngx_http_lua_ffi_worker_id
31+
ngx_lua_ffi_worker_pid = C.ngx_http_lua_ffi_worker_pid
32+
ngx_lua_ffi_worker_count = C.ngx_http_lua_ffi_worker_count
33+
ngx_lua_ffi_worker_exiting = C.ngx_http_lua_ffi_worker_exiting
34+
35+
elseif subsystem == "stream" then
36+
ffi.cdef[[
37+
int ngx_stream_lua_ffi_worker_id(void);
38+
int ngx_stream_lua_ffi_worker_pid(void);
39+
int ngx_stream_lua_ffi_worker_count(void);
40+
int ngx_stream_lua_ffi_worker_exiting(void);
41+
]]
42+
43+
ngx_lua_ffi_worker_id = C.ngx_stream_lua_ffi_worker_id
44+
ngx_lua_ffi_worker_pid = C.ngx_stream_lua_ffi_worker_pid
45+
ngx_lua_ffi_worker_count = C.ngx_stream_lua_ffi_worker_count
46+
ngx_lua_ffi_worker_exiting = C.ngx_stream_lua_ffi_worker_exiting
47+
end
2148

2249

2350
function ngx.worker.exiting()
24-
return C.ngx_http_lua_ffi_worker_exiting() ~= 0
51+
return ngx_lua_ffi_worker_exiting() ~= 0
2552
end
2653

2754

2855
function ngx.worker.pid()
29-
return C.ngx_http_lua_ffi_worker_pid()
56+
return ngx_lua_ffi_worker_pid()
3057
end
3158

3259

3360
function ngx.worker.id()
34-
local id = C.ngx_http_lua_ffi_worker_id()
61+
local id = ngx_lua_ffi_worker_id()
3562
if id < 0 then
3663
return nil
3764
end
@@ -41,7 +68,7 @@ end
4168

4269

4370
function ngx.worker.count()
44-
return C.ngx_http_lua_ffi_worker_count()
71+
return ngx_lua_ffi_worker_count()
4572
end
4673

4774

0 commit comments

Comments
 (0)