Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Avoid many pointless connection attempts #451

Merged
merged 3 commits into from
Mar 11, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions router/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Connection interface {
BreakTie(Connection) ConnectionTieBreak
RemoteTCPAddr() string
Established() bool
Outbound() bool

This comment was marked as abuse.

Shutdown(error)
Log(args ...interface{})
}
Expand All @@ -33,6 +34,7 @@ type RemoteConnection struct {
remote *Peer
remoteTCPAddr string
established bool
outbound bool

This comment was marked as abuse.

}

type LocalConnection struct {
Expand Down Expand Up @@ -66,12 +68,13 @@ type ConnectionInteraction struct {
payload interface{}
}

func NewRemoteConnection(from, to *Peer, tcpAddr string, established bool) *RemoteConnection {
func NewRemoteConnection(from, to *Peer, tcpAddr string, established bool, outbound bool) *RemoteConnection {

This comment was marked as abuse.

return &RemoteConnection{
local: from,
remote: to,
remoteTCPAddr: tcpAddr,
established: established}
established: established,
outbound: outbound}

This comment was marked as abuse.

}

func (conn *RemoteConnection) Local() *Peer {
Expand All @@ -94,6 +97,10 @@ func (conn *RemoteConnection) Established() bool {
return conn.established
}

func (conn *RemoteConnection) Outbound() bool {
return conn.outbound
}

This comment was marked as abuse.

func (conn *RemoteConnection) Shutdown(error) {
}

Expand Down
2 changes: 1 addition & 1 deletion router/connection_maker.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (cm *ConnectionMaker) checkStateAndAttemptConnections() time.Duration {
// aren't
cm.peers.ForEach(func(name PeerName, peer *Peer) {
peer.ForEachConnection(func(otherPeer PeerName, conn Connection) {
if otherPeer == cm.ourself.Name || ourConnectedPeers[otherPeer] {
if otherPeer == cm.ourself.Name || ourConnectedPeers[otherPeer] || !conn.Outbound() {

This comment was marked as abuse.

return
}
address := conn.RemoteTCPAddr()
Expand Down
2 changes: 1 addition & 1 deletion router/gossip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (router *Router) AddTestChannelConnection(r *Router) {
r.Peers.FetchWithDefault(fromPeer) // Has side-effect of incrementing refcount
router.Peers.FetchWithDefault(toPeer) //

conn := &mockChannelConnection{RemoteConnection{router.Ourself.Peer, toPeer, "", false}, r}
conn := &mockChannelConnection{RemoteConnection{router.Ourself.Peer, toPeer, "", false, true}, r}
router.Ourself.handleAddConnection(conn)
router.Ourself.handleConnectionEstablished(conn)
}
Expand Down
2 changes: 1 addition & 1 deletion router/local_peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (peer *LocalPeer) CreateConnection(peerAddr string, acceptNewPeer bool) err
if err != nil {
return err
}
connRemote := NewRemoteConnection(peer.Peer, nil, tcpConn.RemoteAddr().String(), false)
connRemote := NewRemoteConnection(peer.Peer, nil, tcpConn.RemoteAddr().String(), false, true)
connLocal := NewLocalConnection(connRemote, tcpConn, udpAddr, peer.Router)
connLocal.Start(acceptNewPeer)
return nil
Expand Down
4 changes: 2 additions & 2 deletions router/mocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (peers *Peers) AddTestRemoteConnection(p1, p2 *Peer) {
toName := p2.Name
toPeer := NewPeer(toName, "", p2.UID, 0)
toPeer = peers.FetchWithDefault(toPeer)
peers.ourself.addConnection(&RemoteConnection{fromPeer, toPeer, "", false})
peers.ourself.addConnection(&RemoteConnection{fromPeer, toPeer, "", false, false})
}

func (peers *Peers) DeleteTestConnection(p *Peer) {
Expand All @@ -44,7 +44,7 @@ func (peers *Peers) DeleteTestConnection(p *Peer) {
// from what is created by the real code.
func newMockConnection(from, to *Peer) Connection {
type mockConnection struct{ RemoteConnection }
return &mockConnection{RemoteConnection{from, to, "", false}}
return &mockConnection{RemoteConnection{from, to, "", false, false}}
}

func checkEqualConns(t *testing.T, ourName PeerName, got, wanted map[PeerName]Connection) {
Expand Down
15 changes: 10 additions & 5 deletions router/peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func (peers *Peers) decodeUpdate(update []byte) (newPeers map[PeerName]*Peer, de
}

for _, connsBuf := range decodedConns {
decErr := connsIterator(connsBuf, func(remoteNameByte []byte, _ string, _ bool) {
decErr := connsIterator(connsBuf, func(remoteNameByte []byte, _ string, _, _ bool) {
remoteName := PeerNameFromBin(remoteNameByte)
if _, found := newPeers[remoteName]; found {
return
Expand Down Expand Up @@ -268,6 +268,7 @@ func (peer *Peer) encode(enc *gob.Encoder) {
checkFatal(connsEnc.Encode(conn.RemoteTCPAddr()))
// DANGER holding rlock on peer, going to take rlock on conn
checkFatal(connsEnc.Encode(conn.Established()))
checkFatal(connsEnc.Encode(conn.Outbound()))

This comment was marked as abuse.

}
checkFatal(enc.Encode(connsBuf.Bytes()))
}
Expand All @@ -291,7 +292,7 @@ func decodePeerNoConns(dec *gob.Decoder) (nameByte []byte, nickName string, uid
return
}

func connsIterator(input []byte, fun func([]byte, string, bool)) error {
func connsIterator(input []byte, fun func([]byte, string, bool, bool)) error {
buf := new(bytes.Buffer)
buf.Write(input)
dec := gob.NewDecoder(buf)
Expand All @@ -308,16 +309,20 @@ func connsIterator(input []byte, fun func([]byte, string, bool)) error {
if err := dec.Decode(&established); err != nil {
return err
}
fun(nameByte, foundAt, established)
var outbound bool
if err := dec.Decode(&outbound); err != nil {
return err
}
fun(nameByte, foundAt, established, outbound)

This comment was marked as abuse.

}
}

func readConnsMap(peer *Peer, buf []byte, table map[PeerName]*Peer) map[PeerName]Connection {
conns := make(map[PeerName]Connection)
if err := connsIterator(buf, func(nameByte []byte, remoteTCPAddr string, established bool) {
if err := connsIterator(buf, func(nameByte []byte, remoteTCPAddr string, established bool, outbound bool) {
name := PeerNameFromBin(nameByte)
remotePeer := table[name]
conn := NewRemoteConnection(peer, remotePeer, remoteTCPAddr, established)
conn := NewRemoteConnection(peer, remotePeer, remoteTCPAddr, established, outbound)
conns[name] = conn
}); err != io.EOF {
// this should never happen since we've already successfully
Expand Down
2 changes: 1 addition & 1 deletion router/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package router

const (
Protocol = "weave"
ProtocolVersion = 13
ProtocolVersion = 14
)

type ProtocolTag byte
Expand Down
2 changes: 1 addition & 1 deletion router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func (router *Router) acceptTCP(tcpConn *net.TCPConn) {
// start.
remoteAddrStr := tcpConn.RemoteAddr().String()
log.Printf("->[%s] connection accepted\n", remoteAddrStr)
connRemote := NewRemoteConnection(router.Ourself.Peer, nil, remoteAddrStr, false)
connRemote := NewRemoteConnection(router.Ourself.Peer, nil, remoteAddrStr, false, false)
connLocal := NewLocalConnection(connRemote, tcpConn, nil, router)
connLocal.Start(true)
}
Expand Down