V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
douyacun

go time/rate 实现 接口 - IP/账户 限流

  •  
  •   douyacun ·
    douyacun · Apr 1, 2021 · 2396 views
    This topic created in 1854 days ago, the information mentioned may be changed or developed.

    原文链接: https://www.douyacun.com/article/f8f4e2bb806a6176e9c61dcaa0a4a6cc

    背景:

    • gin 框架,中间件
    • 博客开放好玩的 API

    需求:

    • 接口限流
    • 按账户 /IP/Token 限流
    • 定时加载配置

    todo:

    • 分布式限流

    整理了一下 time/rate 实现原理,简化公式:

    tokens (当前可用令牌数) := (now (当前时间) - last(上一次消耗令牌的时间) ).Seconds() * limit(令牌 /每秒)

    4 replies    2021-04-02 23:14:42 +08:00
    Carseason
        1
    Carseason  
       Apr 1, 2021
    赞一个
    douyacun
        2
    douyacun  
    OP
       Apr 1, 2021
    @Carseason 感激啊,我这也是憋了好几天才实现
    p0h5
        3
    p0h5  
       Apr 1, 2021 via iPhone
    赞,可以作中间件发布到 github
    douyacun
        4
    douyacun  
    OP
       Apr 2, 2021
    @p0h5 可以,我整理一下发 github,回头这个帖子下追更
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5122 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 09:40 · PVG 17:40 · LAX 02:40 · JFK 05:40
    ♥ Do have faith in what you're doing.