// ReserveN使用 // Usage example: // r := lim.ReserveN(time.Now(), 1) // if !r.OK() { // // Not allowed to act! Did you remember to set lim.burst to be > 0 ? // return // } // time.Sleep(r.Delay()) // Act()
三种方法的使用场景:
如果不想 drop event,可以使用 Reserve,根据返回的Reservation等待一定时间后执行
如果需要遵守最后期限或取消延迟,使用 Wait
如果可以允许丢弃或者忽略超过速率的事件,使用 Allow
实现原理
令牌桶原理图
看起来是有一个goroutine定时放一个队列中放入令牌,我最开始也是这样认为的,这样的好处是比较简单直观. 在 go 中是采用lazyload的方式实现的,通过记录最近一次消费时间跟当前时间进行对比更新Token数目,同时Token数目也不是使用队列,而是通过计数来实现,节省内存.
tcp_keepalive_intvl (integer; default: 75; since Linux 2.4) The number of seconds between TCP keep-alive probes.
tcp_keepalive_probes (integer; default: 9; since Linux 2.2) The maximum number of TCP keep-alive probes to send before giving up and killing the connection if no response is obtained from the other end.
tcp_keepalive_time (integer; default: 7200; since Linux 2.2) The number of seconds a connection needs to be idle before TCP begins sending out keep-alive probes. Keep-alives are sent only when the SO_KEEPALIVE socket option is enabled. The default value is 7200 seconds (2 hours). An idle connection is terminated after approximately an additional 11 minutes (9 probes an interval of 75 seconds apart) when keep-alive is enabled.
Note that underlying connection tracking mechanisms and application timeouts may be much shorter.
Elastic Load Balancing sets the idle timeout value for TCP flows to 350 seconds. You cannot modify this value. Clients or targets can use TCP keepalive packets to reset the idle timeout.