clock.go 718 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package core
  2. import (
  3. "sync"
  4. "time"
  5. )
  6. // Clock is a V2Ray feature that returns current time.
  7. type Clock interface {
  8. Feature
  9. // Now returns current time.
  10. Now() time.Time
  11. }
  12. type syncClock struct {
  13. sync.RWMutex
  14. Clock
  15. }
  16. func (c *syncClock) Now() time.Time {
  17. c.RLock()
  18. defer c.RUnlock()
  19. if c.Clock == nil {
  20. return time.Now()
  21. }
  22. return c.Clock.Now()
  23. }
  24. func (c *syncClock) Start() error {
  25. c.RLock()
  26. defer c.RUnlock()
  27. if c.Clock == nil {
  28. return nil
  29. }
  30. return c.Clock.Start()
  31. }
  32. func (c *syncClock) Close() error {
  33. c.RLock()
  34. defer c.RUnlock()
  35. if c.Clock == nil {
  36. return nil
  37. }
  38. return c.Clock.Close()
  39. }
  40. func (c *syncClock) Set(clock Clock) {
  41. c.Lock()
  42. defer c.Unlock()
  43. c.Clock = clock
  44. }