ticker.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package alerting
  15. import (
  16. "time"
  17. "github.com/benbjohnson/clock"
  18. )
  19. // Ticker is a ticker to power the alerting scheduler. it's like a time.Ticker, except:
  20. // - it doesn't drop ticks for slow receivers, rather, it queues up. so that callers are in control to instrument what's going on.
  21. // - it automatically ticks every second, which is the right thing in our current design
  22. // - it ticks on second marks or very shortly after. this provides a predictable load pattern
  23. // (this shouldn't cause too much load contention issues because the next steps in the pipeline just process at their own pace)
  24. // - the timestamps are used to mark "last datapoint to query for" and as such, are a configurable amount of seconds in the past
  25. // - because we want to allow:
  26. // - a clean "resume where we left off" and "don't yield ticks we already did"
  27. // - adjusting offset over time to compensate for storage backing up or getting fast and providing lower latency
  28. // you specify a lastProcessed timestamp as well as an offset at creation, or runtime
  29. type Ticker struct {
  30. C chan time.Time
  31. clock clock.Clock
  32. last time.Time
  33. offset time.Duration
  34. newOffset chan time.Duration
  35. }
  36. // NewTicker returns a ticker that ticks on second marks or very shortly after, and never drops ticks
  37. func NewTicker(last time.Time, initialOffset time.Duration, c clock.Clock) *Ticker {
  38. t := &Ticker{
  39. C: make(chan time.Time),
  40. clock: c,
  41. last: last,
  42. offset: initialOffset,
  43. newOffset: make(chan time.Duration),
  44. }
  45. go t.run()
  46. return t
  47. }
  48. func (t *Ticker) run() {
  49. for {
  50. next := t.last.Add(time.Duration(1) * time.Second)
  51. diff := t.clock.Now().Add(-t.offset).Sub(next)
  52. if diff >= 0 {
  53. t.C <- next
  54. t.last = next
  55. continue
  56. }
  57. // tick is too young. try again when ...
  58. select {
  59. case <-t.clock.After(-diff): // ...it'll definitely be old enough
  60. case offset := <-t.newOffset: // ...it might be old enough
  61. t.offset = offset
  62. }
  63. }
  64. }