Skip to content

Commit dc04c71

Browse files
authored
fix: incorrect upstream_response_time when nginx returns 499 (#11)
fix #10
1 parent 757f6d7 commit dc04c71

File tree

2 files changed

+47
-12
lines changed

2 files changed

+47
-12
lines changed

src/ngx_http_lua_var_module.c

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -111,22 +111,19 @@ ngx_http_lua_var_ffi_upstream_response_time(ngx_http_request_t *r,
111111
state = r->upstream_states->elts;
112112

113113
for ( ;; ) {
114-
if (state[i].status) {
114+
if (type == 1) {
115+
ms = state[i].header_time;
115116

116-
if (type == 1 && state[i].header_time != (ngx_msec_t) -1) {
117-
ms = state[i].header_time;
117+
} else if (type == 2) {
118+
ms = state[i].connect_time;
118119

119-
} else if (type == 2 && state[i].connect_time != (ngx_msec_t) -1) {
120-
ms = state[i].connect_time;
121-
122-
} else {
123-
ms = state[i].response_time;
124-
}
125-
126-
ms = ngx_max(ms, 0);
127-
total_ms = total_ms + ms;
120+
} else {
121+
ms = state[i].response_time;
128122
}
129123

124+
ms = ngx_max(ms, 0);
125+
total_ms = total_ms + ms;
126+
130127
if (++i == r->upstream_states->nelts) {
131128
break;
132129
}

t/upstream_response_time_bugfix.t

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# vim:set ft= ts=4 sw=4 et fdm=marker:
2+
3+
use Test::Nginx::Socket::Lua 'no_plan';
4+
5+
repeat_each(1);
6+
7+
#no_shuffle();
8+
no_long_string();
9+
run_tests();
10+
11+
__DATA__
12+
13+
=== TEST 1: ignore the client abort event in the user callback
14+
--- config
15+
location /t {
16+
proxy_pass http://127.0.0.1:$server_port/delay;
17+
log_by_lua_block {
18+
local var = require("resty.ngxvar")
19+
local req = var.request()
20+
ngx.log(ngx.NOTICE, "validate upstream_response_time: ", tonumber(var.fetch("upstream_response_time", req)) > 0)
21+
}
22+
}
23+
location = /delay {
24+
content_by_lua_block {
25+
ngx.sleep(2)
26+
}
27+
}
28+
--- request
29+
GET /t
30+
31+
--- timeout: 0.2
32+
--- abort
33+
--- wait: 0.7
34+
--- ignore_response
35+
--- no_error_log
36+
[error]
37+
--- error_log
38+
validate upstream_response_time: true

0 commit comments

Comments
 (0)