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,11 +765,15 @@ func (n *network) processNetChan(listener tunnel.Listener) {
// get node peers down to MaxDepth encoded in protobuf
msg := PeersToProto(n.node, MaxDepth)
go func() {
// advertise yourself to the network
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)
// advertise all the routes when a new node has connected
if err := n.router.Solicit(); err != nil {
log.Debugf("Network failed to solicit routes: %s", err)
@ -781,6 +785,7 @@ func (n *network) processNetChan(listener tunnel.Listener) {
default:
// don't block
}
}()
case "peer":
// mark the time the message has been received
now := time.Now()
@ -818,10 +823,20 @@ func (n *network) processNetChan(listener tunnel.Listener) {
Id: n.options.Id,
}
// only solicit this peer
go func() {
// advertise yourself to the peer
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
// we're expecting any error to be ErrPeerExists
@ -943,7 +958,7 @@ func (n *network) manage() {
return
case <-announce.C:
// jitter
j := rand.Int63n(30)
j := rand.Int63n(int64(AnnounceTime / 2))
time.Sleep(time.Duration(j) * time.Second)
// TODO: intermittently flip between peer selection

View File

@ -240,7 +240,6 @@ func (t *tun) manageLink(link *link) {
case <-link.closed:
return
case <-discover.C:
go func() {
// wait half the discover time
wait(DiscoverTime)
@ -249,9 +248,7 @@ func (t *tun) manageLink(link *link) {
if err := t.sendMsg("discover", link); err != nil {
log.Debugf("Tunnel failed to send discover to link %s: %v", link.Remote(), err)
}
}()
case <-keepalive.C:
go func() {
// wait half the keepalive time
wait(KeepAliveTime)
@ -262,7 +259,6 @@ func (t *tun) manageLink(link *link) {
t.delLink(link.Remote())
return
}
}()
}
}
}