Skip to content

Commit

Permalink
* Lua code functioning.
Browse files Browse the repository at this point in the history
  • Loading branch information
António P. P. Almeida committed Feb 1, 2012
1 parent 69e13af commit 927d594
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
7 changes: 5 additions & 2 deletions lua/cache_warmer_client.lua
Expand Up @@ -28,18 +28,21 @@
-- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
-- DEALINGS IN THE SOFTWARE.

-- This Lua code is part of the cache_warmer.

-- The http component of the socket library.
local http = require(socket.http)
local http = require('socket.http')

-- Make the request to the given URIs.
function cache_warmer_make_request(uri)
-- Make the request and get the results.
local response, status, headers = http.request {
url = uri,
method = 'HEAD',
-- Use a different User-Agent.
headers = { ['user-agent'] = 'Nginx Cache Warmer' },
redirect = true -- follow redirects
}

return response, status, headers
end -- cache_warmer_make_request

Expand Down
44 changes: 36 additions & 8 deletions lua/cache_warmer_requests.lua
@@ -1,4 +1,32 @@
-- Perform the location multi capture to issue the requests in parallel.
-- cache_warmer_requests.lua --- Performs the location multi capture
-- to issue the requests in parallel.

-- Copyright (C) 2012 António P. P. Almeida <appa@perusio.net>

-- Author: António P. P. Almeida <appa@perusio.net>

-- Permission is hereby granted, free of charge, to any person obtaining a
-- copy of this software and associated documentation files (the "Software"),
-- to deal in the Software without restriction, including without limitation
-- the rights to use, copy, modify, merge, publish, distribute, sublicense,
-- and/or sell copies of the Software, and to permit persons to whom the
-- Software is furnished to do so, subject to the following conditions:

-- The above copyright notice and this permission notice shall be included in
-- all copies or substantial portions of the Software.

-- Except as contained in this notice, the name(s) of the above copyright
-- holders shall not be used in advertising or otherwise to promote the sale,
-- use or other dealings in this Software without prior written authorization.

-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
-- THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
-- DEALINGS IN THE SOFTWARE.

-- This Lua code is part of the cache_warmer.

-- First we grab the POST data.
Expand All @@ -11,21 +39,21 @@ post_data['base_uri'] = nil

local requests = {} -- requests table

-- Building the location for making the parallel requests.
-- -- Building the location for making the parallel requests.
function build_req_uri(base_uri, uri)
return string.format('/parallel-reqs?u=%s/%s', base_uri, uri)
end -- build_req_uri

-- Loop over the post_data table (contains the URIs to be hit).
for i, u in pairs(post_data) do
for _, u in pairs(post_data) do
-- All requests are HEAD requests.
table.insert(requests, { build_req_uri(base_uri, u), { method = ngx.HTTP_HEAD }})
table.insert(requests, { build_req_uri(base_uri, u),{ method = ngx.HTTP_HEAD }})
end

-- Issue the requests and store the responses.
local responses = ngx.location_multi(requests)
-- Issue the requests and store the responses in a table.
local responses = { ngx.location.capture_multi(requests) }

-- Process the responses.
for i, r in pairs(responses) do
ngx.say(i, r)
for _, r in pairs(responses) do
ngx.say(r.status) -- get the status only (HEAD)
end

0 comments on commit 927d594

Please sign in to comment.