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

uber-go 中的 ratelimit 包, 为何 sleepFor 不能小于 maxSlack 呢?

  •  
  •   RedisMasterNode · 2021-01-24 19:05:22 +08:00 · 865 次点击
    这是一个创建于 1160 天前的主题,其中的信息可能已经有所发展或是发生改变。

    Take方法见: https://github.com/uber-go/ratelimit/blob/master/limiter_atomic.go

    其中有一段代码:

    ...
    		if newState.sleepFor < t.maxSlack {
    			newState.sleepFor = t.maxSlack
    		}
    ...
    

    sleepFor是“间隔时间-上次执行时间至当前时间”, 例如间隔时间为 1 秒, 上次执行时间为 5 秒前, sleepFor 就是-4

    sleepFor在末尾用于t.clock.Sleep(newState.sleepFor),貌似没有别的用途,那为什么需要让它不能小于t.maxSlack

    或者如果有 dalao 明白这段注释的意思也可以解释一下:

    ...
    		// We shouldn't allow sleepFor to get too negative, since it would mean that
    		// a service that slowed down a lot for a short period of time would get
    		// a much higher RPS following that.
    ...
    

    萌新提问, 感谢~

    2 条回复    2021-01-28 11:45:03 +08:00
    zhangsanfeng2012
        1
    zhangsanfeng2012  
       2021-01-25 09:40:57 +08:00
    限制突发吧,我猜的
    RedisMasterNode
        2
    RedisMasterNode  
    OP
       2021-01-28 11:45:03 +08:00
    @zhangsanfeng2012 道理都懂,可是代码里面明明没有在用
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2850 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 11:42 · PVG 19:42 · LAX 04:42 · JFK 07:42
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.