connmgr: Rename max outbound to target outbound
This commit is contained in:
parent
6bb8d297a6
commit
aca9fc040c
3 changed files with 52 additions and 57 deletions
|
@ -32,9 +32,9 @@ var (
|
|||
// persistent connections.
|
||||
defaultRetryDuration = time.Second * 5
|
||||
|
||||
// defaultMaxOutbound is the default number of maximum outbound connections
|
||||
// to maintain.
|
||||
defaultMaxOutbound = uint32(8)
|
||||
// defaultTargetOutbound is the default number of outbound connections to
|
||||
// maintain.
|
||||
defaultTargetOutbound = uint32(8)
|
||||
)
|
||||
|
||||
// DialFunc defines a function that dials a connection.
|
||||
|
@ -110,9 +110,9 @@ func (c *ConnReq) String() string {
|
|||
|
||||
// Config holds the configuration options related to the connection manager.
|
||||
type Config struct {
|
||||
// MaxOutbound is the maximum number of outbound network connections to
|
||||
// TargetOutbound is the number of outbound network connections to
|
||||
// maintain. Defaults to 8.
|
||||
MaxOutbound uint32
|
||||
TargetOutbound uint32
|
||||
|
||||
// RetryDuration is the duration to wait before retrying connection
|
||||
// requests. Defaults to 5s.
|
||||
|
@ -207,7 +207,7 @@ func (cm *ConnManager) handleFailedConn(c *ConnReq, retry bool) {
|
|||
// connections so that we remain connected to the network. Connection requests
|
||||
// are processed and mapped by their assigned ids.
|
||||
func (cm *ConnManager) connHandler() {
|
||||
conns := make(map[uint64]*ConnReq, cm.cfg.MaxOutbound)
|
||||
conns := make(map[uint64]*ConnReq, cm.cfg.TargetOutbound)
|
||||
out:
|
||||
for {
|
||||
select {
|
||||
|
@ -329,7 +329,7 @@ func (cm *ConnManager) Start() {
|
|||
cm.wg.Add(1)
|
||||
go cm.connHandler()
|
||||
|
||||
for i := atomic.LoadUint64(&cm.connReqCount); i < uint64(cm.cfg.MaxOutbound); i++ {
|
||||
for i := atomic.LoadUint64(&cm.connReqCount); i < uint64(cm.cfg.TargetOutbound); i++ {
|
||||
go cm.NewConnReq()
|
||||
}
|
||||
}
|
||||
|
@ -359,8 +359,8 @@ func New(cfg *Config) (*ConnManager, error) {
|
|||
if cfg.RetryDuration <= 0 {
|
||||
cfg.RetryDuration = defaultRetryDuration
|
||||
}
|
||||
if cfg.MaxOutbound == 0 {
|
||||
cfg.MaxOutbound = defaultMaxOutbound
|
||||
if cfg.TargetOutbound == 0 {
|
||||
cfg.TargetOutbound = defaultTargetOutbound
|
||||
}
|
||||
cm := ConnManager{
|
||||
cfg: *cfg, // Copy so caller can't mutate
|
||||
|
|
|
@ -100,7 +100,7 @@ func TestStartStop(t *testing.T) {
|
|||
connected := make(chan *ConnReq)
|
||||
disconnected := make(chan *ConnReq)
|
||||
cmgr, err := New(&Config{
|
||||
MaxOutbound: 1,
|
||||
TargetOutbound: 1,
|
||||
GetNewAddress: func() (string, error) { return "127.0.0.1:18555", nil },
|
||||
Dial: mockDialer,
|
||||
OnConnection: func(c *ConnReq, conn net.Conn) {
|
||||
|
@ -141,7 +141,7 @@ func TestStartStop(t *testing.T) {
|
|||
func TestConnectMode(t *testing.T) {
|
||||
connected := make(chan *ConnReq)
|
||||
cmgr, err := New(&Config{
|
||||
MaxOutbound: 2,
|
||||
TargetOutbound: 2,
|
||||
Dial: mockDialer,
|
||||
OnConnection: func(c *ConnReq, conn net.Conn) {
|
||||
connected <- c
|
||||
|
@ -173,15 +173,15 @@ func TestConnectMode(t *testing.T) {
|
|||
cmgr.Stop()
|
||||
}
|
||||
|
||||
// TestMaxOutbound tests the maximum number of outbound connections.
|
||||
// TestTargetOutbound tests the target number of outbound connections.
|
||||
//
|
||||
// We wait until all connections are established, then test they there are the
|
||||
// only connections made.
|
||||
func TestMaxOutbound(t *testing.T) {
|
||||
maxOutbound := uint32(10)
|
||||
func TestTargetOutbound(t *testing.T) {
|
||||
targetOutbound := uint32(10)
|
||||
connected := make(chan *ConnReq)
|
||||
cmgr, err := New(&Config{
|
||||
MaxOutbound: maxOutbound,
|
||||
TargetOutbound: targetOutbound,
|
||||
Dial: mockDialer,
|
||||
GetNewAddress: func() (string, error) { return "127.0.0.1:18555", nil },
|
||||
OnConnection: func(c *ConnReq, conn net.Conn) {
|
||||
|
@ -192,13 +192,13 @@ func TestMaxOutbound(t *testing.T) {
|
|||
t.Fatalf("New error: %v", err)
|
||||
}
|
||||
cmgr.Start()
|
||||
for i := uint32(0); i < maxOutbound; i++ {
|
||||
for i := uint32(0); i < targetOutbound; i++ {
|
||||
<-connected
|
||||
}
|
||||
|
||||
select {
|
||||
case c := <-connected:
|
||||
t.Fatalf("max outbound: got unexpected connection - %v", c.Addr)
|
||||
t.Fatalf("target outbound: got unexpected connection - %v", c.Addr)
|
||||
case <-time.After(time.Millisecond):
|
||||
break
|
||||
}
|
||||
|
@ -214,7 +214,7 @@ func TestRetryPermanent(t *testing.T) {
|
|||
disconnected := make(chan *ConnReq)
|
||||
cmgr, err := New(&Config{
|
||||
RetryDuration: time.Millisecond,
|
||||
MaxOutbound: 1,
|
||||
TargetOutbound: 1,
|
||||
Dial: mockDialer,
|
||||
OnConnection: func(c *ConnReq, conn net.Conn) {
|
||||
connected <- c
|
||||
|
@ -303,7 +303,7 @@ func TestMaxRetryDuration(t *testing.T) {
|
|||
connected := make(chan *ConnReq)
|
||||
cmgr, err := New(&Config{
|
||||
RetryDuration: time.Millisecond,
|
||||
MaxOutbound: 1,
|
||||
TargetOutbound: 1,
|
||||
Dial: timedDialer,
|
||||
OnConnection: func(c *ConnReq, conn net.Conn) {
|
||||
connected <- c
|
||||
|
@ -335,7 +335,7 @@ func TestNetworkFailure(t *testing.T) {
|
|||
return nil, errors.New("network down")
|
||||
}
|
||||
cmgr, err := New(&Config{
|
||||
MaxOutbound: 5,
|
||||
TargetOutbound: 5,
|
||||
RetryDuration: 5 * time.Millisecond,
|
||||
Dial: errDialer,
|
||||
GetNewAddress: func() (string, error) { return "127.0.0.1:18555", nil },
|
||||
|
|
17
server.go
17
server.go
|
@ -45,8 +45,8 @@ const (
|
|||
// required to be supported by outbound peers.
|
||||
defaultRequiredServices = wire.SFNodeNetwork
|
||||
|
||||
// defaultMaxOutbound is the default number of max outbound peers.
|
||||
defaultMaxOutbound = 8
|
||||
// defaultTargetOutbound is the default number of outbound peers to target.
|
||||
defaultTargetOutbound = 8
|
||||
|
||||
// connectionRetryInterval is the base amount of time to wait in between
|
||||
// retries when connecting to persistent peers. It is adjusted by the
|
||||
|
@ -106,7 +106,6 @@ type peerState struct {
|
|||
persistentPeers map[int32]*serverPeer
|
||||
banned map[string]time.Time
|
||||
outboundGroups map[string]int
|
||||
maxOutboundPeers int
|
||||
}
|
||||
|
||||
// Count returns the count of all known peers.
|
||||
|
@ -1659,12 +1658,8 @@ func (s *server) peerHandler() {
|
|||
persistentPeers: make(map[int32]*serverPeer),
|
||||
outboundPeers: make(map[int32]*serverPeer),
|
||||
banned: make(map[string]time.Time),
|
||||
maxOutboundPeers: defaultMaxOutbound,
|
||||
outboundGroups: make(map[string]int),
|
||||
}
|
||||
if cfg.MaxPeers < state.maxOutboundPeers {
|
||||
state.maxOutboundPeers = cfg.MaxPeers
|
||||
}
|
||||
|
||||
if !cfg.DisableDNSSeed {
|
||||
// Add peers discovered through DNS to the address manager.
|
||||
|
@ -2464,13 +2459,13 @@ func newServer(listenAddrs []string, db database.DB, chainParams *chaincfg.Param
|
|||
}
|
||||
|
||||
// Create a connection manager.
|
||||
maxOutbound := defaultMaxOutbound
|
||||
if cfg.MaxPeers < maxOutbound {
|
||||
maxOutbound = cfg.MaxPeers
|
||||
targetOutbound := defaultTargetOutbound
|
||||
if cfg.MaxPeers < targetOutbound {
|
||||
targetOutbound = cfg.MaxPeers
|
||||
}
|
||||
cmgr, err := connmgr.New(&connmgr.Config{
|
||||
RetryDuration: connectionRetryInterval,
|
||||
MaxOutbound: uint32(maxOutbound),
|
||||
TargetOutbound: uint32(targetOutbound),
|
||||
Dial: btcdDial,
|
||||
OnConnection: s.outboundPeerConnected,
|
||||
GetNewAddress: newAddressFunc,
|
||||
|
|
Loading…
Reference in a new issue