-- maximum number of threads to spawn at one time
local max_threads = 24
local function crc_request(uri)
local crc_res = {}
crc_res["uri"] = uri
-- crc_res["raw"] = ngx.location.capture(uri)
return crc_res
end
local function send_requests(buffer)
local threads = {}
for req_id, req_uri in ipairs(buffer) do
table.insert( threads, ngx.thread.spawn( crc_request, "/_getcrc" .. req_uri ) )
end
-- wait for all threads to return
for tid = 1, #threads do
local ok, res = ngx.thread.wait(threads[tid])
if not ok then
ngx.say(tid, ": failed to run: ", res)
else
ngx.say(tid, ": uri: ", res["uri"])
-- ngx.say(tid, ": status: ", res["raw"].status)
-- ngx.say(tid, ": body: ", res["raw"].body)
end
end
end
-- create some URI's to open
local files = {}
for inc = 1,2000,1 do
table.insert(files, "/imgs/cat" .. inc .. ".jpg" )
end
-- count number of files
local total_files = 0
for i, file in ipairs(files) do
total_files = total_files + 1
end
-- check how many threads to open at one time with maximum being max_threads
local open_threads = max_threads
if total_files < max_threads then
open_threads = total_files
end
-- buffer which holds maximum number of files for max_threads
local buffer = {}
local buffer_count = 0
-- for each file get the crc
for id, file in ipairs(files) do
if buffer_count < open_threads then
table.insert(buffer, file)
buffer_count = buffer_count + 1
end
if buffer_count == open_threads then
send_requests( buffer )
buffer = {}
buffer_count = 0
end
end
-- send any remaining requests
ngx.say("sending remaining requests")
send_requests( buffer )
-- end message
ngx.say("closing")