Git Product home page Git Product logo

ratelimit's Introduction

ratelimit

基于漏斗算法的限流组件

令牌桶算法

令牌桶算法是一个存放固定容量令牌(token)的桶,按照固定速率往桶里添加令牌。令牌桶算法基本可以用下面的几个概念来描述: • 令牌将按照固定的速率被放入令牌桶中。比如每秒放10个。 • 桶中最多存放b个令牌,当桶满时,新添加的令牌被丢弃或拒绝。 • 当一个n个字节大小的数据包到达,将从桶中删除n个令牌,接着数据包被发送到网络上。 • 如果桶中的令牌不足n个,则不会删除令牌,且该数据包将被限流(要么丢弃,要么缓冲区等待)。

令牌算法是根据放令牌的速率去控制输出的速率,也就是to network的速率。to network我们可以理解为消息的处理程序,执行某段业务或者调用某个RPC。 使用guava的ratelimiter来实现。

实时更新

使用阿里开源nacos作为配置中心

全局流控

在集群环境中,针对整个集群进行全局流控更为合理。采用令牌桶算法,将中间状态全部存在redis中,通过redis-lua去访问判断保证性能。

使用方式

启动nacos

配置参考如下

{
    "test":[
        {
            "permitsPerSecond":1,
            "warmupPeriod":2,
            "sync":false,
            "timeout":1000,
            "sort": 0,
            "model":"standalone"
        },{
            "permitsPerSecond":1,
            "model":"global",
            "paramNames":["user"],
            "sort":1
        }
    ]
}

使用参考demo

待优化问题

  1. 未提供配置中心的扩展接口,暂时只支持nacos
  2. 未提供自定义参数返回配置

ratelimit's People

Contributors

miraclecczs avatar

Watchers

James Cloos 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.