From 5f04fd58ab7cce48ca772c659f8b7623bda978de Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Mon, 2 Dec 2019 18:48:19 +0000 Subject: [PATCH] optimizations to connect fast --- network/default.go | 44 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/network/default.go b/network/default.go index 0b19a848..b9dfe13f 100644 --- a/network/default.go +++ b/network/default.go @@ -476,6 +476,24 @@ func (n *network) processNetChan(listener tunnel.Listener) { n.Lock() delete(n.peerLinks, pbNetClose.Node.Address) n.Unlock() + case "solicit": + // received a peer solicit message + pbRtrSolicit := &pbRtr.Solicit{} + if err := proto.Unmarshal(m.msg.Body, pbRtrSolicit); err != nil { + log.Debugf("Network fail to unmarshal solicit message: %v", err) + continue + } + log.Debugf("Network received peer solicit message from: %s", pbRtrSolicit.Id) + // ignore solicitation when requested by you + if pbRtrSolicit.Id == n.options.Id { + continue + } + // get node peers down to MaxDepth encoded in protobuf + msg := PeersToProto(n.node, MaxDepth) + // advertise yourself to the network + if err := n.sendMsg("peer", msg, NetworkChannel); err != nil { + log.Debugf("Network failed to advertise peers: %v", err) + } } case <-n.closed: return @@ -767,6 +785,13 @@ func (n *network) processCtrlChan(listener tunnel.Listener) { // loookup advertising node in our peer topology advertNode := n.node.GetPeerNode(pbRtrAdvert.Id) if advertNode == nil { + msg := &pbRtr.Solicit{ + Id: pbRtrAdvert.Id, + } + // go solicit the peer + if err := n.sendMsg("solicit", msg, NetworkChannel); err != nil { + log.Debugf("Network failed to send solicit message: %s", err) + } // if we can't find the node in our topology (MaxDepth) we skipp prcessing adverts log.Debugf("Network skipping advert message from unknown peer: %s", pbRtrAdvert.Id) continue @@ -1020,8 +1045,23 @@ func (n *network) Connect() error { } n.Unlock() - // send the connect message - n.sendConnect() + // send connect after there's a link established + go func() { + // wait for 30 ticks e.g 30 seconds + for i := 0; i < 30; i++ { + // get the current links + links := n.tunnel.Links() + + // if there are no links wait until we have one + if len(links) == 0 { + time.Sleep(time.Second) + continue + } + + // send the connect message + n.sendConnect() + } + }() // go resolving network nodes go n.resolve()