connmgr: Rename max outbound to target outbound

This commit is contained in:
Javed Khan 2016-11-03 01:02:36 +05:30 committed by Dave Collins
parent 6bb8d297a6
commit aca9fc040c
3 changed files with 52 additions and 57 deletions

View file

@ -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

View file

@ -100,9 +100,9 @@ func TestStartStop(t *testing.T) {
connected := make(chan *ConnReq)
disconnected := make(chan *ConnReq)
cmgr, err := New(&Config{
MaxOutbound: 1,
GetNewAddress: func() (string, error) { return "127.0.0.1:18555", nil },
Dial: mockDialer,
TargetOutbound: 1,
GetNewAddress: func() (string, error) { return "127.0.0.1:18555", nil },
Dial: mockDialer,
OnConnection: func(c *ConnReq, conn net.Conn) {
connected <- c
},
@ -141,8 +141,8 @@ func TestStartStop(t *testing.T) {
func TestConnectMode(t *testing.T) {
connected := make(chan *ConnReq)
cmgr, err := New(&Config{
MaxOutbound: 2,
Dial: mockDialer,
TargetOutbound: 2,
Dial: mockDialer,
OnConnection: func(c *ConnReq, conn net.Conn) {
connected <- c
},
@ -173,17 +173,17 @@ 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,
Dial: mockDialer,
GetNewAddress: func() (string, error) { return "127.0.0.1:18555", nil },
TargetOutbound: targetOutbound,
Dial: mockDialer,
GetNewAddress: func() (string, error) { return "127.0.0.1:18555", nil },
OnConnection: func(c *ConnReq, conn net.Conn) {
connected <- c
},
@ -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
}
@ -213,9 +213,9 @@ func TestRetryPermanent(t *testing.T) {
connected := make(chan *ConnReq)
disconnected := make(chan *ConnReq)
cmgr, err := New(&Config{
RetryDuration: time.Millisecond,
MaxOutbound: 1,
Dial: mockDialer,
RetryDuration: time.Millisecond,
TargetOutbound: 1,
Dial: mockDialer,
OnConnection: func(c *ConnReq, conn net.Conn) {
connected <- c
},
@ -302,9 +302,9 @@ func TestMaxRetryDuration(t *testing.T) {
connected := make(chan *ConnReq)
cmgr, err := New(&Config{
RetryDuration: time.Millisecond,
MaxOutbound: 1,
Dial: timedDialer,
RetryDuration: time.Millisecond,
TargetOutbound: 1,
Dial: timedDialer,
OnConnection: func(c *ConnReq, conn net.Conn) {
connected <- c
},
@ -335,10 +335,10 @@ func TestNetworkFailure(t *testing.T) {
return nil, errors.New("network down")
}
cmgr, err := New(&Config{
MaxOutbound: 5,
RetryDuration: 5 * time.Millisecond,
Dial: errDialer,
GetNewAddress: func() (string, error) { return "127.0.0.1:18555", nil },
TargetOutbound: 5,
RetryDuration: 5 * time.Millisecond,
Dial: errDialer,
GetNewAddress: func() (string, error) { return "127.0.0.1:18555", nil },
OnConnection: func(c *ConnReq, conn net.Conn) {
t.Fatalf("network failure: got unexpected connection - %v", c.Addr)
},

View file

@ -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
@ -101,12 +101,11 @@ type updatePeerHeightsMsg struct {
// peerState maintains state of inbound, persistent, outbound peers as well
// as banned peers and outbound groups.
type peerState struct {
inboundPeers map[int32]*serverPeer
outboundPeers map[int32]*serverPeer
persistentPeers map[int32]*serverPeer
banned map[string]time.Time
outboundGroups map[string]int
maxOutboundPeers int
inboundPeers map[int32]*serverPeer
outboundPeers map[int32]*serverPeer
persistentPeers map[int32]*serverPeer
banned map[string]time.Time
outboundGroups map[string]int
}
// Count returns the count of all known peers.
@ -1655,15 +1654,11 @@ func (s *server) peerHandler() {
srvrLog.Tracef("Starting peer handler")
state := &peerState{
inboundPeers: make(map[int32]*serverPeer),
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
inboundPeers: make(map[int32]*serverPeer),
persistentPeers: make(map[int32]*serverPeer),
outboundPeers: make(map[int32]*serverPeer),
banned: make(map[string]time.Time),
outboundGroups: make(map[string]int),
}
if !cfg.DisableDNSSeed {
@ -2464,16 +2459,16 @@ 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),
Dial: btcdDial,
OnConnection: s.outboundPeerConnected,
GetNewAddress: newAddressFunc,
RetryDuration: connectionRetryInterval,
TargetOutbound: uint32(targetOutbound),
Dial: btcdDial,
OnConnection: s.outboundPeerConnected,
GetNewAddress: newAddressFunc,
})
if err != nil {
return nil, err