remove go routines from tunnel, fire network messages in go routines

This commit is contained in:
Asim Aslam 2019-12-12 13:34:08 +00:00
parent ae934c19f1
commit df728aaddd
2 changed files with 50 additions and 39 deletions

View File

@ -765,22 +765,27 @@ func (n *network) processNetChan(listener tunnel.Listener) {
// get node peers down to MaxDepth encoded in protobuf // get node peers down to MaxDepth encoded in protobuf
msg := PeersToProto(n.node, MaxDepth) msg := PeersToProto(n.node, MaxDepth)
// advertise yourself to the network go func() {
if err := n.sendTo("peer", NetworkChannel, peer, msg); err != nil { // advertise yourself to the network
log.Debugf("Network failed to advertise peers: %v", err) if err := n.sendTo("peer", NetworkChannel, peer, msg); err != nil {
} log.Debugf("Network failed to advertise peers: %v", err)
}
// advertise all the routes when a new node has connected // wait for a second
if err := n.router.Solicit(); err != nil { <-time.After(time.Second)
log.Debugf("Network failed to solicit routes: %s", err)
}
// specify that we're soliciting // advertise all the routes when a new node has connected
select { if err := n.router.Solicit(); err != nil {
case n.solicited <- peer: log.Debugf("Network failed to solicit routes: %s", err)
default: }
// don't block
} // specify that we're soliciting
select {
case n.solicited <- peer:
default:
// don't block
}
}()
case "peer": case "peer":
// mark the time the message has been received // mark the time the message has been received
now := time.Now() now := time.Now()
@ -818,10 +823,20 @@ func (n *network) processNetChan(listener tunnel.Listener) {
Id: n.options.Id, Id: n.options.Id,
} }
// only solicit this peer go func() {
if err := n.sendTo("solicit", ControlChannel, peer, msg); err != nil { // advertise yourself to the peer
log.Debugf("Network failed to send solicit message: %s", err) if err := n.sendTo("peer", NetworkChannel, peer, msg); err != nil {
} log.Debugf("Network failed to advertise peers: %v", err)
}
// wait for a second
<-time.After(time.Second)
// then solicit this peer
if err := n.sendTo("solicit", ControlChannel, peer, msg); err != nil {
log.Debugf("Network failed to send solicit message: %s", err)
}
}()
continue continue
// we're expecting any error to be ErrPeerExists // we're expecting any error to be ErrPeerExists
@ -943,7 +958,7 @@ func (n *network) manage() {
return return
case <-announce.C: case <-announce.C:
// jitter // jitter
j := rand.Int63n(30) j := rand.Int63n(int64(AnnounceTime / 2))
time.Sleep(time.Duration(j) * time.Second) time.Sleep(time.Duration(j) * time.Second)
// TODO: intermittently flip between peer selection // TODO: intermittently flip between peer selection

View File

@ -240,29 +240,25 @@ func (t *tun) manageLink(link *link) {
case <-link.closed: case <-link.closed:
return return
case <-discover.C: case <-discover.C:
go func() { // wait half the discover time
// wait half the discover time wait(DiscoverTime)
wait(DiscoverTime)
// send a discovery message to the link // send a discovery message to the link
log.Debugf("Tunnel sending discover to link: %v", link.Remote()) log.Debugf("Tunnel sending discover to link: %v", link.Remote())
if err := t.sendMsg("discover", link); err != nil { if err := t.sendMsg("discover", link); err != nil {
log.Debugf("Tunnel failed to send discover to link %s: %v", link.Remote(), err) log.Debugf("Tunnel failed to send discover to link %s: %v", link.Remote(), err)
} }
}()
case <-keepalive.C: case <-keepalive.C:
go func() { // wait half the keepalive time
// wait half the keepalive time wait(KeepAliveTime)
wait(KeepAliveTime)
// send keepalive message // send keepalive message
log.Debugf("Tunnel sending keepalive to link: %v", link.Remote()) log.Debugf("Tunnel sending keepalive to link: %v", link.Remote())
if err := t.sendMsg("keepalive", link); err != nil { if err := t.sendMsg("keepalive", link); err != nil {
log.Debugf("Tunnel error sending keepalive to link %v: %v", link.Remote(), err) log.Debugf("Tunnel error sending keepalive to link %v: %v", link.Remote(), err)
t.delLink(link.Remote()) t.delLink(link.Remote())
return return
} }
}()
} }
} }
} }