Skip to content

Commit

Permalink
remove go routines from tunnel, fire network messages in go routines
Browse files Browse the repository at this point in the history
  • Loading branch information
asim committed Dec 12, 2019
1 parent ae934c1 commit df728aa
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 40 deletions.
53 changes: 34 additions & 19 deletions network/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
38 changes: 17 additions & 21 deletions tunnel/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

// 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
}
}()
// 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
}
}
}
}
Expand Down

0 comments on commit df728aa

Please sign in to comment.