mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 02:42:27 +00:00
p2p/discover: simplify rand timer
This commit is contained in:
parent
c1afb3c0cc
commit
7667ebfceb
2 changed files with 6 additions and 14 deletions
|
|
@ -104,20 +104,14 @@ type randomSource interface {
|
||||||
|
|
||||||
// reseedingRandom is a random number generator that tracks when it was last re-seeded.
|
// reseedingRandom is a random number generator that tracks when it was last re-seeded.
|
||||||
type reseedingRandom struct {
|
type reseedingRandom struct {
|
||||||
cur atomic.Pointer[rand.Rand]
|
cur atomic.Pointer[rand.Rand]
|
||||||
lastSeed mclock.AbsTime
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *reseedingRandom) nextReseedTime() mclock.AbsTime {
|
func (r *reseedingRandom) seed() {
|
||||||
return r.lastSeed.Add(10 * time.Minute)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *reseedingRandom) seed(now mclock.AbsTime) {
|
|
||||||
var b [8]byte
|
var b [8]byte
|
||||||
crand.Read(b[:])
|
crand.Read(b[:])
|
||||||
seed := binary.BigEndian.Uint64(b[:])
|
seed := binary.BigEndian.Uint64(b[:])
|
||||||
r.cur.Store(rand.New(rand.NewSource(int64(seed))))
|
r.cur.Store(rand.New(rand.NewSource(int64(seed))))
|
||||||
r.lastSeed = now
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *reseedingRandom) Intn(n int) int {
|
func (r *reseedingRandom) Intn(n int) int {
|
||||||
|
|
|
||||||
|
|
@ -135,7 +135,7 @@ func newTable(t transport, db *enode.DB, cfg Config) (*Table, error) {
|
||||||
ips: netutil.DistinctNetSet{Subnet: bucketSubnet, Limit: bucketIPLimit},
|
ips: netutil.DistinctNetSet{Subnet: bucketSubnet, Limit: bucketIPLimit},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
tab.rand.seed(cfg.Clock.Now())
|
tab.rand.seed()
|
||||||
tab.revalidation.init(&cfg)
|
tab.revalidation.init(&cfg)
|
||||||
|
|
||||||
// initial table content
|
// initial table content
|
||||||
|
|
@ -337,7 +337,7 @@ func (tab *Table) loop() {
|
||||||
refreshDone = make(chan struct{}) // where doRefresh reports completion
|
refreshDone = make(chan struct{}) // where doRefresh reports completion
|
||||||
waiting = []chan struct{}{tab.initDone} // holds waiting callers while doRefresh runs
|
waiting = []chan struct{}{tab.initDone} // holds waiting callers while doRefresh runs
|
||||||
revalTimer = mclock.NewAlarm(tab.cfg.Clock)
|
revalTimer = mclock.NewAlarm(tab.cfg.Clock)
|
||||||
reseedRandTimer = mclock.NewAlarm(tab.cfg.Clock)
|
reseedRandTimer = time.NewTicker(10 * time.Minute)
|
||||||
)
|
)
|
||||||
defer refresh.Stop()
|
defer refresh.Stop()
|
||||||
defer revalTimer.Stop()
|
defer revalTimer.Stop()
|
||||||
|
|
@ -351,11 +351,9 @@ loop:
|
||||||
nextTime := tab.revalidation.run(tab, tab.cfg.Clock.Now())
|
nextTime := tab.revalidation.run(tab, tab.cfg.Clock.Now())
|
||||||
revalTimer.Schedule(nextTime)
|
revalTimer.Schedule(nextTime)
|
||||||
|
|
||||||
reseedRandTimer.Schedule(tab.rand.nextReseedTime())
|
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-reseedRandTimer.C():
|
case <-reseedRandTimer.C:
|
||||||
tab.rand.seed(tab.cfg.Clock.Now())
|
tab.rand.seed()
|
||||||
|
|
||||||
case <-revalTimer.C():
|
case <-revalTimer.C():
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue