当前位置: 首页 > news >正文

Kong基于QPS、IP限流

Rate Limiting限流插件

https://docs.konghq.com/hub/kong-inc/rate-limiting/

它可以针对consumer ,credential ,ip ,service,path,header 等多种维度来进行限流.流量控制的精准度也有多种方式可以参考,比如可以做到秒级,分钟级,小时级等限流控制.

基于IP限流

源码地址: kong/kong/plugins/ip-restriction/handler.lua at master · Kong/kong · GitHub

local lrucache = require "resty.lrucache"
local ipmatcher = require "resty.ipmatcher"
local kong_meta = require "kong.meta"local error = error
local kong = kong
local log = kong.log
local ngx_var = ngx.varlocal IPMATCHER_COUNT = 512
local IPMATCHER_TTL   = 3600
local cache = lrucache.new(IPMATCHER_COUNT)local IpRestrictionHandler = {PRIORITY = 990,VERSION = kong_meta.version,
}local isempty
dolocal tb_isempty = require "table.isempty"isempty = function(t)return t == nil or tb_isempty(t)end
endlocal function do_exit(status, message)status = status or 403message = message orstring.format("IP address not allowed: %s", ngx_var.remote_addr)log.warn(message)return kong.response.error(status, message)
endlocal function match_bin(list, binary_remote_addr)local matcher, errmatcher = cache:get(list)if not matcher thenmatcher, err = ipmatcher.new(list)if err thenreturn error("failed to create a new ipmatcher instance: " .. err)endcache:set(list, matcher, IPMATCHER_TTL)endlocal is_matchis_match, err = matcher:match_bin(binary_remote_addr)if err thenreturn error("invalid binary ip address: " .. err)endreturn is_match
endlocal function do_restrict(conf)local binary_remote_addr = ngx_var.binary_remote_addrif not binary_remote_addr thenreturn do_exit(403,"Cannot identify the client IP address, " .."unix domain sockets are not supported.")endlocal deny = conf.denyif not isempty(deny) thenlocal blocked = match_bin(deny, binary_remote_addr)if blocked thenreturn do_exit(conf.status, conf.message)endendlocal allow = conf.allowif not isempty(allow) thenlocal allowed = match_bin(allow, binary_remote_addr)if not allowed thenreturn do_exit(conf.status, conf.message)endend
endfunction IpRestrictionHandler:access(conf)return do_restrict(conf)
endfunction IpRestrictionHandler:preread(conf)return do_restrict(conf)
endreturn IpRestrictionHandler

http://www.lryc.cn/news/332821.html

相关文章:

  • 基于springboot实现甘肃非物质文化网站系统项目【项目源码+论文说明】
  • 【瑞萨RA6M3】1. 基于 vscode 搭建开发环境
  • 使用pip install替代conda install将packet下载到anaconda虚拟环境
  • 【HTML】常用CSS属性
  • python中的print(f‘‘)具体用法
  • 《青少年成长管理2024》022 “成长七要素之三:文化”4/5
  • Linux(05) Debian 系统修改主机名
  • 之前翻硬币问题胡思乱想的完善
  • 前端与后端协同:实现Excel导入导出功能
  • Docker:探索容器化技术,重塑云计算时代应用交付与管理
  • 畅捷通T+ KeyInfoList.aspx SQL漏洞复现
  • 【面经】interrupt()、interrupted()和isInterrupted()的区别与使用
  • 了解这些技术:Flutter应用顺利登陆iOS平台的步骤与方法
  • 经济学 劳动市场 医疗经济学
  • vue + koa + Sequelize + 阿里云部署 + 宝塔:宝塔数据库连接
  • 华为昇腾认证考试内容有哪些
  • Spring Boot接收从前端传过来的数据常用方式以及处理的技巧
  • EFCore通用数据操作类
  • java Web 辅助学习管理系统idea开发mysql数据库web结构java编程计算机网页源码maven项目
  • 使用Python实现K近邻算法
  • Celery的任务流
  • 使用Arcpy进行数据批处理-批量裁剪
  • 【攻防世界】ics-05
  • VTK的交互器
  • ChatGPT(3.5版本)开放无需注册:算力背后的数据之战悄然打响
  • python项目练习——14.学生管理系统
  • 基于SpringBoot的公益慈善平台
  • Python网络爬虫(一):HTML/CSS/JavaScript介绍
  • 机器学习每周挑战——旅游景点数据分析
  • 开发语言漫谈-C语言