-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e26a80f
commit 569ba9c
Showing
7 changed files
with
150 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
local helpers = require "spec.helpers" | ||
local cjson = require "cjson" | ||
local meta = require "kong.meta" | ||
|
||
describe("Resolver", function() | ||
local client | ||
setup(function() | ||
helpers.kill_all() | ||
|
||
assert(helpers.dao.apis:insert { | ||
request_host = "mockbin.com", | ||
upstream_url = "http://mockbin.com" | ||
}) | ||
|
||
assert(helpers.start_kong({ | ||
["custom_plugins"] = "database-cache", | ||
lua_package_path = "?/init.lua;./kong/?.lua;./spec/fixtures/?.lua" | ||
})) | ||
|
||
-- Add the plugin | ||
local admin_client = helpers.admin_client() | ||
local res = assert(admin_client:send { | ||
method = "POST", | ||
path = "/apis/mockbin.com/plugins/", | ||
body = { | ||
name = "database-cache" | ||
}, | ||
headers = { | ||
["Content-Type"] = "application/json" | ||
} | ||
}) | ||
assert.res_status(201, res) | ||
admin_client:close() | ||
end) | ||
|
||
teardown(function() | ||
helpers.stop_kong() | ||
end) | ||
|
||
it("avoids dog-pile effect", function() | ||
local function make_request(premature, sleep_time) | ||
local client = helpers.proxy_client() | ||
local res = assert(client:send { | ||
method = "GET", | ||
path = "/status/200?sleep="..sleep_time, | ||
headers = { | ||
["Host"] = "mockbin.com" | ||
} | ||
}) | ||
res:read_body() | ||
client:close() | ||
end | ||
|
||
local ok, err = ngx.timer.at(0, make_request, 2) | ||
assert.truthy(ok) | ||
|
||
local ok, err = ngx.timer.at(0, make_request, 5) | ||
assert.truthy(ok) | ||
|
||
local ok, err = ngx.timer.at(0, make_request, 1) | ||
assert.truthy(ok) | ||
|
||
helpers.wait_until(function() | ||
local admin_client = helpers.admin_client() | ||
local res = assert(admin_client:send { | ||
method = "GET", | ||
path = "/cache/invocations" | ||
}) | ||
local body = res:read_body() | ||
admin_client:close() | ||
return cjson.decode(body).message == 3 | ||
end, 10) | ||
|
||
-- Invocation are 3, but lookups should be 1 | ||
local admin_client = helpers.admin_client() | ||
local res = assert(admin_client:send { | ||
method = "GET", | ||
path = "/cache/lookups" | ||
}) | ||
local body = res:read_body() | ||
admin_client:close() | ||
assert.equal(1, cjson.decode(body).message) | ||
end) | ||
end) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
local BasePlugin = require "kong.plugins.base_plugin" | ||
local cache = require "kong.tools.database_cache" | ||
|
||
local INVOCATIONS = "invocations" | ||
local LOOKUPS = "lookups" | ||
|
||
local DatabaseCacheHandler = BasePlugin:extend() | ||
|
||
DatabaseCacheHandler.PRIORITY = 1000 | ||
|
||
function DatabaseCacheHandler:new() | ||
DatabaseCacheHandler.super.new(self, "database-cache") | ||
end | ||
|
||
function DatabaseCacheHandler:init_worker() | ||
DatabaseCacheHandler.super.init_worker(self) | ||
|
||
cache.rawset(INVOCATIONS, 0) | ||
cache.rawset(LOOKUPS, 0) | ||
end | ||
|
||
function DatabaseCacheHandler:access(conf) | ||
DatabaseCacheHandler.super.access(self) | ||
|
||
cache.get_or_set("pile_effect", function() | ||
cache.incr(LOOKUPS, 1) | ||
-- Adds some delay | ||
ngx.sleep(tonumber(ngx.req.get_uri_args().sleep)) | ||
return true | ||
end) | ||
|
||
cache.incr(INVOCATIONS, 1) | ||
end | ||
|
||
return DatabaseCacheHandler |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
return { | ||
fields = {} | ||
} |