mirror of
https://github.com/bunkerity/bunkerweb
synced 2026-05-24 09:28:37 +00:00
bw - various improvements to redis sentinel
This commit is contained in:
parent
551a0b5205
commit
5ad99ac82b
6 changed files with 42 additions and 9 deletions
|
|
@ -8,7 +8,7 @@ copyright: Copyright © <script>document.write(new Date().getFullYear())</sc
|
|||
|
||||
nav:
|
||||
- Introduction: 'index.md'
|
||||
- Migrating from 1.4.X: 'migrating.md'
|
||||
- Migrating: 'migrating.md'
|
||||
- Concepts: 'concepts.md'
|
||||
- Integrations: 'integrations.md'
|
||||
- Quickstart guide: 'quickstart-guide.md'
|
||||
|
|
|
|||
|
|
@ -63,7 +63,7 @@ function cachestore:get(key)
|
|||
local callback = function(key, cs)
|
||||
-- Connect to redis
|
||||
-- luacheck: ignore 431
|
||||
local ok, err, _ = cs:connect()
|
||||
local ok, err, _ = cs:connect(true)
|
||||
if not ok then
|
||||
return nil, "can't connect to redis : " .. err, nil
|
||||
end
|
||||
|
|
|
|||
|
|
@ -12,7 +12,9 @@ local logger = clogger:new("CLUSTERSTORE")
|
|||
local get_variable = utils.get_variable
|
||||
local is_cosocket_available = utils.is_cosocket_available
|
||||
local ERR = ngx.ERR
|
||||
local INFO = ngx.INFO
|
||||
local tonumber = tonumber
|
||||
local tostring = tostring
|
||||
local random = math.random
|
||||
|
||||
function clusterstore:initialize(pool)
|
||||
|
|
@ -112,7 +114,7 @@ function clusterstore:connect(readonly)
|
|||
return false, "error while connecting to sentinels : " .. err
|
||||
end
|
||||
if readonly then
|
||||
redis_clients, err = rs.get_slaves(redis_sentinel, self.options.master_name)
|
||||
local redis_clients, err = rs.get_slaves(redis_sentinel, self.options.master_name)
|
||||
if redis_clients then
|
||||
redis_client = redis_clients[random(#redis_clients)]
|
||||
else
|
||||
|
|
@ -130,7 +132,13 @@ function clusterstore:connect(readonly)
|
|||
return false, "error while getting redis client : " .. err
|
||||
end
|
||||
-- Everything went well
|
||||
return true, "success", self.redis_client:get_reused_times()
|
||||
local times, err = self.redis_client:get_reused_times()
|
||||
if times == nil then
|
||||
self:close()
|
||||
return false, "error while getting reused times : " .. err
|
||||
end
|
||||
logger:log(INFO, "redis reused times = " .. tostring(times))
|
||||
return true, "success", times
|
||||
end
|
||||
|
||||
function clusterstore:close()
|
||||
|
|
|
|||
|
|
@ -637,7 +637,7 @@ utils.is_banned = function(ip)
|
|||
end
|
||||
-- Connect
|
||||
local clusterstore = require "bunkerweb.clusterstore":new()
|
||||
local ok, err = clusterstore:connect()
|
||||
local ok, err = clusterstore:connect(true)
|
||||
if not ok then
|
||||
return nil, "can't connect to redis server : " .. err
|
||||
end
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ function redis:init_worker()
|
|||
return self:ret(true, "init_worker not needed")
|
||||
end
|
||||
-- Check redis connection
|
||||
local ok, err = self.clusterstore:connect()
|
||||
local ok, err = self.clusterstore:connect(true)
|
||||
if not ok then
|
||||
return self:ret(false, "redis connect error : " .. err)
|
||||
end
|
||||
|
|
|
|||
|
|
@ -61,6 +61,12 @@ function sessions:init()
|
|||
["REDIS_TIMEOUT"] = "",
|
||||
["REDIS_KEEPALIVE_IDLE"] = "",
|
||||
["REDIS_KEEPALIVE_POOL"] = "",
|
||||
["REDIS_USERNAME"] = "",
|
||||
["REDIS_PASSWORD"] = "",
|
||||
["REDIS_SENTINEL_HOSTS"] = "",
|
||||
["REDIS_SENTINEL_USERNAME"] = "",
|
||||
["REDIS_SENTINEL_PASSWORD"] = "",
|
||||
["REDIS_SENTINEL_MASTER"] = ""
|
||||
}
|
||||
for k, _ in pairs(redis_vars) do
|
||||
local value, err = get_variable(k, false)
|
||||
|
|
@ -105,6 +111,8 @@ function sessions:init()
|
|||
config.storage = "redis"
|
||||
config.redis = {
|
||||
prefix = "sessions_",
|
||||
username = redis_vars["REDIS_USERNAME"],
|
||||
password = redis_vars["REDIS_PASSWORD"],
|
||||
connect_timeout = tonumber(redis_vars["REDIS_TIMEOUT"]),
|
||||
send_timeout = tonumber(redis_vars["REDIS_TIMEOUT"]),
|
||||
read_timeout = tonumber(redis_vars["REDIS_TIMEOUT"]),
|
||||
|
|
@ -112,10 +120,27 @@ function sessions:init()
|
|||
pool = "bw-redis",
|
||||
pool_size = tonumber(redis_vars["REDIS_KEEPALIVE_POOL"]),
|
||||
ssl = redis_vars["REDIS_SSL"] == "yes",
|
||||
host = redis_vars["REDIS_HOST"],
|
||||
port = tonumber(redis_vars["REDIS_PORT"]),
|
||||
database = tonumber(redis_vars["REDIS_DATABASE"]),
|
||||
database = tonumber(redis_vars["REDIS_DATABASE"])
|
||||
}
|
||||
if redis_vars["REDIS_SENTINEL_HOSTS"] ~= "" then
|
||||
config.redis.master = redis_vars["REDIS_SENTINEL_MASTER"]
|
||||
config.redis.role = "master"
|
||||
config.redis.sentinel_username = redis_vars["REDIS_SENTINEL_USERNAME"]
|
||||
config.redis.sentinel_password = redis_vars["REDIS_SENTINEL_PASSWORD"]
|
||||
config.redis.sentinels = {}
|
||||
for sentinel_host in redis_vars["REDIS_SENTINEL_HOSTS"]:gmatch("%S+") do
|
||||
local shost, sport = sentinel_host:match("([^:]+):?(%d*)")
|
||||
if sport == "" then
|
||||
sport = 26379
|
||||
else
|
||||
sport = tonumber(sport)
|
||||
end
|
||||
table.insert(config.redis.sentinels, {host = shost, port = sport})
|
||||
end
|
||||
else
|
||||
config.redis.host = redis_vars["REDIS_HOST"]
|
||||
config.redis.port = tonumber(redis_vars["REDIS_PORT"])
|
||||
end
|
||||
end
|
||||
session_init(config)
|
||||
return self:ret(true, "sessions init successful")
|
||||
|
|
|
|||
Loading…
Reference in a new issue