V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
douyacun
V2EX  ›  Go 编程语言

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

  •  
  •   douyacun ·
    douyacun · 2021-04-01 11:22:33 +08:00 · 1516 次点击
    这是一个创建于 1092 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    背景:

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

    需求:

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

    todo:

    • 分布式限流

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

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

    4 条回复    2021-04-02 23:14:42 +08:00
    Carseason
        1
    Carseason  
       2021-04-01 11:37:29 +08:00
    赞一个
    douyacun
        2
    douyacun  
    OP
       2021-04-01 11:39:54 +08:00
    @Carseason 感激啊,我这也是憋了好几天才实现
    p0h5
        3
    p0h5  
       2021-04-01 23:15:14 +08:00 via iPhone
    赞,可以作中间件发布到 github
    douyacun
        4
    douyacun  
    OP
       2021-04-02 23:14:42 +08:00
    @p0h5 可以,我整理一下发 github,回头这个帖子下追更
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3229 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 11:42 · PVG 19:42 · LAX 04:42 · JFK 07:42
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.