forked from starjun/openstar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
i_worker.lua
195 lines (165 loc) · 4.63 KB
/
i_worker.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
local _worker_count = ngx.worker.count()
local _worker_id = ngx.worker.id()
local ngx_shared = ngx.shared
local require = require
local ipairs = ipairs
local ngx_log = ngx.log
local ngx_ERR = ngx.ERR
local ngx_thread = ngx.thread
local timer_at = ngx.timer.at
local timer_every = ngx.timer.every
local config_dict = ngx_shared.config_dict
local cjson_safe = require "cjson.safe"
local http = require "resty.http"
local handler
local handler_all
local handler_zero
local config_base
-- dict 清空过期内存
local function flush_expired_dict()
local dict_list = {"token_dict","count_dict","config_dict","host_dict","ip_dict","limit_ip_dict"}
for _,v in ipairs(dict_list) do
ngx_shared[v]:flush_expired()
end
end
-- 拉取config_dict配置数据
local function pull_redisConfig()
-- local httpc = http.new()
-- local _pull_url = "http://127.0.0.1:5460/api/dict_redis?action=pull&key=all_dict"
-- local res, err = httpc:request_uri(_push_url,{
-- method = "GET",
-- headers = {
-- ["Host"] = "127.0.0.1:5460",
-- }
-- })
local httpc = http.new()
-- The generic form gives us more control. We must connect manually.
httpc:set_timeout(500)
httpc:connect("127.0.0.1", 5460)
-- And request using a path, rather than a full URI.
local res, err = httpc:request{
path = "/api/dict_redis?action=pull&key=all_dict",
headers = {
["Host"] = "127.0.0.1:5460",
},
}
if not res then
ngx_log(ngx_ERR, "failed to pull_redisConfig request: ", err)
return
else
return true
end
end
-- 推送config_dict、host_dict、count_dict到redis
local function push_Master()
local httpc = http.new()
-- The generic form gives us more control. We must connect manually.
httpc:set_timeout(500)
httpc:connect("127.0.0.1", 5460)
local res, err = httpc:request{
path = "/api/dict_redis?action=push&key=all_dict&slave=yes",
headers = {
["Host"] = "127.0.0.1:5460",
},
}
if not res then
ngx_log(ngx_ERR, "failed to push_Master request: ", err)
return
else
return true
end
end
-- 推送count_dict统计、计数等
local function push_count_dict()
local httpc = http.new()
-- The generic form gives us more control. We must connect manually.
httpc:set_timeout(500)
httpc:connect("127.0.0.1", 5460)
-- And request using a path, rather than a full URI.
-- 目前是调试阶段 denug=yes ,否则就是 no
local res, err = httpc:request{
path = "/api/dict_redis?action=push&key=count_dict",
headers = {
["Host"] = "127.0.0.1:5460",
},
}
if not res then
ngx_log(ngx_ERR, "failed to push_count_dict request: ", err)
return
else
return true
end
end
-- 保存config_dict、host_dict到本机文件
local function save_configFile(_debug)
local httpc = http.new()
-- The generic form gives us more control. We must connect manually.
httpc:set_timeout(500)
httpc:connect("127.0.0.1", 5460)
-- And request using a path, rather than a full URI.
-- 调试阶段debug=yes 否则应该是 no
local res, err = httpc:request{
path = "/api/config?action=save&mod=all_Mod&debug=".._debug,
headers = {
["Host"] = "127.0.0.1:5460",
},
}
if not res then
ngx_log(ngx_ERR, "failed to save_configFile request: ", err)
return
else
return true
end
end
handler_zero = function ()
-- do something
local config = cjson_safe.decode(config_dict:get("config")) or {}
config_base = config.base or {}
local timeAt = config_base.autoSync.timeAt or 5
-- 如果 auto Sync 开启 就定时从redis 拉取配置并推送一些计数
if config_base.autoSync.state == "Master" then
push_Master()
elseif config_base.autoSync.state == "Slave" then
if pull_redisConfig() then
local _debug = "no"
if config_base.debug_Mod then
_debug = "yes"
end
save_configFile(_debug)
end
--推送count_dict到redis
push_count_dict()
else
-- nothing todo
end
--清空过期内存
ngx_thread.spawn(flush_expired_dict)
--
local ok, err = timer_at(timeAt, handler_zero)
if not ok then
ngx_log(ngx_ERR, "failed to startup handler_zero worker...", err)
end
end
handler_all = function ()
local optl = require("optl")
local dict_config_version = config_dict:get("config_version")
local optl_config_version = optl.config_version
if dict_config_version ~= optl_config_version then
local config = cjson_safe.decode(config_dict:get("config"))
-- 简单判断config,最好是内容规则的判断
if config ~= nil then
optl.config = config
optl.config_version = dict_config_version
end
end
end
handler = function()
if _worker_id == 0 then
handler_zero()
end
timer_every(1,handler_all)
end
local ok, err = timer_at(0, handler)
if not ok then
ngx_log(ngx_ERR, "failed to startup handler worker...", err)
end