Skip to content

Commit 1ea001f

Browse files
committed
feat(*): support default database
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.
1 parent 38804b5 commit 1ea001f

5 files changed

Lines changed: 39 additions & 3 deletions

File tree

lib/resty/session/redis.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ local null = ngx.null
1515

1616
local DEFAULT_HOST = "127.0.0.1"
1717
local DEFAULT_PORT = 6379
18+
local DEFAULT_DATABASE = 0
1819

1920

2021
local SET = common.SET
@@ -62,7 +63,7 @@ local function exec(self, func, ...)
6263
end
6364
end
6465

65-
local database = self.database
66+
local database = self.database or DEFAULT_DATABASE
6667
if database then
6768
ok, err = red:select(database)
6869
if not ok then
@@ -220,7 +221,7 @@ function storage.new(configuration)
220221

221222
local username = configuration and configuration.username
222223
local password = configuration and configuration.password
223-
local database = configuration and configuration.database
224+
local database = configuration and configuration.database or DEFAULT_DATABASE
224225

225226
local connect_timeout = configuration and configuration.connect_timeout
226227
local send_timeout = configuration and configuration.send_timeout

lib/resty/session/redis/sentinel.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ local GET = common.GET
1818
local UNLINK = common.UNLINK
1919
local READ_METADATA = common.READ_METADATA
2020

21+
local DEFAULT_DATABASE = 0
2122

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

193194
local username = configuration and configuration.username
194195
local password = configuration and configuration.password
195-
local database = configuration and configuration.database
196+
local database = configuration and configuration.database or DEFAULT_DATABASE
196197

197198
local connect_timeout = configuration and configuration.connect_timeout
198199
local send_timeout = configuration and configuration.send_timeout

spec/01-utils_spec.lua

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,34 @@ describe("Testing utils", function()
137137
end)
138138
end)
139139
describe("load_storage", function()
140+
for _, db in ipairs { 1, 2, "nil" } do
141+
it("set correct #redis database " .. db, function()
142+
local strategy = "redis"
143+
local storage = assert(utils.load_storage(strategy, {
144+
[strategy] = {
145+
prefix = "oidc-storage",
146+
host = "redis",
147+
port = 6379,
148+
database = db == "nil" and nil or db,
149+
username = "default",
150+
password = "PaSsw0rd",
151+
connect_timeout = 1000,
152+
read_timeout = 1000,
153+
send_timeout = 1000,
154+
pool = "oidc:redis:6379",
155+
pool_size = 10,
156+
backlog = 20,
157+
},
158+
}))
159+
160+
assert.equal(db == "nil" and 0 or db, storage.database)
161+
assert.equal("PaSsw0rd", storage.password)
162+
assert.equal("oidc:redis:6379", storage.options.pool)
163+
assert.equal(10, storage.options.pool_size)
164+
assert.equal(20, storage.options.backlog)
165+
end)
166+
end
167+
140168
-- "dshm" is disabled as it currently cannot be checked by CI
141169
for _, strategy in ipairs({ "memcached", "mysql", "postgres", "redis" }) do
142170
it("respects pool parameters #" .. strategy, function()

spec/04-storage-1_spec.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ for _, st in ipairs({
6565
conf[st] = storage_configs[st]
6666
storage = utils.load_storage(st, conf)
6767
assert.is_not_nil(storage)
68+
if st == "redis" then
69+
assert.equals(0, storage.database)
70+
end
6871
end)
6972

7073
before_each(function()

spec/05-storage-2_spec.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ for _, st in ipairs({
9393
conf[storage_type(st)] = storage_configs[st]
9494
storage = utils.load_storage(storage_type(st), conf)
9595
assert.is_not_nil(storage)
96+
if storage_type(st) == "redis" then
97+
assert.equals(0, storage.database)
98+
end
9699
end)
97100

98101
before_each(function()

0 commit comments

Comments
 (0)