1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 10:08:15 -05:00
v2fly/clock.go
2018-01-23 23:12:02 +01:00

60 lines
701 B
Go

package core
import (
"sync"
"time"
)
// Clock is a V2Ray feature that returns current time.
type Clock interface {
Feature
// Now returns current time.
Now() time.Time
}
type syncClock struct {
sync.RWMutex
Clock
}
func (c *syncClock) Now() time.Time {
c.RLock()
defer c.RUnlock()
if c.Clock == nil {
return time.Now()
}
return c.Clock.Now()
}
func (c *syncClock) Start() error {
c.RLock()
defer c.RUnlock()
if c.Clock == nil {
return nil
}
return c.Clock.Start()
}
func (c *syncClock) Close() {
c.RLock()
defer c.RUnlock()
if c.Clock == nil {
return
}
c.Clock.Close()
}
func (c *syncClock) Set(clock Clock) {
c.Lock()
defer c.Unlock()
c.Clock = clock
}