diff --git a/frontend/udp/frontend.go b/frontend/udp/frontend.go index 4ecbf85..acd77a3 100644 --- a/frontend/udp/frontend.go +++ b/frontend/udp/frontend.go @@ -18,6 +18,7 @@ import ( "github.com/chihaya/chihaya/frontend/udp/bytepool" "github.com/chihaya/chihaya/pkg/log" "github.com/chihaya/chihaya/pkg/stop" + "github.com/chihaya/chihaya/pkg/timecache" ) var allowedGeneratedPrivateKeyRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890") @@ -256,7 +257,7 @@ func (t *Frontend) handleRequest(r Request, w ResponseWriter) (actionName string // If this isn't requesting a new connection ID and the connection ID is // invalid, then fail. - if actionID != connectActionID && !ValidConnectionID(connID, r.IP, time.Now(), t.MaxClockSkew, t.PrivateKey) { + if actionID != connectActionID && !ValidConnectionID(connID, r.IP, timecache.Now(), t.MaxClockSkew, t.PrivateKey) { err = errBadConnectionID WriteError(w, txID, err) return @@ -272,7 +273,7 @@ func (t *Frontend) handleRequest(r Request, w ResponseWriter) (actionName string return } - WriteConnectionID(w, txID, NewConnectionID(r.IP, time.Now(), t.PrivateKey)) + WriteConnectionID(w, txID, NewConnectionID(r.IP, timecache.Now(), t.PrivateKey)) case announceActionID, announceV6ActionID: actionName = "announce" diff --git a/storage/memory/peer_store.go b/storage/memory/peer_store.go index f2a21bb..073671b 100644 --- a/storage/memory/peer_store.go +++ b/storage/memory/peer_store.go @@ -7,13 +7,13 @@ import ( "net" "runtime" "sync" - "sync/atomic" "time" "gopkg.in/yaml.v2" "github.com/chihaya/chihaya/bittorrent" "github.com/chihaya/chihaya/pkg/log" + "github.com/chihaya/chihaya/pkg/timecache" "github.com/chihaya/chihaya/storage" ) @@ -146,22 +146,6 @@ func New(provided Config) (storage.PeerStore, error) { } }() - // Start a goroutine for updating our cached system clock. - ps.wg.Add(1) - go func() { - defer ps.wg.Done() - t := time.NewTicker(1 * time.Second) - for { - select { - case <-ps.closed: - t.Stop() - return - case now := <-t.C: - ps.setClock(now.UnixNano()) - } - } - }() - // Start a goroutine for reporting statistics to Prometheus. ps.wg.Add(1) go func() { @@ -229,10 +213,6 @@ type peerStore struct { cfg Config shards []*peerShard - // clock stores the current time nanoseconds, updated every second. - // Must be accessed atomically! - clock int64 - closed chan struct{} wg sync.WaitGroup } @@ -263,11 +243,7 @@ func recordGCDuration(duration time.Duration) { } func (ps *peerStore) getClock() int64 { - return atomic.LoadInt64(&ps.clock) -} - -func (ps *peerStore) setClock(to int64) { - atomic.StoreInt64(&ps.clock, to) + return timecache.NowUnixNano() } func (ps *peerStore) shardIndex(infoHash bittorrent.InfoHash, af bittorrent.AddressFamily) uint32 { diff --git a/storage/memorybysubnet/peer_store.go b/storage/memorybysubnet/peer_store.go index 5bff7b5..206074e 100644 --- a/storage/memorybysubnet/peer_store.go +++ b/storage/memorybysubnet/peer_store.go @@ -8,13 +8,13 @@ import ( "net" "runtime" "sync" - "sync/atomic" "time" "gopkg.in/yaml.v2" "github.com/chihaya/chihaya/bittorrent" "github.com/chihaya/chihaya/pkg/log" + "github.com/chihaya/chihaya/pkg/timecache" "github.com/chihaya/chihaya/storage" ) @@ -155,22 +155,6 @@ func New(provided Config) (storage.PeerStore, error) { } }() - // Start a goroutine for updating our cached system clock. - ps.wg.Add(1) - go func() { - defer ps.wg.Done() - t := time.NewTicker(1 * time.Second) - for { - select { - case <-ps.closed: - t.Stop() - return - case now := <-t.C: - ps.setClock(now.UnixNano()) - } - } - }() - // Start a goroutine for reporting statistics to Prometheus. ps.wg.Add(1) go func() { @@ -269,10 +253,6 @@ type peerStore struct { ipv6Mask net.IPMask shards []*peerShard - // clock stores the current time nanoseconds, updated every second. - // Must be accessed atomically! - clock int64 - closed chan struct{} wg sync.WaitGroup } @@ -303,11 +283,7 @@ func recordGCDuration(duration time.Duration) { } func (ps *peerStore) getClock() int64 { - return atomic.LoadInt64(&ps.clock) -} - -func (ps *peerStore) setClock(to int64) { - atomic.StoreInt64(&ps.clock, to) + return timecache.NowUnixNano() } func (ps *peerStore) shardIndex(infoHash bittorrent.InfoHash, af bittorrent.AddressFamily) uint32 {