gpt4 book ai didi

go - 我必须发送数千条提醒,有什么办法可以避免每分钟都被提醒?

转载 作者:IT王子 更新时间:2023-10-29 01:56:41 26 4
gpt4 key购买 nike

我有一个像这样的结构:

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 .

  1. 确定下一个通知是什么
  2. 相应地设置或重置计时器
    1. 计时器触发后,转到 1
    2. 如果添加通知,转到1
    3. 如果通知被删除,转到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))
}

关于go - 我必须发送数千条提醒,有什么办法可以避免每分钟都被提醒?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53198618/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com