Code Monkey home page Code Monkey logo

rate-limiter's Introduction

基于redis的限流组件

支持两种使用方法:

  1. AOP注解拦截限流
@RateLimiter(key = "xxxx", max = 100, timeout = 1, timeUnit = TimeUnit.MINUTES)
  • key 限流key

  • max 限流最大值

  • timeout 限流时间

  • timeUnit 限流时间单位

    即在设置时间内,接口访问超过max次数时,会触发限流规则

  1. 自定义方法限流
boolean limited = rateLimiterUtils.shouldLimited("xxxx-1", 30, 1, TimeUnit.MINUTES);
if (limited) {
    return null;
}
  • 传参与注解一致
  • 返回值为true代表被限流
实现lua脚本
local key = KEYS[1]
local now = tonumber(ARGV[1])
local ttl = tonumber(ARGV[2])
local expired = tonumber(ARGV[3])
local max = tonumber(ARGV[4])

-- 清除过期的数据
redis.call('zremrangebyscore', key, 0, expired)

-- 获取 zset 中的当前元素个数
local current = tonumber(redis.call('zcard', key))
local next = current + 1

-- 如果当前个数大于限流最大值,则触发限流
if next > max then
  return 0;
else
  redis.call("zadd", key, now, now)
  -- 每次访问均重新设置 zset 的过期时间
  redis.call("pexpire", key, ttl)
  return next
end

rate-limiter's People

Watchers

James Cloos avatar Faylinn avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.