Skip to content

feat(*): support default database #179

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/resty/session/redis.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ local null = ngx.null

local DEFAULT_HOST = "127.0.0.1"
local DEFAULT_PORT = 6379
local DEFAULT_DATABASE = 0


local SET = common.SET
Expand Down Expand Up @@ -62,7 +63,7 @@ local function exec(self, func, ...)
end
end

local database = self.database
local database = self.database or DEFAULT_DATABASE
if database then
ok, err = red:select(database)
if not ok then
Expand Down Expand Up @@ -220,7 +221,7 @@ function storage.new(configuration)

local username = configuration and configuration.username
local password = configuration and configuration.password
local database = configuration and configuration.database
local database = configuration and configuration.database or DEFAULT_DATABASE

local connect_timeout = configuration and configuration.connect_timeout
local send_timeout = configuration and configuration.send_timeout
Expand Down
3 changes: 2 additions & 1 deletion lib/resty/session/redis/sentinel.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ local GET = common.GET
local UNLINK = common.UNLINK
local READ_METADATA = common.READ_METADATA

local DEFAULT_DATABASE = 0

local function exec(self, func, ...)
local red, err = self.connector:connect()
Expand Down Expand Up @@ -192,7 +193,7 @@ function storage.new(configuration)

local username = configuration and configuration.username
local password = configuration and configuration.password
local database = configuration and configuration.database
local database = configuration and configuration.database or DEFAULT_DATABASE

local connect_timeout = configuration and configuration.connect_timeout
local send_timeout = configuration and configuration.send_timeout
Expand Down
28 changes: 28 additions & 0 deletions spec/01-utils_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,34 @@ describe("Testing utils", function()
end)
end)
describe("load_storage", function()
for _, db in ipairs { 1, 2, "nil" } do
it("set correct #redis database " .. db, function()
local strategy = "redis"
local storage = assert(utils.load_storage(strategy, {
[strategy] = {
prefix = "oidc-storage",
host = "redis",
port = 6379,
database = db ~= "nil" and db or nil,
username = "default",
password = "PaSsw0rd",
connect_timeout = 1000,
read_timeout = 1000,
send_timeout = 1000,
pool = "oidc:redis:6379",
pool_size = 10,
backlog = 20,
},
}))

assert.equal(db == "nil" and 0 or db, storage.database)
assert.equal("PaSsw0rd", storage.password)
assert.equal("oidc:redis:6379", storage.options.pool)
assert.equal(10, storage.options.pool_size)
assert.equal(20, storage.options.backlog)
end)
end

-- "dshm" is disabled as it currently cannot be checked by CI
for _, strategy in ipairs({ "memcached", "mysql", "postgres", "redis" }) do
it("respects pool parameters #" .. strategy, function()
Expand Down
3 changes: 3 additions & 0 deletions spec/04-storage-1_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ for _, st in ipairs({
conf[st] = storage_configs[st]
storage = utils.load_storage(st, conf)
assert.is_not_nil(storage)
if st == "redis" then
assert.equals(0, storage.database)
end
end)

before_each(function()
Expand Down
3 changes: 3 additions & 0 deletions spec/05-storage-2_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ for _, st in ipairs({
conf[storage_type(st)] = storage_configs[st]
storage = utils.load_storage(storage_type(st), conf)
assert.is_not_nil(storage)
if storage_type(st) == "redis" then
assert.equals(0, storage.database)
end
end)

before_each(function()
Expand Down
Loading