--[[
实现请求统计,并且将单位时间内异常次数达到阀值的请求加入到黑名单中
--]] --获取共享内存
local limit_req_store = ngx.shared.limit_req_store --过载保护策略总开关,若开关关闭,则全部策略失效
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.INFO, "nginx limit strategy has been shutdown.")
return
end --获取返回状态码
local status = ngx.status --获取请求uri
local uri = ngx.var.uri ngx.log(ngx.DEBUG, "current request uri:", uri, ", http status code:", status) local count --记录当前请求的异常次数
local uri_last_decay_time_key="cur_time:"..uri --记录当前请求上一次衰减时间节点的 key
local black_list_key = "black_list:"..uri --记录异常次数达到上限时被加入黑名单的 URI 的 key
local os_cur_time = os.time() --获取当前系统时间,单位是秒
local cur_uri = "cur:"..uri --记录请求异常次数的 key,不能直接用 uri 做 key,会和resty-limit-multiple-strategy.lua文件中的key 冲突 --衰减时间内最大异常次数
local max_fail_time= limit_req_store:get("period_max_fail_time")
if (max_fail_time == nil or max_fail_time == ) then
max_fail_time =
end ngx.log(ngx.DEBUG,"nginx config period_max_fail_time:",max_fail_time) --衰减时间,所有与过期时间相关的设置单位都是秒
local decay_time= limit_req_store:get("period_time")
if (decay_time == nil or decay_time == ) then
decay_time =
end --异常请求加入blacklist的有效时间,单位是秒
local black_list_time = limit_req_store:get("black_list_survival_time")
if (black_list_time == nil or black_list_time == ) then
black_list_time =
end --err_code 默认为执行限流策略时的返回码,不需要后续逻辑处理
if status == err_code then
return
end --只统计400以上的返回异常
if status >= then
count= limit_req_store:get(cur_uri)
if count == nil then
limit_req_store:set(cur_uri,)
limit_req_store:set(uri_last_decay_time_key,os_cur_time)
count =
else
--获取当前异常次数的值
count = limit_req_store:incr(cur_uri,)
end
--print("count-----:",count)
if count >= tonumber(max_fail_time) then
ngx.log(ngx.ERR, "nginx current request uri:", uri," fail time is up to the max_fail_time,and will be added to black_list.")
limit_req_store:set(black_list_key,,black_list_time)
limit_req_store:delete(cur_uri)
limit_req_store:delete(uri_last_decay_time_key)
return
end
local time = limit_req_store:get(uri_last_decay_time_key)
time = os_cur_time-time
if time >= tonumber(decay_time) then
--math.ceil()函数用户取整
count = math.ceil(count/)
ngx.log(ngx.DEBUG, "nginx current request uri:", uri," fail time was be decayed,current count:", count)
limit_req_store:set(cur_uri,count)
limit_req_store:set(uri_last_decay_time_key,os_cur_time)
end
end

