1. --[[
  2. 实现请求统计,并且将单位时间内异常次数达到阀值的请求加入到黑名单中
  3. --]]
  4.  
  5. --获取共享内存
  6. local limit_req_store = ngx.shared.limit_req_store
  7.  
  8. --过载保护策略总开关,若开关关闭,则全部策略失效
  9. local overload_protection_switch = limit_req_store:get("overload_protection_switch")
  10. if (overload_protection_switch ~= nil and overload_protection_switch == "N") then
  11. ngx.log(ngx.INFO, "nginx limit strategy has been shutdown.")
  12. return
  13. end
  14.  
  15. --获取返回状态码
  16. local status = ngx.status
  17.  
  18. --获取请求uri
  19. local uri = ngx.var.uri
  20.  
  21. ngx.log(ngx.DEBUG, "current request uri:", uri, ", http status code:", status)
  22.  
  23. local count --记录当前请求的异常次数
  24. local uri_last_decay_time_key="cur_time:"..uri --记录当前请求上一次衰减时间节点的 key
  25. local black_list_key = "black_list:"..uri --记录异常次数达到上限时被加入黑名单的 URI key
  26. local os_cur_time = os.time() --获取当前系统时间,单位是秒
  27. local cur_uri = "cur:"..uri --记录请求异常次数的 key,不能直接用 uri key,会和resty-limit-multiple-strategy.lua文件中的key 冲突
  28.  
  29. --衰减时间内最大异常次数
  30. local max_fail_time= limit_req_store:get("period_max_fail_time")
  31. if (max_fail_time == nil or max_fail_time == ) then
  32. max_fail_time =
  33. end
  34.  
  35. ngx.log(ngx.DEBUG,"nginx config period_max_fail_time:",max_fail_time)
  36.  
  37. --衰减时间,所有与过期时间相关的设置单位都是秒
  38. local decay_time= limit_req_store:get("period_time")
  39. if (decay_time == nil or decay_time == ) then
  40. decay_time =
  41. end
  42.  
  43. --异常请求加入blacklist的有效时间,单位是秒
  44. local black_list_time = limit_req_store:get("black_list_survival_time")
  45. if (black_list_time == nil or black_list_time == ) then
  46. black_list_time =
  47. end
  48.  
  49. --err_code 默认为执行限流策略时的返回码,不需要后续逻辑处理
  50. if status == err_code then
  51. return
  52. end
  53.  
  54. --只统计400以上的返回异常
  55. if status >= then
  56. count= limit_req_store:get(cur_uri)
  57. if count == nil then
  58. limit_req_store:set(cur_uri,)
  59. limit_req_store:set(uri_last_decay_time_key,os_cur_time)
  60. count =
  61. else
  62. --获取当前异常次数的值
  63. count = limit_req_store:incr(cur_uri,)
  64. end
  65. --print("count-----:",count)
  66. if count >= tonumber(max_fail_time) then
  67. 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.")
  68. limit_req_store:set(black_list_key,,black_list_time)
  69. limit_req_store:delete(cur_uri)
  70. limit_req_store:delete(uri_last_decay_time_key)
  71. return
  72. end
  73. local time = limit_req_store:get(uri_last_decay_time_key)
  74. time = os_cur_time-time
  75. if time >= tonumber(decay_time) then
  76. --math.ceil()函数用户取整
  77. count = math.ceil(count/)
  78. ngx.log(ngx.DEBUG, "nginx current request uri:", uri," fail time was be decayed,current count:", count)
  79. limit_req_store:set(cur_uri,count)
  80. limit_req_store:set(uri_last_decay_time_key,os_cur_time)
  81. end
  82. 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. 运行maven build报错No goals have been specified for this build.

    运行maven报错: [ERROR] No goals have been specified for this build. You must specify a valid lifecycle p ...

  2. WCF把书读薄(4)——事务编程与可靠会话

    WCF把书读薄(3)——数据契约.消息契约与错误契约 真不愧是老A的书,例子多,而且也讲了不少原理方面的内容,不过越读越觉得压力山大……这次来稍微整理整理事务和可靠会话的内容. 十八.事务编程 WCF ...

  3. 申请参加腾讯云自媒体分享计划,送代金券+CN域名

    文章介绍 看到腾讯支持博客了,就尝试下,看看效果如何. 文章属于转载,文末有文章来源,转载注明出处. 活动规则 活动资格:面向腾讯云官网已注册且完成实名认证用户,除协作者之外,其余用户均可参与; 如何 ...

  4. 三维GIS

    三维GIS数据结构 三维GIS数据库 三维渲染显示 点云处理 cnki:http://kns.cnki.net/kns/brief/default_result.aspx

  5. .net 3.5 Lambda表达式

    Lambda表达式 转自http://www.cnblogs.com/kingmoon/archive/2011/05/03/2035696.html "Lambda表达式"是一个 ...

  6. dssfsfsfs

    一直都好使啊..好使 好使 MainActivity://overover package com.example.sockettest; import java.io.BufferedReader; ...

  7. angular ng-content

    <p> child works! </p> <ng-content></ng-content> <app-child> 父组件投影 < ...

  8. Android app如何加密?

    欢迎访问网易云社区,了解更多网易技术产品运营经验. Android App包含的内容有dex文件,so文件,res,assets资源文件.对应的加密按此内容分为三大方面:dex保护.so加密.资源保护 ...

  9. 十四、JS同步异步知识点,重点(Node.js-fs模块补充篇)

    (本片文章如果你能耐着性子看我,保证会对同步和异步有一个非常深刻的理解) JavaScript是单线程执行,所谓的单线程呢就是指如果有多个任务就必须去排队,前面任务执行完成后,后面任务再执行.因为Ja ...

  10. fputs()

    原型:int fputs(const char *str, FILE *stream) 参数解释: const char *str : const限制函数内部修改指针指向的数据(在函数形参使用cons ...