我必须发送数千条提醒,有什么办法可以避免每分钟都被提醒?
I have to send out thousands of reminders, any way to avoid a ticker every minute?
我有一个像这样的结构:
type Notifications struct {
Id int
Start *time.Time
}
notifications := db.GetNotifications()
所以现在我需要在时间与当前时间匹配时发送这些通知。
1 2018-11-07 09:05:00
2 2018-11-07 09:05:00
3 2018-11-07 09:15:00
..
对我来说最简单的方法是使用自动收报机:
ticker := time.NewTicker(30 * time.Second)
defer ticker.Stop()
for {
<-ticker.C
alerts := []Notification
for _, n := range notifications {
if n.Start == // same year, month, day, hour and minute {
alerts = append(alerts, n)
}
}
sendNotifications(alerts)
// TODO mutate the notifications to avoid duplicatation sending
}
有没有更有效的方法来做到这一点?
匹配时间的最佳方法是什么,我是否必须在我的 if 语句中分别比较 time.Now() 的属性,如年、月、日、小时和分钟?即,如果达到年、月、日、小时和分钟(秒及以后将被忽略),则会触发通知
首先,要比较时间值,请使用 Time.Equal, Time.Before, and 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()) // false
fmt.Println(t2.Equal(t1)) // true
https://play.golang.org/p/06RcvuI_1Ha
对于调度问题,我会使用 time.Timer。
- 找出下一个通知
- 相应地设置或重置计时器
- 计时器触发后,转到 1
- 如果添加通知,转到1
- 如果通知被删除,转到1
这是一个草图:
package main
import "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))
}
我有一个像这样的结构:
type Notifications struct {
Id int
Start *time.Time
}
notifications := db.GetNotifications()
所以现在我需要在时间与当前时间匹配时发送这些通知。
1 2018-11-07 09:05:00
2 2018-11-07 09:05:00
3 2018-11-07 09:15:00
..
对我来说最简单的方法是使用自动收报机:
ticker := time.NewTicker(30 * time.Second)
defer ticker.Stop()
for {
<-ticker.C
alerts := []Notification
for _, n := range notifications {
if n.Start == // same year, month, day, hour and minute {
alerts = append(alerts, n)
}
}
sendNotifications(alerts)
// TODO mutate the notifications to avoid duplicatation sending
}
有没有更有效的方法来做到这一点?
匹配时间的最佳方法是什么,我是否必须在我的 if 语句中分别比较 time.Now() 的属性,如年、月、日、小时和分钟?即,如果达到年、月、日、小时和分钟(秒及以后将被忽略),则会触发通知
首先,要比较时间值,请使用 Time.Equal, Time.Before, and 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()) // false
fmt.Println(t2.Equal(t1)) // true
https://play.golang.org/p/06RcvuI_1Ha
对于调度问题,我会使用 time.Timer。
- 找出下一个通知
- 相应地设置或重置计时器
- 计时器触发后,转到 1
- 如果添加通知,转到1
- 如果通知被删除,转到1
这是一个草图:
package main
import "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))
}