request-statistics.lua的更多相关文章

  1. 【lua】LWT HttpdModule

    要使用httpd模块,需要在脚本开头添加: require "httpd" httpd.pairs(apr_table) 用以遍历apr_table for key, value ...

  2. 01 . OpenResty简介部署,优缺点,压测,适用场景及用Lua实现服务灰度发布

    简介 OpenResty 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库.第三方模块以及大多数的依赖项.用于方便地搭建能够处理超高并发.扩展性极高的动态 ...

  3. Nginx+lua+openresty精简系列

    1. CentOS系统安装openresty 你可以在你的 CentOS 系统中添加 openresty 仓库,这样就可以便于未来安装或更新我们的软件包(通过 yum update 命令).运行下面的 ...

  4. PHP7函数大全(4553个函数)

    转载来自: http://www.infocool.net/kb/PHP/201607/168683.html a 函数 说明 abs 绝对值 acos 反余弦 acosh 反双曲余弦 addcsla ...

  5. cf.VK CUP 2015.B.Mean Requests

    Mean Requests time limit per test 4 seconds memory limit per test 256 megabytes input standard input ...

  6. 微软Azure云平台Hbase 的使用

    In this article What is HBase? Prerequisites Provision HBase clusters using Azure Management portal ...

  7. ntopng源码分析

    参数初始化以及ntop主流程启动 #ifndef WIN32 ) && (argv[][] != '-')) rc = prefs->loadFromFile(argv[]); ...

  8. Oracle Applications DBA 基础(二)

    6.OAM及系统管理 2014年9月13日 20:40 参考资料: 1.Oracle Applications System Administrator's Guide - Configuration ...

  9. 性能测试工具Locust的使用

    一.写在前面 官网:https://www.locust.io/ 官方使用文档:https://docs.locust.io/en/latest/ 大并发量测试时,建议在linux系统下进行. 二.L ...

  10. PHP类和函数注释大全

    每次要用PHP的某个功能的时候,都要去查一下,于是决定将PHP所有类和函数都整理出来,加上注释 大致实现 将php.jar文件解压,取出目录stubs 将stubs中的所有php文件中的注释去掉,并做 ...

随机推荐

  1. HDU 6034 Balala Power! (贪心+坑题)

    题意:给定一个 n 个字符串,然后问你怎么给 a-z赋值0-25,使得给定的字符串看成26进制得到的和最大,并且不能出现前导0. 析:一个很恶心的题目,细节有点多,首先是思路,给定个字符一个权值,然后 ...

  2. 换零钞——第九届蓝桥杯C语言B组(国赛)第一题

    原创 标题:换零钞 x星球的钞票的面额只有:100元,5元,2元,1元,共4种.小明去x星旅游,他手里只有2张100元的x星币,太不方便,恰好路过x星银行就去换零钱.小明有点强迫症,他坚持要求200元 ...

  3. 转Delphi中XLSReadWrite控件的使用(1)---简介

    XLSReadWrite控件简介: 一个你需要的,能在Delphi和.NET下访问Excel文件的完美解决方案. 一个经典的读写Excel的控件,对于使用Excel 开发很有帮助 官方网站: http ...

  4. springcloud 实现微服务间调用

    package com.idoipo.ibt.config; import org.apache.http.HttpException; import org.apache.http.HttpRequ ...

  5. 微服务linux启动停止脚本

    # 停止脚本#!/bin/bash #其他服务停止脚步可以通过修改APP_MAIN参数即可 APP_MAIN=com.idoipo.infras.eureka.center.Application t ...

  6. 正则表达式回溯-导致CPU偏高

    最近了解了下有关正则表达式回溯的内容,想想就写下来,方便自己. 正则表达式匹配算法是建立在正则表达式引擎的基础上的,目前有两种引擎:DFA(确定型有穷自动机)和NFA(不确定型有穷自动机).这两种引擎 ...

  7. Vue watch用法

    Vue.js 提供了一个方法 watch,它用于观察Vue实例上的数据变动.对应一个对象,键是观察表达式,值是对应回调.值也可以是方法名,或者是对象,包含选项. 例如,同一个组件切换时,不会触发生命周 ...

  8. C# 抽象(4)

    抽象类和普通类有区别嘛?在继承的时候,还是只能继承一次父类嘛? 答案: 抽象类除了在抽象方法和属性上面不能实现具体的代码之外和普通类没有区别.依旧符合类的基本特征.所以在继承的时候抽象类也是属于基类, ...

  9. 犯得错误QAQ

    1.十年OI一场空,不开longlong见祖宗(丢过150分) 2.计算完了再开数组,开的足足的.不要少开0:(丢过一共200分) 3.最大值,最小值一定开成7个f.(丢了20分). 4.freope ...

  10. 190221协程与IO模型

    一.协程 又称微线程 协程是一种用户态的轻量级的线程 在单线程下实现的并发,例如:yield 优点: 无需线程上下文切换的开销 无需原子操作锁定及同步的开销 方便切换控制流,简化编程模型 高并发,高扩 ...