summaryrefslogtreecommitdiffstats
path: root/modules/serve_stale/serve_stale.lua
blob: cf19be1731155fe9182b2a091cf2db85a9b289a8 (plain)
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
local M = {} -- the module

local ffi = require('ffi')

-- Beware that the timeout is only considered at certain points in time;
-- approximately at multiples of KR_CONN_RTT_MAX.
M.timeout = 3*sec

M.callback = ffi.cast("kr_stale_cb",
	function (ttl) --, name, type, qry)
		--log('[     ][stal]   => called back with TTL: ' .. tostring(ttl))
		if ttl + 3600 * 24 > 0 then -- at most one day stale
			return 1
		else
			return -1
		end
	end)

M.layer = {
	produce = function (state, req)
		req = kres.request_t(req)
		local qry = req:current()
		-- Don't do anything for priming, prefetching, etc.
		-- TODO: not all cases detected ATM.
		if qry.flags.NO_CACHE then return state end

		local now = ffi.C.kr_now()
		local deadline = qry.creation_time_mono + M.timeout
		if now > deadline or qry.flags.NO_NS_FOUND then
			if verbose() then
				log('[     ][stal]   => no reachable NS, using stale data')
			end
			qry.stale_cb = M.callback
			-- TODO: probably start the same request that doesn't stale-serve,
			-- but first we need some detection of non-interactive / internal requests.
			-- resolve(kres.dname2str(qry.sname), qry.stype, qry.sclass)
		end

		return state
	end,
}

return M