Remove connection_options.pool option from redis connection and optimize redis connections

This commit is contained in:
Théophile Diot 2024-01-16 15:47:03 +00:00
parent f38f775f3d
commit 5fd54f8292
No known key found for this signature in database
GPG key ID: 248FEA4BAE400D06
2 changed files with 5 additions and 23 deletions

View file

@ -2,7 +2,6 @@ local ngx = ngx
local class = require "middleclass"
local clogger = require "bunkerweb.logger"
local rc = require "resty.redis.connector"
local rs = require("resty.redis.sentinel")
local utils = require "bunkerweb.utils"
local clusterstore = class("clusterstore")
@ -15,7 +14,6 @@ local ERR = ngx.ERR
local INFO = ngx.INFO
local tonumber = tonumber
local tostring = tostring
local random = math.random
function clusterstore:initialize(pool)
-- Get variables
@ -73,7 +71,6 @@ function clusterstore:initialize(pool)
}
self.pool = pool == nil or pool
if self.pool then
options.connection_options.pool = "bw-redis"
options.connection_options.pool_size = tonumber(self.variables["REDIS_KEEPALIVE_POOL"])
end
if self.variables["REDIS_SENTINEL_HOSTS"] ~= "" then
@ -84,7 +81,7 @@ function clusterstore:initialize(pool)
else
sport = tonumber(sport)
end
table.insert(options.sentinel, { host = shost, port = sport })
table.insert(options.sentinels, { host = shost, port = sport })
end
end
self.options = options
@ -110,23 +107,8 @@ function clusterstore:connect(readonly)
end
-- Connect to sentinels if needed
local redis_client, err
if #self.options.sentinels > 0 then
local redis_sentinel
redis_sentinel, err = self.redis_connector:connect()
if not redis_sentinel then
return false, "error while connecting to sentinels : " .. err
end
if readonly then
local redis_clients, _ = rs.get_slaves(redis_sentinel, self.options.master_name)
if redis_clients then
redis_client = redis_clients[random(#redis_clients)]
else
redis_client = nil
end
else
redis_client, err = rs.get_master(redis_sentinel, self.options.master_name)
end
-- Classic connection
if #self.options.sentinels > 0 and readonly then
redis_client, err = self.redis_connector:connect({ role = "slave" })
else
redis_client, err = self.redis_connector:connect()
end
@ -135,7 +117,8 @@ function clusterstore:connect(readonly)
return false, "error while getting redis client : " .. err
end
-- Everything went well
local times, err = self.redis_client:get_reused_times()
local times
times, err = self.redis_client:get_reused_times()
if times == nil then
self:close()
return false, "error while getting reused times : " .. err

View file

@ -122,7 +122,6 @@ function sessions:init()
send_timeout = tonumber(redis_vars["REDIS_TIMEOUT"]),
read_timeout = tonumber(redis_vars["REDIS_TIMEOUT"]),
keepalive_timeout = tonumber(redis_vars["REDIS_KEEPALIVE_IDLE"]),
pool = "bw-redis",
pool_size = tonumber(redis_vars["REDIS_KEEPALIVE_POOL"]),
ssl = redis_vars["REDIS_SSL"] == "yes",
ssl_verify = redis_vars["REDIS_SSL_VERIFY"] == "yes",