Skip to content

Commit 464121e

Browse files
thibaultchadoujiang24
authored andcommitted
bugfix: added the missing 'ngx.req.start_time' to the stream subsystem.
The `ngx.req.start_time()` API is part of `request.lua` which is was understandably not loaded for the stream subsystem. There existed no tests for this API in the stream subsystem, so the removal of its underlying CFunction resulted in `ngx.req.start_time` being `nil` with no test cases to realize it.
1 parent f1b1e76 commit 464121e

File tree

4 files changed

+94
-31
lines changed

4 files changed

+94
-31
lines changed

lib/resty/core.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ require "resty.core.hash"
1212
require "resty.core.uri"
1313
require "resty.core.exit"
1414
require "resty.core.base64"
15+
require "resty.core.request"
1516

1617

1718
if subsystem == 'http' then
18-
require "resty.core.request"
1919
require "resty.core.response"
2020
require "resty.core.phase"
2121
require "resty.core.ndk"

lib/resty/core/request.lua

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33

44
local ffi = require 'ffi'
55
local base = require "resty.core.base"
6-
base.allows_subsystem("http")
76
local utils = require "resty.core.utils"
87

98

9+
local subsystem = ngx.config.subsystem
1010
local FFI_BAD_CONTEXT = base.FFI_BAD_CONTEXT
1111
local FFI_DECLINED = base.FFI_DECLINED
1212
local FFI_OK = base.FFI_OK
@@ -34,6 +34,40 @@ local _M = {
3434
}
3535

3636

37+
local ngx_lua_ffi_req_start_time
38+
39+
40+
if subsystem == "stream" then
41+
ffi.cdef[[
42+
double ngx_stream_lua_ffi_req_start_time(ngx_stream_lua_request_t *r);
43+
]]
44+
45+
ngx_lua_ffi_req_start_time = C.ngx_stream_lua_ffi_req_start_time
46+
47+
elseif subsystem == "http" then
48+
ffi.cdef[[
49+
double ngx_http_lua_ffi_req_start_time(ngx_http_request_t *r);
50+
]]
51+
52+
ngx_lua_ffi_req_start_time = C.ngx_http_lua_ffi_req_start_time
53+
end
54+
55+
56+
function ngx.req.start_time()
57+
local r = get_request()
58+
if not r then
59+
error("no request found")
60+
end
61+
62+
return tonumber(ngx_lua_ffi_req_start_time(r))
63+
end
64+
65+
66+
if subsystem == "stream" then
67+
return _M
68+
end
69+
70+
3771
local errmsg = base.get_errmsg_ptr()
3872
local ffi_str_type = ffi.typeof("ngx_http_lua_ffi_str_t*")
3973
local ffi_str_size = ffi.sizeof("ngx_http_lua_ffi_str_t")
@@ -59,8 +93,6 @@ ffi.cdef[[
5993
int ngx_http_lua_ffi_req_get_uri_args(ngx_http_request_t *r,
6094
unsigned char *buf, ngx_http_lua_ffi_table_elt_t *out, int count);
6195

62-
double ngx_http_lua_ffi_req_start_time(ngx_http_request_t *r);
63-
6496
int ngx_http_lua_ffi_req_get_method(ngx_http_request_t *r);
6597

6698
int ngx_http_lua_ffi_req_get_method_name(ngx_http_request_t *r,
@@ -222,16 +254,6 @@ function ngx.req.get_uri_args(max_args)
222254
end
223255

224256

225-
function ngx.req.start_time()
226-
local r = get_request()
227-
if not r then
228-
error("no request found")
229-
end
230-
231-
return tonumber(C.ngx_http_lua_ffi_req_start_time(r))
232-
end
233-
234-
235257
do
236258
local methods = {
237259
[0x0002] = "GET",

lib/resty/core/utils.lua

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
local ffi = require "ffi"
55
local base = require "resty.core.base"
6-
base.allows_subsystem("http")
76

87

98
local C = ffi.C
@@ -12,32 +11,35 @@ local ffi_copy = ffi.copy
1211
local byte = string.byte
1312
local str_find = string.find
1413
local get_string_buf = base.get_string_buf
15-
16-
17-
ffi.cdef[[
18-
void ngx_http_lua_ffi_str_replace_char(unsigned char *buf, size_t len,
19-
const unsigned char find, const unsigned char replace);
20-
]]
14+
local subsystem = ngx.config.subsystem
2115

2216

2317
local _M = {
2418
version = base.version
2519
}
2620

2721

28-
function _M.str_replace_char(str, find, replace)
29-
if not str_find(str, find, nil, true) then
30-
return str
31-
end
22+
if subsystem == "http" then
23+
ffi.cdef[[
24+
void ngx_http_lua_ffi_str_replace_char(unsigned char *buf, size_t len,
25+
const unsigned char find, const unsigned char replace);
26+
]]
27+
3228

33-
local len = #str
34-
local buf = get_string_buf(len)
35-
ffi_copy(buf, str)
29+
function _M.str_replace_char(str, find, replace)
30+
if not str_find(str, find, nil, true) then
31+
return str
32+
end
3633

37-
C.ngx_http_lua_ffi_str_replace_char(buf, len, byte(find),
38-
byte(replace))
34+
local len = #str
35+
local buf = get_string_buf(len)
36+
ffi_copy(buf, str)
3937

40-
return ffi_str(buf, len)
38+
C.ngx_http_lua_ffi_str_replace_char(buf, len, byte(find),
39+
byte(replace))
40+
41+
return ffi_str(buf, len)
42+
end
4143
end
4244

4345

t/stream/request.t

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# vim:set ft= ts=4 sw=4 et fdm=marker:
2+
use lib '.';
3+
use t::TestCore::Stream;
4+
5+
repeat_each(2);
6+
7+
plan tests => repeat_each() * (blocks() * 6);
8+
9+
no_long_string();
10+
check_accum_error_log();
11+
run_tests();
12+
13+
__DATA__
14+
15+
=== TEST 1: ngx.req.start_time()
16+
--- stream_server_config
17+
content_by_lua_block {
18+
local t
19+
for i = 1, 500 do
20+
t = ngx.req.start_time()
21+
end
22+
ngx.sleep(0.10)
23+
local elapsed = ngx.now() - t
24+
ngx.say(t > 1399867351)
25+
ngx.say(">= 0.099: ", elapsed >= 0.099)
26+
ngx.say("< 0.11: ", elapsed < 0.11)
27+
-- ngx.say(t, " ", elapsed)
28+
}
29+
--- stream_response
30+
true
31+
>= 0.099: true
32+
< 0.11: true
33+
34+
--- error_log eval
35+
qr/\[TRACE\s+\d+ content_by_lua\(nginx\.conf:\d+\):3 loop\]/
36+
--- no_error_log
37+
[error]
38+
bad argument type
39+
stitch

0 commit comments

Comments
 (0)