init_worker.lua主要工作是每隔一段时间(1秒)向不同域名发送http请求,代码如下,其中simple.lua为github(https://github.com/bakins/lua-resty-http-simple/blob/master/lib/resty/http/simple.lua):
local ngx = ngx
local new_timer = ngx.timer.at
local sleep = ngx.sleep
local http = require('simple')
local collectgarbage = collectgarbage
function request(scheme, host, port, uri, get_args, post_body, headers)
local options = {}
options['method'] = method
options['path'] = uri
options['query'] = get_args
options['body'] = post_body
options['headers'] = headers
local res, err = http.request(scheme, host, port, options)
if res then
ngx.log(ngx.ERR, 'status: ', res.status)
else
ngx.log(ngx.ERR, 'request failed')
end
end
local function get_task(premature, id)
local idx = 0
while true do
local task_num = 100
if task_num > 0 then
request('http', '127.0.0.1', 8087, '/html', 'a=1', '')
collectgarbage('collect')
sleep(1)
idx = idx + 1
else
sleep(1)
end
end
end
for i = 1, 1000 do
local ok, err = new_timer(0, get_task, i)
if not ok then
ngx.log(ngx.ERR, 'create timer failed:', err)
end
end