bff2ba70fd
This commit introduces package connmgr which contains connection management related functionality. The following is an overview of the features the package provides: - Maintain fixed number of outbound connections - Optional connect-only mode - Retry persistent connections with increasing back-off - Source peers from DNS seeds - Use Tor to resolve DNS - Dynamic ban scores - Test coverage In addition, btcd has been refactored to make use of the new package by extending the connection manager to work with the server to source and maintain peer connections. The following is a broad overview of the changes to integrate the package: - Simplify peer state by removing pending, retry peers - Refactor to remove retries which are now handled by connmgr - Use callback to add addresses sourced from the DNS seed Finally the following connection-related things have been improved as a part of this refactor: - Fixes 100% cpu usage when network is down (#129) - Fixes issues with max peers (#577) - Simplify outbound peer connections management
66 lines
1.8 KiB
Go
66 lines
1.8 KiB
Go
// Copyright (c) 2016 The btcsuite developers
|
|
// Use of this source code is governed by an ISC
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package connmgr
|
|
|
|
import (
|
|
mrand "math/rand"
|
|
"net"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/btcsuite/btcd/chaincfg"
|
|
"github.com/btcsuite/btcd/wire"
|
|
)
|
|
|
|
const (
|
|
// These constants are used by the DNS seed code to pick a random last
|
|
// seen time.
|
|
secondsIn3Days int32 = 24 * 60 * 60 * 3
|
|
secondsIn4Days int32 = 24 * 60 * 60 * 4
|
|
)
|
|
|
|
// OnSeed is the signature of the callback function which is invoked when DNS
|
|
// seeding is succesfull.
|
|
type OnSeed func(addrs []*wire.NetAddress)
|
|
|
|
// LookupFunc is the signature of the DNS lookup function.
|
|
type LookupFunc func(string) ([]net.IP, error)
|
|
|
|
// SeedFromDNS uses DNS seeding to populate the address manager with peers.
|
|
func SeedFromDNS(chainParams *chaincfg.Params, lookupFn LookupFunc, seedFn OnSeed) {
|
|
for _, seeder := range chainParams.DNSSeeds {
|
|
go func(seeder string) {
|
|
randSource := mrand.New(mrand.NewSource(time.Now().UnixNano()))
|
|
|
|
seedpeers, err := lookupFn(seeder)
|
|
if err != nil {
|
|
log.Infof("DNS discovery failed on seed %s: %v", seeder, err)
|
|
return
|
|
}
|
|
numPeers := len(seedpeers)
|
|
|
|
log.Infof("%d addresses found from DNS seed %s", numPeers, seeder)
|
|
|
|
if numPeers == 0 {
|
|
return
|
|
}
|
|
addresses := make([]*wire.NetAddress, len(seedpeers))
|
|
// if this errors then we have *real* problems
|
|
intPort, _ := strconv.Atoi(chainParams.DefaultPort)
|
|
for i, peer := range seedpeers {
|
|
addresses[i] = new(wire.NetAddress)
|
|
addresses[i].SetAddress(peer, uint16(intPort))
|
|
// bitcoind seeds with addresses from
|
|
// a time randomly selected between 3
|
|
// and 7 days ago.
|
|
addresses[i].Timestamp = time.Now().Add(-1 *
|
|
time.Second * time.Duration(secondsIn3Days+
|
|
randSource.Int31n(secondsIn4Days)))
|
|
}
|
|
|
|
seedFn(addresses)
|
|
}(seeder)
|
|
}
|
|
}
|