Skip to content

Commit 3a62ad6

Browse files
committed
avoid shadowing ips variable while retry to get mycelium iface
1 parent e4fd5e4 commit 3a62ad6

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

pkg/netbase/ifaceutil/ifaceutil.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ func GetIPsForIFace(iface, nsName string) ([]net.IPNet, error) {
3030
}
3131

3232
for _, ip := range ips {
33-
results = append(results, *ip.IPNet)
33+
if ip.IPNet != nil {
34+
results = append(results, *ip.IPNet)
35+
}
3436
}
35-
3637
return results, nil
3738
}
3839

@@ -48,9 +49,9 @@ func GetIPsForIFace(iface, nsName string) ([]net.IPNet, error) {
4849

4950
var results []net.IPNet
5051
err = netns.Do(func(_ ns.NetNS) error {
51-
var getErr error
52-
results, getErr = getIPs()
53-
return getErr
52+
r, e := getIPs()
53+
results = r
54+
return e
5455
})
5556

5657
return results, err

pkg/network/nr/net_resource.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -337,24 +337,31 @@ func getMyceliumPeers() ([]string, error) {
337337
mycInterface = publicInterfaceName
338338
}
339339

340+
log.Debug().Str("namespace", mycNamespace).Str("interface", mycInterface).Msg("get mycelium peers from")
341+
340342
var ips []net.IPNet
341343
bo := backoff.NewExponentialBackOff()
342344
bo.MaxElapsedTime = time.Minute
343345
bo.MaxInterval = 10 * time.Second
344346

345-
err := backoff.Retry(func() error {
346-
ips, err := baseifaceutil.GetIPsForIFace(mycInterface, mycNamespace)
347+
op := func() error {
348+
r, err := baseifaceutil.GetIPsForIFace(mycInterface, mycNamespace)
347349
if err != nil {
348-
return backoff.Permanent(errors.Wrap(err, "failed to get IPs"))
350+
return errors.Wrap(err, "failed to get IPs")
349351
}
350-
if len(ips) == 0 {
352+
if len(r) == 0 {
351353
return fmt.Errorf("no IPs available yet")
352354
}
355+
ips = r // only set if success
353356
return nil
354-
}, bo)
357+
}
358+
359+
notify := func(err error, d time.Duration) {
360+
log.Debug().Err(err).Dur("wait", d).Msg("retrying to get mycelium IPs")
361+
}
355362

356363
var hostPeers []string
357-
if err != nil {
364+
if err := backoff.RetryNotify(op, bo, notify); err != nil {
358365
log.Warn().Msg("failed to get IPs after 1 minute, falling back to public mycelium peers")
359366
peers, err := mycelium.FindPeers(context.Background(), nil)
360367
if err != nil {
@@ -365,6 +372,7 @@ func getMyceliumPeers() ([]string, error) {
365372
hostPeers = baseifaceutil.BuildMyceliumPeerURLs(ips)
366373
}
367374

375+
log.Debug().Str("peers", fmt.Sprintf("%v", hostPeers)).Msg("mycelium peers discovered")
368376
return hostPeers, nil
369377
}
370378

0 commit comments

Comments
 (0)