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 // get node peers down to MaxDepth encoded in protobuf
msg := PeersToProto(n.node, MaxDepth) msg := PeersToProto(n.node, MaxDepth)
go func() {
// advertise yourself to the network // advertise yourself to the network
if err := n.sendTo("peer", NetworkChannel, peer, msg); err != nil { if err := n.sendTo("peer", NetworkChannel, peer, msg); err != nil {
log.Debugf("Network failed to advertise peers: %v", err) 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 // advertise all the routes when a new node has connected
if err := n.router.Solicit(); err != nil { if err := n.router.Solicit(); err != nil {
log.Debugf("Network failed to solicit routes: %s", err) log.Debugf("Network failed to solicit routes: %s", err)
@ -781,6 +785,7 @@ func (n *network) processNetChan(listener tunnel.Listener) {
default: default:
// don't block // 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() {
// 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 { if err := n.sendTo("solicit", ControlChannel, peer, msg); err != nil {
log.Debugf("Network failed to send solicit message: %s", err) 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,7 +240,6 @@ 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)
@ -249,9 +248,7 @@ func (t *tun) manageLink(link *link) {
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)
@ -262,7 +259,6 @@ func (t *tun) manageLink(link *link) {
t.delLink(link.Remote()) t.delLink(link.Remote())
return return
} }
}()
} }
} }
} }