Skip to content
This repository has been archived by the owner on Jan 2, 2025. It is now read-only.

Commit

Permalink
fix(daemon): Bootstrap autoconnect.
Browse files Browse the repository at this point in the history
  • Loading branch information
juligasa committed Oct 16, 2023
1 parent 56e27fe commit cff7733
Showing 1 changed file with 39 additions and 11 deletions.
50 changes: 39 additions & 11 deletions backend/mttnet/mttnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,27 +359,55 @@ func (n *Node) startLibp2p(ctx context.Context) error {
if err != nil {
return fmt.Errorf("failed to parse bootstrap addresses %+v: %w", n.cfg.BootstrapPeers, err)
}

ticker := time.NewTicker(10 * time.Minute)
done := make(chan bool)
res := n.p2p.Bootstrap(ctx, bootInfo)

if res.NumFailedConnections == 0 {
n.log.Info("BootstrapFinished",
zap.Int("peersTotal", len(res.Peers)),
zap.Int("failedConnections", int(res.NumFailedConnections)),
)
return nil
}
n.log.Info("BootstrapFinished",
zap.NamedError("dhtError", res.RoutingErr),
zap.Int("peersTotal", len(res.Peers)),
zap.Int("failedConnectionsTotal", int(res.NumFailedConnections)),
zap.Any("ConnectErrs", res.ConnectErrs),
)

if res.NumFailedConnections > 0 {
for i, err := range res.ConnectErrs {
if err == nil {
continue
go func() {
for {
select {
case <-done:
return
case <-ticker.C:
res := n.p2p.Bootstrap(ctx, bootInfo)

n.log.Info("BootstrapFinished",
zap.NamedError("dhtError", res.RoutingErr),
zap.Int("peersTotal", len(res.Peers)),
zap.Int("failedConnectionsTotal", int(res.NumFailedConnections)),
zap.Any("ConnectErrs", res.ConnectErrs),
)

if res.NumFailedConnections > 0 {
for i, err := range res.ConnectErrs {
if err == nil {
continue
}
n.log.Debug("BootstrapConnectionError",
zap.String("peer", res.Peers[i].ID.String()),
zap.Error(err),
)
}
} else {
ticker.Stop()

This comment has been minimized.

Copy link
@burdiyan

burdiyan Oct 26, 2023

Collaborator

Not sure if I'm reading it wrong, but looks like this goroutine would return as soon as res.NumFailedConnection is 0 (which is the normal case). So looks like it wouldn't bootstrap periodically. @juligasa

This comment has been minimized.

Copy link
@juligasa

juligasa Oct 26, 2023

Author Collaborator

At the beginning, if there are some failing bootstraps, we will retry to connect to them until all of them are connected. At that point, if any bootstrap goes down and then up (its more likely that the local app goes down first), they will still be bootstrap nodes.

done <- true
}
}
n.log.Debug("BootstrapConnectionError",
zap.String("peer", res.Peers[i].ID.String()),
zap.Error(err),
)
}
}
}()
}

return nil
Expand Down

0 comments on commit cff7733

Please sign in to comment.