Skip to content

Commit 2bc138e

Browse files
authored
optimize: small optimization of ngx.worker.pids() .
1 parent f75eec9 commit 2bc138e

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

lib/resty/core/worker.lua

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ end
7474

7575
if is_not_windows then
7676
if subsystem == "http" then
77+
require "resty.core.phase" -- for ngx.get_phase
78+
7779
ffi.cdef[[
7880
int ngx_http_lua_ffi_worker_pids(int *pids, size_t *pids_len);
7981
]]
@@ -88,16 +90,18 @@ if is_not_windows then
8890
ngx_lua_ffi_worker_pids = C.ngx_stream_lua_ffi_worker_pids
8991
end
9092

93+
local ngx_phase = ngx.get_phase
9194

9295
function ngx.worker.pids()
93-
if ngx.get_phase() == "init" or ngx.get_phase() == "init_worker" then
96+
local phase = ngx_phase()
97+
if phase == "init" or phase == "init_worker" then
9498
return nil, "API disabled in the current context"
9599
end
96100

97101
local pids = {}
98102
local size_ptr = get_size_ptr()
99103
-- the old and the new workers coexist during reloading
100-
local worker_cnt = ngx.worker.count() * 4
104+
local worker_cnt = ngx_lua_ffi_worker_count() * 4
101105
if worker_cnt == 0 then
102106
return pids
103107
end
@@ -117,6 +121,7 @@ if is_not_windows then
117121
end
118122
end
119123

124+
120125
function ngx.worker.id()
121126
local id = ngx_lua_ffi_worker_id()
122127
if id < 0 then

0 commit comments

Comments
 (0)