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
msg := PeersToProto(n.node, MaxDepth)
// advertise yourself to the network
if err := n.sendTo("peer", NetworkChannel, peer, msg); err != nil {
log.Debugf("Network failed to advertise peers: %v", err)
}
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)
}
// 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)
}
// wait for a second
<-time.After(time.Second)
// specify that we're soliciting
select {
case n.solicited <- peer:
default:
// don't block
}
// 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)
}
// specify that we're soliciting
select {
case n.solicited <- peer:
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
if err := n.sendTo("solicit", ControlChannel, peer, msg); err != nil {
log.Debugf("Network failed to send solicit message: %s", err)
}
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,29 +240,25 @@ func (t *tun) manageLink(link *link) {
case <-link.closed:
return
case <-discover.C:
go func() {
// wait half the discover time
wait(DiscoverTime)
// wait half the discover time
wait(DiscoverTime)
// send a discovery message to the link
log.Debugf("Tunnel sending discover to link: %v", link.Remote())
if err := t.sendMsg("discover", link); err != nil {
log.Debugf("Tunnel failed to send discover to link %s: %v", link.Remote(), err)
}
}()
// send a discovery message to the link
log.Debugf("Tunnel sending discover to link: %v", link.Remote())
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)
// wait half the keepalive time
wait(KeepAliveTime)
// send keepalive message
log.Debugf("Tunnel sending keepalive to link: %v", link.Remote())
if err := t.sendMsg("keepalive", link); err != nil {
log.Debugf("Tunnel error sending keepalive to link %v: %v", link.Remote(), err)
t.delLink(link.Remote())
return
}
}()
// send keepalive message
log.Debugf("Tunnel sending keepalive to link: %v", link.Remote())
if err := t.sendMsg("keepalive", link); err != nil {
log.Debugf("Tunnel error sending keepalive to link %v: %v", link.Remote(), err)
t.delLink(link.Remote())
return
}
}
}
}