optimizations to connect fast

This commit is contained in:
Asim Aslam 2019-12-02 18:48:19 +00:00
parent da04aa8ae8
commit 5f04fd58ab

View File

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