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. // persistent connections.
defaultRetryDuration = time.Second * 5 defaultRetryDuration = time.Second * 5
// defaultMaxOutbound is the default number of maximum outbound connections // defaultTargetOutbound is the default number of outbound connections to
// to maintain. // maintain.
defaultMaxOutbound = uint32(8) defaultTargetOutbound = uint32(8)
) )
// DialFunc defines a function that dials a connection. // 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. // Config holds the configuration options related to the connection manager.
type Config struct { 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. // maintain. Defaults to 8.
MaxOutbound uint32 TargetOutbound uint32
// RetryDuration is the duration to wait before retrying connection // RetryDuration is the duration to wait before retrying connection
// requests. Defaults to 5s. // 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 // connections so that we remain connected to the network. Connection requests
// are processed and mapped by their assigned ids. // are processed and mapped by their assigned ids.
func (cm *ConnManager) connHandler() { func (cm *ConnManager) connHandler() {
conns := make(map[uint64]*ConnReq, cm.cfg.MaxOutbound) conns := make(map[uint64]*ConnReq, cm.cfg.TargetOutbound)
out: out:
for { for {
select { select {
@ -329,7 +329,7 @@ func (cm *ConnManager) Start() {
cm.wg.Add(1) cm.wg.Add(1)
go cm.connHandler() 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() go cm.NewConnReq()
} }
} }
@ -359,8 +359,8 @@ func New(cfg *Config) (*ConnManager, error) {
if cfg.RetryDuration <= 0 { if cfg.RetryDuration <= 0 {
cfg.RetryDuration = defaultRetryDuration cfg.RetryDuration = defaultRetryDuration
} }
if cfg.MaxOutbound == 0 { if cfg.TargetOutbound == 0 {
cfg.MaxOutbound = defaultMaxOutbound cfg.TargetOutbound = defaultTargetOutbound
} }
cm := ConnManager{ cm := ConnManager{
cfg: *cfg, // Copy so caller can't mutate cfg: *cfg, // Copy so caller can't mutate

View file

@ -100,7 +100,7 @@ func TestStartStop(t *testing.T) {
connected := make(chan *ConnReq) connected := make(chan *ConnReq)
disconnected := make(chan *ConnReq) disconnected := make(chan *ConnReq)
cmgr, err := New(&Config{ cmgr, err := New(&Config{
MaxOutbound: 1, TargetOutbound: 1,
GetNewAddress: func() (string, error) { return "127.0.0.1:18555", nil }, GetNewAddress: func() (string, error) { return "127.0.0.1:18555", nil },
Dial: mockDialer, Dial: mockDialer,
OnConnection: func(c *ConnReq, conn net.Conn) { OnConnection: func(c *ConnReq, conn net.Conn) {
@ -141,7 +141,7 @@ func TestStartStop(t *testing.T) {
func TestConnectMode(t *testing.T) { func TestConnectMode(t *testing.T) {
connected := make(chan *ConnReq) connected := make(chan *ConnReq)
cmgr, err := New(&Config{ cmgr, err := New(&Config{
MaxOutbound: 2, TargetOutbound: 2,
Dial: mockDialer, Dial: mockDialer,
OnConnection: func(c *ConnReq, conn net.Conn) { OnConnection: func(c *ConnReq, conn net.Conn) {
connected <- c connected <- c
@ -173,15 +173,15 @@ func TestConnectMode(t *testing.T) {
cmgr.Stop() 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 // We wait until all connections are established, then test they there are the
// only connections made. // only connections made.
func TestMaxOutbound(t *testing.T) { func TestTargetOutbound(t *testing.T) {
maxOutbound := uint32(10) targetOutbound := uint32(10)
connected := make(chan *ConnReq) connected := make(chan *ConnReq)
cmgr, err := New(&Config{ cmgr, err := New(&Config{
MaxOutbound: maxOutbound, TargetOutbound: targetOutbound,
Dial: mockDialer, Dial: mockDialer,
GetNewAddress: func() (string, error) { return "127.0.0.1:18555", nil }, GetNewAddress: func() (string, error) { return "127.0.0.1:18555", nil },
OnConnection: func(c *ConnReq, conn net.Conn) { OnConnection: func(c *ConnReq, conn net.Conn) {
@ -192,13 +192,13 @@ func TestMaxOutbound(t *testing.T) {
t.Fatalf("New error: %v", err) t.Fatalf("New error: %v", err)
} }
cmgr.Start() cmgr.Start()
for i := uint32(0); i < maxOutbound; i++ { for i := uint32(0); i < targetOutbound; i++ {
<-connected <-connected
} }
select { select {
case c := <-connected: 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): case <-time.After(time.Millisecond):
break break
} }
@ -214,7 +214,7 @@ func TestRetryPermanent(t *testing.T) {
disconnected := make(chan *ConnReq) disconnected := make(chan *ConnReq)
cmgr, err := New(&Config{ cmgr, err := New(&Config{
RetryDuration: time.Millisecond, RetryDuration: time.Millisecond,
MaxOutbound: 1, TargetOutbound: 1,
Dial: mockDialer, Dial: mockDialer,
OnConnection: func(c *ConnReq, conn net.Conn) { OnConnection: func(c *ConnReq, conn net.Conn) {
connected <- c connected <- c
@ -303,7 +303,7 @@ func TestMaxRetryDuration(t *testing.T) {
connected := make(chan *ConnReq) connected := make(chan *ConnReq)
cmgr, err := New(&Config{ cmgr, err := New(&Config{
RetryDuration: time.Millisecond, RetryDuration: time.Millisecond,
MaxOutbound: 1, TargetOutbound: 1,
Dial: timedDialer, Dial: timedDialer,
OnConnection: func(c *ConnReq, conn net.Conn) { OnConnection: func(c *ConnReq, conn net.Conn) {
connected <- c connected <- c
@ -335,7 +335,7 @@ func TestNetworkFailure(t *testing.T) {
return nil, errors.New("network down") return nil, errors.New("network down")
} }
cmgr, err := New(&Config{ cmgr, err := New(&Config{
MaxOutbound: 5, TargetOutbound: 5,
RetryDuration: 5 * time.Millisecond, RetryDuration: 5 * time.Millisecond,
Dial: errDialer, Dial: errDialer,
GetNewAddress: func() (string, error) { return "127.0.0.1:18555", nil }, GetNewAddress: func() (string, error) { return "127.0.0.1:18555", nil },

View file

@ -45,8 +45,8 @@ const (
// required to be supported by outbound peers. // required to be supported by outbound peers.
defaultRequiredServices = wire.SFNodeNetwork defaultRequiredServices = wire.SFNodeNetwork
// defaultMaxOutbound is the default number of max outbound peers. // defaultTargetOutbound is the default number of outbound peers to target.
defaultMaxOutbound = 8 defaultTargetOutbound = 8
// connectionRetryInterval is the base amount of time to wait in between // connectionRetryInterval is the base amount of time to wait in between
// retries when connecting to persistent peers. It is adjusted by the // retries when connecting to persistent peers. It is adjusted by the
@ -106,7 +106,6 @@ type peerState struct {
persistentPeers map[int32]*serverPeer persistentPeers map[int32]*serverPeer
banned map[string]time.Time banned map[string]time.Time
outboundGroups map[string]int outboundGroups map[string]int
maxOutboundPeers int
} }
// Count returns the count of all known peers. // Count returns the count of all known peers.
@ -1659,12 +1658,8 @@ func (s *server) peerHandler() {
persistentPeers: make(map[int32]*serverPeer), persistentPeers: make(map[int32]*serverPeer),
outboundPeers: make(map[int32]*serverPeer), outboundPeers: make(map[int32]*serverPeer),
banned: make(map[string]time.Time), banned: make(map[string]time.Time),
maxOutboundPeers: defaultMaxOutbound,
outboundGroups: make(map[string]int), outboundGroups: make(map[string]int),
} }
if cfg.MaxPeers < state.maxOutboundPeers {
state.maxOutboundPeers = cfg.MaxPeers
}
if !cfg.DisableDNSSeed { if !cfg.DisableDNSSeed {
// Add peers discovered through DNS to the address manager. // 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. // Create a connection manager.
maxOutbound := defaultMaxOutbound targetOutbound := defaultTargetOutbound
if cfg.MaxPeers < maxOutbound { if cfg.MaxPeers < targetOutbound {
maxOutbound = cfg.MaxPeers targetOutbound = cfg.MaxPeers
} }
cmgr, err := connmgr.New(&connmgr.Config{ cmgr, err := connmgr.New(&connmgr.Config{
RetryDuration: connectionRetryInterval, RetryDuration: connectionRetryInterval,
MaxOutbound: uint32(maxOutbound), TargetOutbound: uint32(targetOutbound),
Dial: btcdDial, Dial: btcdDial,
OnConnection: s.outboundPeerConnected, OnConnection: s.outboundPeerConnected,
GetNewAddress: newAddressFunc, GetNewAddress: newAddressFunc,