mclock.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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.
  29. func (t AbsTime) Add(d time.Duration) AbsTime {
  30. return t + AbsTime(d)
  31. }
  32. // Clock interface makes it possible to replace the monotonic system clock with
  33. // a simulated clock.
  34. type Clock interface {
  35. Now() AbsTime
  36. Sleep(time.Duration)
  37. After(time.Duration) <-chan time.Time
  38. }
  39. // System implements Clock using the system clock.
  40. type System struct{}
  41. // Now implements Clock.
  42. func (System) Now() AbsTime {
  43. return AbsTime(monotime.Now())
  44. }
  45. // Sleep implements Clock.
  46. func (System) Sleep(d time.Duration) {
  47. time.Sleep(d)
  48. }
  49. // After implements Clock.
  50. func (System) After(d time.Duration) <-chan time.Time {
  51. return time.After(d)
  52. }