[NOD-114] On start, DNSSeeder should update its addresses more frequently (#250)

* [NOD-114] Added a minimum address amount GetAddrs.

* [NOD-114] Added smallNetwork intervals for when the network is small.

* [NOD-114] Fixed bad minimum address calculation.
This commit is contained in:
stasatdaglabs 2019-04-15 15:57:43 +03:00 committed by Ori Newman
parent 7353a49469
commit c5827febf7
3 changed files with 48 additions and 5 deletions

View File

@ -160,6 +160,10 @@ const (
// will consider evicting an address.
minBadDays = 7
// getAddrMin is the least addresses that we will send in response
// to a getAddr. If we have less than this amount, we send everything.
getAddrMin = 50
// getAddrMax is the most addresses that we will send in response
// to a getAddr (in practise the most addresses we will return from a
// call to AddressCache()).
@ -844,6 +848,12 @@ func (a *AddrManager) AddressCache(includeAllSubnetworks bool, subnetworkID *sub
if numAddresses > getAddrMax {
numAddresses = getAddrMax
}
if len(allAddr) < getAddrMin {
numAddresses = len(allAddr)
}
if len(allAddr) > getAddrMin && numAddresses < getAddrMin {
numAddresses = getAddrMin
}
// Fisher-Yates shuffle the array. We only need to do the first
// `numAddresses' since we are throwing the rest.

View File

@ -92,8 +92,9 @@ func creep() {
peers = amgr.Addresses()
}
if len(peers) == 0 {
log.Printf("No stale addresses -- sleeping for 10 minutes")
for i := 0; i < 600; i++ {
creepIntervalInSeconds := int(amgr.creepInterval().Seconds())
log.Printf("No stale addresses -- sleeping for %d seconds", creepIntervalInSeconds)
for i := 0; i < creepIntervalInSeconds; i++ {
time.Sleep(time.Second)
if atomic.LoadInt32(&systemShutdown) != 0 {
log.Printf("Creep thread shutdown")

View File

@ -45,6 +45,10 @@ const (
// stale.
defaultStaleTimeout = time.Hour
// smallNetworkStaleTimeout is the time in which a host is considered
// stale if the network is small.
smallNetworkStaleTimeout = time.Second * 30
// dumpAddressInterval is the interval used to dump the address
// cache to disk for future use.
dumpAddressInterval = time.Second * 30
@ -59,6 +63,18 @@ const (
// pruneExpireTimeout is the expire time in which a node is
// considered dead.
pruneExpireTimeout = time.Hour * 8
// defaultCreepInterval is the interval between creep runs.
defaultCreepInterval = time.Minute * 10
// defaultCreepInterval is the interval between creep runs if the
// network is small.
smallNetworkCreepInterval = time.Second * 30
// smallNetworkNodeAmount is amount of nodes under which a network
// is considered small. A small network has shorter timeouts and
// intervals to encourage faster network growth.
smallNetworkNodeAmount = 50
)
var (
@ -181,8 +197,8 @@ func (m *Manager) Addresses() []*wire.NetAddress {
if i == 0 {
break
}
if now.Sub(node.LastSuccess) < defaultStaleTimeout ||
now.Sub(node.LastAttempt) < defaultStaleTimeout {
if now.Sub(node.LastSuccess) < m.staleTimeout() ||
now.Sub(node.LastAttempt) < m.staleTimeout() {
continue
}
addrs = append(addrs, node.Addr)
@ -198,6 +214,22 @@ func (m *Manager) AddressCount() int {
return len(m.nodes)
}
func (m *Manager) staleTimeout() time.Duration {
if m.AddressCount() < smallNetworkNodeAmount {
return smallNetworkStaleTimeout
}
return defaultStaleTimeout
}
func (m *Manager) creepInterval() time.Duration {
if m.AddressCount() < smallNetworkNodeAmount {
return smallNetworkCreepInterval
}
return defaultCreepInterval
}
// GoodAddresses returns good working IPs that match both the
// passed DNS query type and have the requested services.
func (m *Manager) GoodAddresses(qtype uint16, services wire.ServiceFlag, includeAllSubnetworks bool, subnetworkID *subnetworkid.SubnetworkID) []*wire.NetAddress {
@ -230,7 +262,7 @@ func (m *Manager) GoodAddresses(qtype uint16, services wire.ServiceFlag, include
}
if node.LastSuccess.IsZero() ||
now.Sub(node.LastSuccess) > defaultStaleTimeout {
now.Sub(node.LastSuccess) > m.staleTimeout() {
continue
}