作者热门文章
- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我有一个像这样的结构:
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 , 和 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 .
这是一个草图:
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))
}
关于go - 我必须发送数千条提醒,有什么办法可以避免每分钟都被提醒?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53198618/
我是一名优秀的程序员,十分优秀!