Skip to content

Commit

Permalink
feat(*): support default database
Browse files Browse the repository at this point in the history
We ensure the default database `0` is used if there is no database is
provided, so that the correct database is selected whenever a connection
with a different database is retrieved from the connection pool.
  • Loading branch information
outsinre committed Jul 17, 2024
1 parent 38804b5 commit 298fd1c
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
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 i, 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 nil or i,
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 i, 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
1 change: 1 addition & 0 deletions spec/04-storage-1_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ for _, st in ipairs({
conf[st] = storage_configs[st]
storage = utils.load_storage(st, conf)
assert.is_not_nil(storage)
assert.equals(0, storage.database)
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

0 comments on commit 298fd1c

Please sign in to comment.