码迷,mamicode.com
首页 > 其他好文 > 详细

init_config_file.lua

时间:2018-06-06 22:15:57      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:rate   limit_req   work   初始化   log   system   shutdown   --   return   

--[[
读取限流配置
--]]

--获取共享内存
local limit_req_store = ngx.shared.limit_req_store
--初始化拒绝 URI 列表
reject_uri_tab = {}

-- 重新初始化拒绝 URI 列表
local function resetRejectUri(uri_list)
    if uri_list ~= nil then
        reject_uri_tab = {}
        while (string.len(uri_list) > 0) do
            local s, e = string.find(uri_list, ",")
            if e ~= nil then
                local str = string.sub(uri_list,1,s-1)
                uri_list = string.sub(uri_list, e+1,string.len(uri_list))
        reject_uri_tab["rejected"..str]="rejected"
      else
        reject_uri_tab["rejected"..uri_list]="rejected"
        break
      end
        end
    for key, value in pairs(reject_uri_tab) do
      ngx.log(ngx.DEBUG, "nginx limit module reject uri :",key)
    end
    end
end

-- 设置共享内存
local function setSharedDict(data)
    while (string.len(data) > 1)
    do
        local a, b = string.find(data,"\n")
        if b ~= nil then
            local str = string.sub(data,1,a-1)
            data = string.sub(data,b+1,string.len(data))
            local a1, b1 = string.find(str, "=")
            local str1 = string.sub(str,1,a1-1)
            local str2 = string.sub(str,b1+1,string.len(str))
            ngx.log(ngx.DEBUG,"limit config property: ",str1, " = ",str2)
            limit_req_store:set(str1,str2)
        else
            local a1, b1 = string.find(data,"=")
            local str1 = string.sub(data,1,a1-1)
            local str2 = string.sub(data,b1+1,string.len(data))
            ngx.log(ngx.DEBUG,"limit config property: ",str1, " = ",str2)
            limit_req_store:set(str1,str2)
            ngx.log(ngx.DEBUG, "nginx request limit initialize file has been successful read.")
            break
        end
    end
    resetRejectUri(limit_req_store:get("reject_uri_list"))
end


-- 读限流配置文件
local function fileRead()
    local file = io.open("/wls/appsystems/conf/lua-module/overload_protect_config.txt","r")
    --local file = io.open("/Users/zuodejun595/work/openresty-lua-project/nginx_limit_req/overload_protect_config.txt","r")
    if file == nil then
        ngx.log(ngx.ERR, "nginx request limit initialize file not exist.")
        return
    end
    local data = file:read("*a") --从当前位置读取整个文件。例:file.read("*a")
    ngx.log(ngx.DEBUG, "read file result:", data)
    file:close()
    setSharedDict(data)

    --过载保护策略总开关,若开关关闭,则全部策略失效
    --[[
    local overload_protection_switch = limit_req_store:get("overload_protection_switch")
    if (overload_protection_switch ~= nil and overload_protection_switch == "N") then
            ngx.log(ngx.ERR, "nginx request limit strategy has shutdown.")
        return
    end
    --]]

    ngx.log(ngx.INFO,"nginx request limit module will read config file after 5 seconds.")
    ngx.timer.at(5,fileRead)
    --ngx.timer.at(30,fileRead)
end

-- 方法执行入口
fileRead()

 

init_config_file.lua

标签:rate   limit_req   work   初始化   log   system   shutdown   --   return   

原文地址:https://www.cnblogs.com/koushr/p/5873406.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!