*: make use of timecache

This commit is contained in:
Leo Balduf 2017-09-29 00:50:20 +02:00
parent 55b57549a6
commit 89bc479a3b
3 changed files with 7 additions and 54 deletions

View file

@ -18,6 +18,7 @@ import (
"github.com/chihaya/chihaya/frontend/udp/bytepool" "github.com/chihaya/chihaya/frontend/udp/bytepool"
"github.com/chihaya/chihaya/pkg/log" "github.com/chihaya/chihaya/pkg/log"
"github.com/chihaya/chihaya/pkg/stop" "github.com/chihaya/chihaya/pkg/stop"
"github.com/chihaya/chihaya/pkg/timecache"
) )
var allowedGeneratedPrivateKeyRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890") 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 // If this isn't requesting a new connection ID and the connection ID is
// invalid, then fail. // 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 err = errBadConnectionID
WriteError(w, txID, err) WriteError(w, txID, err)
return return
@ -272,7 +273,7 @@ func (t *Frontend) handleRequest(r Request, w ResponseWriter) (actionName string
return return
} }
WriteConnectionID(w, txID, NewConnectionID(r.IP, time.Now(), t.PrivateKey)) WriteConnectionID(w, txID, NewConnectionID(r.IP, timecache.Now(), t.PrivateKey))
case announceActionID, announceV6ActionID: case announceActionID, announceV6ActionID:
actionName = "announce" actionName = "announce"

View file

@ -7,13 +7,13 @@ import (
"net" "net"
"runtime" "runtime"
"sync" "sync"
"sync/atomic"
"time" "time"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"github.com/chihaya/chihaya/bittorrent" "github.com/chihaya/chihaya/bittorrent"
"github.com/chihaya/chihaya/pkg/log" "github.com/chihaya/chihaya/pkg/log"
"github.com/chihaya/chihaya/pkg/timecache"
"github.com/chihaya/chihaya/storage" "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. // Start a goroutine for reporting statistics to Prometheus.
ps.wg.Add(1) ps.wg.Add(1)
go func() { go func() {
@ -229,10 +213,6 @@ type peerStore struct {
cfg Config cfg Config
shards []*peerShard shards []*peerShard
// clock stores the current time nanoseconds, updated every second.
// Must be accessed atomically!
clock int64
closed chan struct{} closed chan struct{}
wg sync.WaitGroup wg sync.WaitGroup
} }
@ -263,11 +243,7 @@ func recordGCDuration(duration time.Duration) {
} }
func (ps *peerStore) getClock() int64 { func (ps *peerStore) getClock() int64 {
return atomic.LoadInt64(&ps.clock) return timecache.NowUnixNano()
}
func (ps *peerStore) setClock(to int64) {
atomic.StoreInt64(&ps.clock, to)
} }
func (ps *peerStore) shardIndex(infoHash bittorrent.InfoHash, af bittorrent.AddressFamily) uint32 { func (ps *peerStore) shardIndex(infoHash bittorrent.InfoHash, af bittorrent.AddressFamily) uint32 {

View file

@ -8,13 +8,13 @@ import (
"net" "net"
"runtime" "runtime"
"sync" "sync"
"sync/atomic"
"time" "time"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"github.com/chihaya/chihaya/bittorrent" "github.com/chihaya/chihaya/bittorrent"
"github.com/chihaya/chihaya/pkg/log" "github.com/chihaya/chihaya/pkg/log"
"github.com/chihaya/chihaya/pkg/timecache"
"github.com/chihaya/chihaya/storage" "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. // Start a goroutine for reporting statistics to Prometheus.
ps.wg.Add(1) ps.wg.Add(1)
go func() { go func() {
@ -269,10 +253,6 @@ type peerStore struct {
ipv6Mask net.IPMask ipv6Mask net.IPMask
shards []*peerShard shards []*peerShard
// clock stores the current time nanoseconds, updated every second.
// Must be accessed atomically!
clock int64
closed chan struct{} closed chan struct{}
wg sync.WaitGroup wg sync.WaitGroup
} }
@ -303,11 +283,7 @@ func recordGCDuration(duration time.Duration) {
} }
func (ps *peerStore) getClock() int64 { func (ps *peerStore) getClock() int64 {
return atomic.LoadInt64(&ps.clock) return timecache.NowUnixNano()
}
func (ps *peerStore) setClock(to int64) {
atomic.StoreInt64(&ps.clock, to)
} }
func (ps *peerStore) shardIndex(infoHash bittorrent.InfoHash, af bittorrent.AddressFamily) uint32 { func (ps *peerStore) shardIndex(infoHash bittorrent.InfoHash, af bittorrent.AddressFamily) uint32 {