Skip to content

Commit 86ab22f

Browse files
committed
add header cache, restructure worker
1 parent 9f4c411 commit 86ab22f

11 files changed

+183
-89
lines changed

conf/dyn.conf

100644100755
File mode changed.

conf/postgres.conf

+25-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
user root;
22
worker_processes 2;
33
daemon off;
4-
error_log stderr info;
4+
error_log stderr warn;
55

66
pid /var/run/nginx.pid;
77

@@ -24,7 +24,14 @@ http {
2424
lua_shared_dict settings 250m;
2525
lua_shared_dict cached 100m;
2626
lua_shared_dict locked 100k;
27-
27+
28+
proxy_cache pcache;
29+
proxy_cache_path /data/proxy_cache levels=1:2 keys_zone=pcache:10m inactive=1d max_size=10m use_temp_path=off inactive=180m;
30+
proxy_cache_lock on;
31+
proxy_cache_min_uses 1;
32+
proxy_cache_revalidate on;
33+
proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
34+
2835
init_by_lua_block {
2936
local config = require "cdn.config"
3037
config:set("log.status", true)
@@ -56,14 +63,20 @@ http {
5663
set $ups "";
5764
location / {
5865
proxy_pass http://$ups;
66+
add_header X-Cache "$upstream_cache_status from DYN";
5967
access_by_lua_block {
6068
require "cdn.access"
6169
}
6270

6371
rewrite_by_lua_block {
6472
local worker = require "cdn.worker"
65-
worker:rewrite()
66-
}
73+
worker.rewrite()
74+
}
75+
76+
header_filter_by_lua_block {
77+
local header = require "cdn.header"
78+
header:bootstrap()
79+
}
6780
}
6881

6982
location @dyup_init {
@@ -95,6 +108,14 @@ http {
95108
ngx.say(t:domain("aasdf.asdfsd.com.cn"))
96109
}
97110
}
111+
112+
location /t3 {
113+
content_by_lua_block {
114+
local config = require "cdn.config"
115+
local cjson = require "cjson"
116+
ngx.say(cjson.encode(config:get("hostcfg")))
117+
}
118+
}
98119

99120
location /loadall {
100121
content_by_lua_block {

lua-nginx-module

lualib/cdn/access.lua

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
local cjson = require "cjson"
22
local cookie = require "cdn.cookie"
3+
local config = require "cdn.config"
4+
local log = require "cdn.log"
35

46
local tostring, ipairs, pairs, type, tonumber, next, unpack =
57
tostring, ipairs, pairs, type, tonumber, next, unpack
68

7-
local ngx_log = ngx.log
89
local ngx_var = ngx.var
910
local ngx_ctx = ngx.ctx
1011
local ngx_re_find = ngx.re.find
11-
local ngx_DEBUG = ngx.DEBUG
12-
local ngx_ERR = ngx.ERR
13-
local ngx_INFO = ngx.INFO
1412
local ngx_now = ngx.now
1513
local ngx_exit = ngx.exit
1614
local ngx_md5 = ngx.md5
@@ -83,7 +81,7 @@ if not hd_count then
8381
end
8482

8583

86-
ngx_log(ngx_INFO, "ip total: ", req_iplist:get(ip_key), "(", ip_interval, "s)",
84+
log:info("ip total: ", req_iplist:get(ip_key), "(", ip_interval, "s)",
8785
"req total: ", req_metrics:get(req_key), "(", req_interval, "s)",
8886
"http header total: ", req_metrics:get(hd_key), "(", hd_interval, "s)", ngx_time()
8987
)

lualib/cdn/config.lua

-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
local ngx = ngx
2-
local tostring, ipairs, pairs, type, tonumber, next, unpack =
3-
tostring, ipairs, pairs, type, tonumber, next, unpack
42

53
local _M = {
64
_VERSION = '0.01',

lualib/cdn/events.lua

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
local cjson = require "cjson"
22
local config = require "cdn.config"
33
local log = require "cdn.log"
4+
local hosts = require "cdn.hosts"
45

56
local redis_mod = require "resty.redis"
67
local dyups = require "ngx.dyups"
@@ -116,14 +117,15 @@ _M.states = {
116117
if setting_json == nil then
117118
return false
118119
end
120+
hosts.delete_cache(servername)
121+
settings:delete(servername)
119122
local setting = cmsgpack.unpack(setting_json)
120123
local k
121124
for k, _ in pairs(setting) do
122125
log:debug("delete vhost: ", k)
123126
dyups.delete(k)
124127
upstream_cached:delete(k)
125128
end
126-
settings:delete(servername)
127129
return true
128130
end,
129131

lualib/cdn/header.lua

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
local cjson = require "cjson"
2+
local config = require "cdn.config"
3+
local log = require "cdn.log"
4+
local http_cache = require "http_cache"
5+
6+
local ipairs = ipairs
7+
local str_find = string.find
8+
9+
local ngx = ngx
10+
local ngx_var = ngx.var
11+
12+
local _M = {
13+
_VERSION = 0.01,
14+
events = {"cache"},
15+
}
16+
17+
function _M.get_file_ext(uri)
18+
return uri:match(".+%.(%w+)$") or "none"
19+
end
20+
21+
_M.states = {
22+
cache = function()
23+
local cache_status = (ngx_var.upstream_cache_status or "")
24+
local upsconf = config:get("upsconf")
25+
local cache_time = 0
26+
log:info("upsconf header: ", cjson.encode(upsconf), "cache: ", cache_status)
27+
if not upsconf["cache"] then return end
28+
for _, r in ipairs(upsconf["cache"]) do
29+
if r["url"] and str_find(ngx_var.uri, r["url"], 1, true) ~= nil then
30+
cache_time = r["time"]
31+
elseif r["file"] and str_find(r["file"], _M.get_file_ext(ngx_var.uri), 1, true) ~= nil then
32+
cache_time = r["time"]
33+
end
34+
end
35+
if cache_time > 0 and (cache_status == "MISS" or cache_status == "EXPIRED") then
36+
local cache_data = http_cache.get_metadata()
37+
local new_expire = ngx.time() + cache_time
38+
39+
if cache_data and cache_data["valid_sec"] then
40+
http_cache.set_metadata({ valid_sec = new_expire,
41+
fcn = { valid_sec = new_expire,
42+
expire = new_expire } })
43+
end
44+
end
45+
end,
46+
}
47+
48+
function _M.bootstrap()
49+
for _, state in ipairs(_M.events) do
50+
_M.states[state]()
51+
end
52+
end
53+
54+
return _M

lualib/cdn/hosts.lua

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
local cmsgpack = require "cmsgpack"
2+
local tlds = require "cdn.tlds"
3+
local lrucache_mod = require "resty.lrucache"
4+
local log = require "cdn.log"
5+
local lrudsc, err = lrucache_mod.new(500)
6+
if not lrudsc then
7+
error("failed to create the cache: " .. (err or "unknown"))
8+
end
9+
local lrucache, err = lrucache_mod.new(500)
10+
if not lrucache then
11+
error("failed to create the cache: " .. (err or "unknown"))
12+
end
13+
14+
local ngx = ngx
15+
local ngx_var = ngx.var
16+
local ngx_re_find = ngx.re.find
17+
18+
local settings = ngx.shared.settings
19+
20+
local _M = {
21+
_VERSION = '0.01',
22+
}
23+
24+
function _M.get_ups(host)
25+
local ups_key, ups_value
26+
local setting, err = _M.get_setting(host)
27+
if setting == nil then
28+
log:info("get_setting : ", err)
29+
return nil, nil
30+
end
31+
if setting[ngx_var.host] == nil then
32+
for k, v in pairs(setting) do
33+
local i = k:find("%*")
34+
if i then
35+
local rek, n, err = ngx.re.gsub(k, "\\*", "(.*?)")
36+
local from, to, err = ngx_re_find(ngx_var.host, rek, "isjo")
37+
if from and v.ups ~= nil then
38+
ups_key = k
39+
lrucache:set(ngx_var.host, ups_key)
40+
ups_value = v.ups
41+
break
42+
end
43+
end
44+
end
45+
else
46+
local v = setting[ngx_var.host]
47+
if v.ups ~= nil then
48+
ups_key = ngx_var.host
49+
lrucache:set(ngx_var.host, ups_key)
50+
ups_value = v.ups
51+
end
52+
end
53+
return ups_key, ups_value
54+
end
55+
56+
function _M.get_ups_key(host)
57+
return lrucache:get(host)
58+
end
59+
60+
function _M.get_setting(host)
61+
local topleveldomain = tlds.domain(host)
62+
if topleveldomain == nil then
63+
return nil, "failed to get tld: " .. host
64+
end
65+
local setting = lrudsc:get(topleveldomain)
66+
if setting ~= nil then
67+
return setting
68+
end
69+
local setting_json = settings:get(topleveldomain)
70+
if setting_json == nil then
71+
return nil, "failed to get setting: " .. topleveldomain
72+
end
73+
local setting = cmsgpack.unpack(setting_json)
74+
lrudsc:set(topleveldomain, setting)
75+
return setting, nil
76+
77+
end
78+
79+
function _M.delete_cache(host)
80+
lrudsc:delete(host)
81+
end
82+
83+
84+
return _M

lualib/cdn/tlds.lua

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ local function split(s, re)
3737
end
3838
end
3939

40-
function _M.get_tlds(self)
40+
function _M.get_tlds()
4141
local tlds = cached:get("tldsdb")
4242
local tmp = {}
4343
if not tlds then
@@ -65,12 +65,12 @@ function _M.get_tlds(self)
6565
return cmsgpack.unpack(cached:get("tldsdb"))
6666
end
6767

68-
function _M.domain(self, host)
68+
function _M.domain(host)
6969
local result, state = lrucache:get(host)
7070
if result then
7171
return result
7272
end
73-
local node = self.get_tlds()
73+
local node = _M.get_tlds()
7474
local parts = split(host:lower(), "%.")
7575
local i1 = 1
7676
local re = "%."

0 commit comments

Comments
 (0)