zoukankan      html  css  js  c++  java
  • init_config_file.lua

    --[[
    读取限流配置
    --]]
    
    --获取共享内存
    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,"
    ")
            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()
  • 相关阅读:
    JSAJAX请求
    ES6-形参默认值
    ES6-三点运算符
    ES6-箭头函数
    ES6-对象的简写方式
    ES6-使用模板字符串完成字符串拼接
    201712-2 游戏
    Product of Polynomials
    Maximum Subsequence Sum
    蓝桥杯 龟兔赛跑预测
  • 原文地址:https://www.cnblogs.com/koushr/p/5873406.html
Copyright © 2011-2022 走看看