package store import ( "context" "time" ) // ClaimNotice claims the sending of a notification: true when nobody has // sent the same one within the window, and from now on, nobody else may // until it has passed. One statement, so two control planes asking at once // cannot both be told yes. func (s *DB) ClaimNotice(ctx context.Context, key string, at time.Time, window time.Duration) (bool, error) { now := at.UnixMilli() res, err := s.db.ExecContext(ctx, `INSERT INTO notify_sent (key, sent_at) VALUES (?, ?) ON CONFLICT (key) DO UPDATE SET sent_at = excluded.sent_at WHERE notify_sent.sent_at < ?`, key, now, now-window.Milliseconds()) if err != nil { return false, err } n, err := res.RowsAffected() return n == 1, err }