mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-09-27 14:26:11 -04:00
injectable clock
This commit is contained in:
parent
c3dce11c4e
commit
9cd0e90e99
59
clock.go
Normal file
59
clock.go
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
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
|
||||||
|
}
|
6
v2ray.go
6
v2ray.go
@ -27,6 +27,7 @@ type Instance struct {
|
|||||||
router syncRouter
|
router syncRouter
|
||||||
ihm syncInboundHandlerManager
|
ihm syncInboundHandlerManager
|
||||||
ohm syncOutboundHandlerManager
|
ohm syncOutboundHandlerManager
|
||||||
|
clock syncClock
|
||||||
|
|
||||||
features []Feature
|
features []Feature
|
||||||
id uuid.UUID
|
id uuid.UUID
|
||||||
@ -163,3 +164,8 @@ func (s *Instance) InboundHandlerManager() InboundHandlerManager {
|
|||||||
func (s *Instance) OutboundHandlerManager() OutboundHandlerManager {
|
func (s *Instance) OutboundHandlerManager() OutboundHandlerManager {
|
||||||
return &(s.ohm)
|
return &(s.ohm)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Clock returns the Clock used by this Instance. The returned Clock is always functional.
|
||||||
|
func (s *Instance) Clock() Clock {
|
||||||
|
return &(s.clock)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user