function _M.init()

in scripts/lua/lib/redis.lua [63:95]


function _M.init()
  local host = REDIS_HOST
  local password = REDIS_PASS
  local port = REDIS_PORT
  local redis = require "resty.redis"
  local red = redis:new()
  red:set_timeout(REDIS_TIMEOUT)
  
  local retryCount = REDIS_RETRY_COUNT
  local connect, err = red:connect(host, port)
  while not connect and retryCount > 0 do
    local msg = utils.concatStrings({"Failed to connect to redis at ", host, ":", port, ". Retrying ", retryCount, " more times."})
    if retryCount == 1 then
      msg = utils.concatStrings({msg:sub(1, -3), "."})
    end
    logger.info(msg)
    retryCount = retryCount - 1
    os.execute("sleep 1")
    connect, err = red:connect(host, port)
  end
  if not connect then
    request.err(500, utils.concatStrings({"Failed to connect to redis: ", err}))
  end
  
  if password ~= nil and password ~= "" and red:get_reused_times() < 1 then
    local res, err_auth = red:auth(password)
    if not res then
      ngx.log(ngx.ERR, utils.concatStrings({"[redis] failed to authenticate: ", err_auth}))
      request.err(500, "Internal server error")
    end
  end
  return red
end