-
Notifications
You must be signed in to change notification settings - Fork 8
/
swr.lua
68 lines (58 loc) · 1.75 KB
/
swr.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
61
62
63
64
65
66
67
68
-- rough example showing how we can do Stale-While-Revalidate
-- https://tools.ietf.org/html/rfc5861
function async()
ts.debug("async")
local url = ts.ctx['url'] or ''
-- add extra query parameter to async request
url = url .. '?async=yes'
local ct = {
header = ts.ctx['headers']
}
local res = ts.fetch(url, ct)
if res.status == 200 then
ts.debug('pushing')
local purl = ts.ctx['url']
local presp = 'HTTP/1.0 200 OK\r\n'
local header = res.header
for k, v in pairs( header) do
presp = presp.. k .. ': ' .. v .. '\r\n'
end
presp = presp .. '\r\n' .. res.body
local phdr = {}
for k, v in pairs(ts.ctx['headers']) do
phdr[k] = v
end
phdr['Content-Length'] = string.format('%d', string.len(presp))
local pct = {
header = phdr,
method = 'PUSH',
body = presp
}
local pres = ts.fetch(purl, pct)
end
end
function cache_lookup()
ts.debug('cache-lookup')
local inner = ts.http.is_internal_request()
if inner ~= 0 then
-- always make internal requests to be a cache miss so we retrive from origin
ts.debug('internal')
ts.http.set_cache_lookup_status(TS_LUA_CACHE_LOOKUP_MISS)
else
-- mark stale hit as fresh hit and do an async request
ts.debug('external')
local cache_status = ts.http.get_cache_lookup_status()
if cache_status == TS_LUA_CACHE_LOOKUP_HIT_STALE then
ts.debug('stale hit')
ts.http.set_cache_lookup_status(TS_LUA_CACHE_LOOKUP_HIT_FRESH)
ts.schedule(TS_LUA_THREAD_POOL_NET, 0, async)
end
end
return 0
end
function do_global_read_request()
ts.ctx['url'] = ts.client_request.get_url()
ts.ctx['headers'] = ts.client_request.get_headers()
ts.hook(TS_LUA_HOOK_CACHE_LOOKUP_COMPLETE, cache_lookup)
return 0
end