-
Notifications
You must be signed in to change notification settings - Fork 1
/
response_time.lua
60 lines (46 loc) · 1.18 KB
/
response_time.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
local sformat = string.format
local function _set_header(options)
local digits = options.digits or 3
local header = options.header or 'X-Response-Time'
local suffix = options.suffix or false
return function(req, res, time)
if ngx.req.get_headers()[header] then
return
end
local val = sformat("%.".. digits .. "f", time)
if suffix then
val = val .. " ms"
end
res:set_header(header, val)
end
end
local function _wrap(prev_send, res, fn)
local invoked = false
return function(self, content)
if not invoked then
invoked = true
fn()
end
prev_send(res, content)
end
end
local function _inject(res, fn)
res._send = _wrap(res._send, res, fn)
end
return function(options)
local opts = options or {}
local fn
if type(opts) ~= 'function' then
fn = _set_header(opts)
else
fn = opts
end
return function(req, res, next)
local start = ngx.now()
_inject(res, function()
local diff = ngx.now()*1000 - start*1000
fn(req, res, diff)
end)
next()
end
end