我必须发出数以千计的提醒,有什么办法避免每分钟发出响声吗?

我必须发出数以千计的提醒,有什么办法避免每分钟发出响声吗?,第1张

我必须发出数以千计的提醒,有什么办法避免每分钟发出响声吗?

第一件事首先,比较时间值,使用Time.Equal,Time.Before和time.After方法。比较各个组件根本不可靠:

newYork, _ := time.LoadLocation("America/New_York")t1 := time.Date(2018, 11, 8, 4, 0, 0, 0, time.UTC)t2 := t1.In(newYork)fmt.Printf("%v == %v?n", t1, t2) // 2018-11-08 04:00:00 +0000 UTC == 2018-11-07 23:00:00 -0500 EST?fmt.Println(t1.Day() == t2.Day()) // falsefmt.Println(t2.Equal(t1))         // true

https://play.golang.org/p/06RcvuI_1Ha


对于计划问题,我将使用time.Timer。

  1. 找出接下来要发出的通知
  2. 相应地设置或重置计时器
    1. 计时器触发后,转到1
    2. 如果添加了通知,请转到1
    3. 如果删除通知,则转到1

这是一个草图:

package mainimport "time"func main() {    t := time.NewTimer(0)    go func() {        for range t.C { nextTwo := db.GetNextNotifications(2) // Sanity check if time.Until(nextTwo[0].Start) > 1*time.Second {     // The timer went off early. Perhaps the notification has been     // deleted?     t.Reset(time.Until(nextTwo[0].Start))     continue } go send(nextTwo[0]) t.Reset(time.Until(nextTwo[1].Start))        }    }()    resetTimer(t) // call as required whenever a notification is added or removed}func resetTimer(t *time.Timer) {    next := db.GetNextNotification()    t.Reset(time.Until(next.Start))}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5028794.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-15
下一篇 2022-11-15

发表评论

登录后才能评论

评论列表(0条)

保存