关于time.Ticker的一个问题

kiwi-yan · 2020-04-24 22:06:50 · 1131 次点击 · 大约8小时之前 开始浏览    置顶
这是一个创建于 2020-04-24 22:06:50 的主题,其中的信息可能已经有所发展或是发生改变。

我们线上有个服务大概有个这样一段代码跑着

func SyncData() {
    t := time.NewTicker(time.Minute * 2)
    for {
        syncAllInfo()
        <-t.C
    }
}

定时更新数据,每2分钟一次; 预计未来随着数据的增多,syncAllInfo()执行可能会超过2分钟,如果syncAllInfo()执行时间过长,长期下来会怎么样?

求各位大佬解答

我知道同步数据间隔可以调大一些,但是我想弄清楚如果不调会怎么样?


有疑问加站长微信联系(非本文作者)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

1131 次点击  
加入收藏 微博
2 回复  |  直到 2020-04-26 09:48:53
colinrs
colinrs · #1 · 5年之前

godoc.org: 里面写到

NewTicker returns a new Ticker containing a channel that will send the time with a period specified by the duration argument. It adjusts the intervals or drops ticks to make up for slow receivers. The duration d must be greater than zero; if not, NewTicker will panic. Stop the ticker to release associated resources.

丢弃tick信息以适应反应慢的接收者, 你的情况应该影响不大吧

kiwi-yan
kiwi-yan · #2 · 5年之前
colinrscolinrs #1 回复

[godoc.org][1]: 里面写到 NewTicker returns a new Ticker containing a channel that will send the time with a period specified by the duration argument. It adjusts the intervals or **drops ticks to make up for slow receivers**. The duration d must be greater than zero; if not, NewTicker will panic. Stop the ticker to release associated resources. 丢弃tick信息以适应反应慢的接收者, 你的情况应该影响不大吧 [1]: https://godoc.org/time#Ticker

赞,因为粗略看了下源码,往发送ticker.C发送消息是开了goroutine去发的,然后ticker.C的大小是1,如果处理不及时的话,担心发生goroutine泄露;如果官方文档这么说的话就放心了,看来源码还是得仔细看看啊:)

添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传