Lookup each DNS seed in its own go routine.

By putting each DNS seed in its own go routine, btcd can start connecting
to nodes as they are found instead of waiting for all seeds to respond.  This
significantly speeds up startup time.

Additionally, logging was added to show how many addresses were fetched from
each seed.
This commit is contained in:
David Hill 2014-07-04 14:04:01 -04:00
parent 2e029b1c3d
commit ebc5db2710
2 changed files with 39 additions and 37 deletions

View file

@ -125,17 +125,12 @@ func torLookupIP(host, proxy string) ([]net.IP, error) {
// dnsDiscover looks up the list of peers resolved by DNS for all hosts in // dnsDiscover looks up the list of peers resolved by DNS for all hosts in
// seeders. If proxy is not "" then it is used as a tor proxy for the // seeders. If proxy is not "" then it is used as a tor proxy for the
// resolution. If any errors occur then the seeder that errored will not have // resolution.
// any hosts in the list. Therefore if all hosts failed an empty slice of func dnsDiscover(seeder string) ([]net.IP, error) {
// strings will be returned.
func dnsDiscover(seeder string) []net.IP {
discLog.Debugf("Fetching list of seeds from %v", seeder)
peers, err := btcdLookup(seeder) peers, err := btcdLookup(seeder)
if err != nil { if err != nil {
discLog.Debugf("Unable to fetch dns seeds from %s: %v", return nil, err
seeder, err)
return []net.IP{}
} }
return peers return peers, nil
} }

View file

@ -474,9 +474,18 @@ func (s *server) seedFromDNS() {
} }
for _, seeder := range activeNetParams.dnsSeeds { for _, seeder := range activeNetParams.dnsSeeds {
seedpeers := dnsDiscover(seeder) go func(seeder string) {
if len(seedpeers) == 0 { seedpeers, err := dnsDiscover(seeder)
continue if err != nil {
discLog.Infof("DNS discovery failed on seed %s: %v", seeder, err)
return
}
numPeers := len(seedpeers)
discLog.Infof("%d addresses found from DNS seed %s", numPeers, seeder)
if numPeers == 0 {
return
} }
addresses := make([]*btcwire.NetAddress, len(seedpeers)) addresses := make([]*btcwire.NetAddress, len(seedpeers))
// if this errors then we have *real* problems // if this errors then we have *real* problems
@ -498,9 +507,8 @@ func (s *server) seedFromDNS() {
// to replicate this behaviour we put all addresses as // to replicate this behaviour we put all addresses as
// having come from the first one. // having come from the first one.
s.addrManager.AddAddresses(addresses, addresses[0]) s.addrManager.AddAddresses(addresses, addresses[0])
}(seeder)
} }
// XXX if this is empty do we want to use hardcoded
// XXX peers like bitcoind does?
} }
// peerHandler is used to handle peer operations such as adding and removing // peerHandler is used to handle peer operations such as adding and removing
@ -617,10 +625,9 @@ out:
// Just check that we don't already have an address // Just check that we don't already have an address
// in the same group so that we are not connecting // in the same group so that we are not connecting
// to the same network segment at the expense of // to the same network segment at the expense of
// others. bitcoind breaks out of the loop here, but // others.
// we continue to try other addresses.
if state.outboundGroups[key] != 0 { if state.outboundGroups[key] != 0 {
continue break
} }
tries++ tries++