From 298fd1c4a4952e863df8d222b2958aebb3cef28a Mon Sep 17 00:00:00 2001 From: Zachary Hu Date: Wed, 17 Jul 2024 21:14:07 +0800 Subject: [PATCH] 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. --- lib/resty/session/redis.lua | 5 +++-- lib/resty/session/redis/sentinel.lua | 3 ++- spec/01-utils_spec.lua | 28 ++++++++++++++++++++++++++++ spec/04-storage-1_spec.lua | 1 + spec/05-storage-2_spec.lua | 3 +++ 5 files changed, 37 insertions(+), 3 deletions(-) diff --git a/lib/resty/session/redis.lua b/lib/resty/session/redis.lua index baffcce3..262dfdbc 100644 --- a/lib/resty/session/redis.lua +++ b/lib/resty/session/redis.lua @@ -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 @@ -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 @@ -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 diff --git a/lib/resty/session/redis/sentinel.lua b/lib/resty/session/redis/sentinel.lua index 73db463f..a5768450 100644 --- a/lib/resty/session/redis/sentinel.lua +++ b/lib/resty/session/redis/sentinel.lua @@ -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() @@ -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 diff --git a/spec/01-utils_spec.lua b/spec/01-utils_spec.lua index 95cb53c7..35f45332 100644 --- a/spec/01-utils_spec.lua +++ b/spec/01-utils_spec.lua @@ -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() diff --git a/spec/04-storage-1_spec.lua b/spec/04-storage-1_spec.lua index 6271622e..56363280 100644 --- a/spec/04-storage-1_spec.lua +++ b/spec/04-storage-1_spec.lua @@ -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() diff --git a/spec/05-storage-2_spec.lua b/spec/05-storage-2_spec.lua index f6eb4c26..2fe71bb1 100644 --- a/spec/05-storage-2_spec.lua +++ b/spec/05-storage-2_spec.lua @@ -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()