Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HTTP Cache support to http_ng #357

Merged
merged 12 commits into from
May 5, 2017
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

- Bump OpenResty version to [1.11.2.3](https://github.com/3scale/s2i-openresty/releases/tag/1.11.2.3-1) [PR #359](https://github.com/3scale/apicast/pull/359)

### Added

- Experimental caching proxy to the http client [PR #357](https://github.com/3scale/apicast/pull/357)

## [3.0.0-rc1] - 2017-04-04

### Added
Expand Down
4 changes: 2 additions & 2 deletions apicast/src/resty/http_ng.lua
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ http.method = function(method, client)
local req_params = get_request_params(method, client, url, options)
local req = http.request.new(req_params)

return client.backend.send(req)
return client.backend:send(req)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the difference between : and . ?

Copy link
Contributor Author

@mikz mikz May 3, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hah! : is syntactic sugar for passing self.

So these are the same code:

object:method(param)
object.method(object, param)

This works in the function definition too:

function _M.method(self, param)
end

function _M:method(param)
end

Passing self It is the only way how a function can know about the object being called on. Like instance method. Otherwise it does not really have any state other than a closure.

end
end

Expand All @@ -111,7 +111,7 @@ http.method_with_body = function(method, client)
req_params.body = body
local req = http.request.new(req_params)

return client.backend.send(req)
return client.backend:send(req)
end
end

Expand Down
2 changes: 1 addition & 1 deletion apicast/src/resty/http_ng/backend/async_resty.lua
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ local function future(thread, request)
})
end

_M.send = function(request)
_M.send = function(_, request)
local thread = spawn(_M.async, request)
return future(thread, request)
end
Expand Down
37 changes: 37 additions & 0 deletions apicast/src/resty/http_ng/backend/cache.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
local setmetatable = setmetatable

------------
--- HTTP
-- HTTP client
-- @module http_ng.backend

local _M = {}

local mt = { __index = _M }

function _M.new(backend, options)
local opts = options or {}
return setmetatable({
backend = backend, cache_store = opts.cache_store
}, mt)
end

--- Send request and return the response
-- @tparam http_ng.request request
-- @treturn http_ng.response
function _M:send(request)
local cache_store = self.cache_store
local backend = self.backend

local response, err

if cache_store then
response, err = cache_store:send(backend, request)
else
response, err = backend:send(request)
end

return response, err
end

return _M
2 changes: 1 addition & 1 deletion apicast/src/resty/http_ng/backend/ngx.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ local METHODS = {
local PROXY_LOCATION = '/___http_call'

backend.capture = ngx.location.capture
backend.send = function(request)
backend.send = function(_, request)
local res = backend.capture(PROXY_LOCATION, {
method = METHODS[request.method],
body = request.body,
Expand Down
2 changes: 1 addition & 1 deletion apicast/src/resty/http_ng/backend/resty.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ local http = require 'resty.resolver.http'
--- Send request and return the response
-- @tparam http_ng.request request
-- @treturn http_ng.response
backend.send = function(request)
backend.send = function(_, request)
local httpc = http.new()
local ssl_verify = request.options and request.options.ssl and request.options.ssl.verify

Expand Down
13 changes: 11 additions & 2 deletions apicast/src/resty/http_ng/backend/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local pairs = pairs
local type = type
local assert = assert
local setmetatable = setmetatable
local getmetatable = getmetatable
local insert = table.insert
local remove = table.remove
local error = error
Expand All @@ -13,7 +14,15 @@ local _M = {}
local function contains(expected, actual)
if actual == expected then return true end
local t1,t2 = type(actual), type(expected)
if t1 ~= t2 then return false, format("can't compare %q with %q", t1, t2) end

if t1 ~= t2 then
local mt = getmetatable(actual) or {}
if t2 == 'string' and mt.__tostring then
return mt.__tostring(actual) == expected
else
return false, format("can't compare %q with %q", t1, t2)
end
end

if t1 == 'table' then
for k,v in pairs(expected) do
Expand Down Expand Up @@ -60,7 +69,7 @@ _M.new = function()
return expectation
end

backend.send = function(request)
backend.send = function(_, request)
local expectation = remove(expectations, 1)

if not expectation then error('no expectation') end
Expand Down
Loading