|
1 | 1 | -- Copyright (C) Yichun Zhang (agentzh) |
2 | 2 |
|
3 | 3 |
|
4 | | -local ffi = require 'ffi' |
| 4 | +local ffi = require "ffi" |
5 | 5 | local base = require "resty.core.base" |
6 | 6 |
|
7 | 7 |
|
8 | 8 | local C = ffi.C |
9 | 9 | 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 |
10 | 17 |
|
11 | 18 |
|
12 | 19 | ngx.worker = new_tab(0, 4) |
13 | 20 |
|
14 | 21 |
|
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 |
21 | 48 |
|
22 | 49 |
|
23 | 50 | function ngx.worker.exiting() |
24 | | - return C.ngx_http_lua_ffi_worker_exiting() ~= 0 |
| 51 | + return ngx_lua_ffi_worker_exiting() ~= 0 |
25 | 52 | end |
26 | 53 |
|
27 | 54 |
|
28 | 55 | function ngx.worker.pid() |
29 | | - return C.ngx_http_lua_ffi_worker_pid() |
| 56 | + return ngx_lua_ffi_worker_pid() |
30 | 57 | end |
31 | 58 |
|
32 | 59 |
|
33 | 60 | function ngx.worker.id() |
34 | | - local id = C.ngx_http_lua_ffi_worker_id() |
| 61 | + local id = ngx_lua_ffi_worker_id() |
35 | 62 | if id < 0 then |
36 | 63 | return nil |
37 | 64 | end |
|
41 | 68 |
|
42 | 69 |
|
43 | 70 | function ngx.worker.count() |
44 | | - return C.ngx_http_lua_ffi_worker_count() |
| 71 | + return ngx_lua_ffi_worker_count() |
45 | 72 | end |
46 | 73 |
|
47 | 74 |
|
|
0 commit comments