mclock.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright 2016 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. // Package mclock is a wrapper for a monotonic clock source
  17. package mclock
  18. import (
  19. "time"
  20. "github.com/aristanetworks/goarista/monotime"
  21. )
  22. // AbsTime represents absolute monotonic time.
  23. type AbsTime time.Duration
  24. // Now returns the current absolute monotonic time.
  25. func Now() AbsTime {
  26. return AbsTime(monotime.Now())
  27. }
  28. // Add returns t + d as absolute time.
  29. func (t AbsTime) Add(d time.Duration) AbsTime {
  30. return t + AbsTime(d)
  31. }
  32. // Sub returns t - t2 as a duration.
  33. func (t AbsTime) Sub(t2 AbsTime) time.Duration {
  34. return time.Duration(t - t2)
  35. }
  36. // The Clock interface makes it possible to replace the monotonic system clock with
  37. // a simulated clock.
  38. type Clock interface {
  39. Now() AbsTime
  40. Sleep(time.Duration)
  41. NewTimer(time.Duration) ChanTimer
  42. After(time.Duration) <-chan AbsTime
  43. AfterFunc(d time.Duration, f func()) Timer
  44. }
  45. // Timer is a cancellable event created by AfterFunc.
  46. type Timer interface {
  47. // Stop cancels the timer. It returns false if the timer has already
  48. // expired or been stopped.
  49. Stop() bool
  50. }
  51. // ChanTimer is a cancellable event created by NewTimer.
  52. type ChanTimer interface {
  53. Timer
  54. // The channel returned by C receives a value when the timer expires.
  55. C() <-chan AbsTime
  56. // Reset reschedules the timer with a new timeout.
  57. // It should be invoked only on stopped or expired timers with drained channels.
  58. Reset(time.Duration)
  59. }
  60. // System implements Clock using the system clock.
  61. type System struct{}
  62. // Now returns the current monotonic time.
  63. func (c System) Now() AbsTime {
  64. return AbsTime(monotime.Now())
  65. }
  66. // Sleep blocks for the given duration.
  67. func (c System) Sleep(d time.Duration) {
  68. time.Sleep(d)
  69. }
  70. // NewTimer creates a timer which can be rescheduled.
  71. func (c System) NewTimer(d time.Duration) ChanTimer {
  72. ch := make(chan AbsTime, 1)
  73. t := time.AfterFunc(d, func() {
  74. // This send is non-blocking because that's how time.Timer
  75. // behaves. It doesn't matter in the happy case, but does
  76. // when Reset is misused.
  77. select {
  78. case ch <- c.Now():
  79. default:
  80. }
  81. })
  82. return &systemTimer{t, ch}
  83. }
  84. // After returns a channel which receives the current time after d has elapsed.
  85. func (c System) After(d time.Duration) <-chan AbsTime {
  86. ch := make(chan AbsTime, 1)
  87. time.AfterFunc(d, func() { ch <- c.Now() })
  88. return ch
  89. }
  90. // AfterFunc runs f on a new goroutine after the duration has elapsed.
  91. func (c System) AfterFunc(d time.Duration, f func()) Timer {
  92. return time.AfterFunc(d, f)
  93. }
  94. type systemTimer struct {
  95. *time.Timer
  96. ch <-chan AbsTime
  97. }
  98. func (st *systemTimer) Reset(d time.Duration) {
  99. st.Timer.Reset(d)
  100. }
  101. func (st *systemTimer) C() <-chan AbsTime {
  102. return st.ch
  103. }