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

时间:2018-11-07 22:08:50

标签: go goroutine

我有一个类似的结构:

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
}

是否有更有效的方法来做到这一点?

在时间上进行匹配的最佳方法是什么,我是否必须比较time.Now()的if语句中的属性,例如年,月,日,小时和分钟,分别?即,如果达到年,月,日,时和分(秒和以后的时间将被忽略),则会触发通知

1 个答案:

答案 0 :(得分:3)

首先,要比较时间值,请使用Time.EqualTime.Beforetime.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))
}