1
0
Fork 0
mirror of https://github.com/anyproto/anytype-heart.git synced 2025-06-08 05:47:07 +09:00

GO-4123 do not run if no ifaces

This commit is contained in:
Roman Khafizianov 2024-09-24 18:59:41 +02:00
parent 3607ee9ce5
commit 160fa1f5f9
No known key found for this signature in database
GPG key ID: F07A7D55A2684852
2 changed files with 15 additions and 2 deletions

View file

@ -266,6 +266,14 @@ func (i InterfacesAddrs) SortInterfacesWithPriority(priority []string) {
slices.SortFunc(i.Interfaces, compare)
}
func (i InterfacesAddrs) InterfaceNames() []string {
var names = make([]string, 0, len(i.Interfaces))
for _, iface := range i.Interfaces {
names = append(names, iface.Name)
}
return names
}
func (i InterfacesAddrs) NetInterfaces() []net.Interface {
var s = make([]net.Interface, 0, len(i.Interfaces))
for _, iface := range i.Interfaces {

View file

@ -147,6 +147,7 @@ func (l *localDiscovery) refreshInterfaces(ctx context.Context) (err error) {
newAddrs, err := addrs.GetInterfacesAddrs()
if addrs.NetAddrsEqualUnordered(l.interfacesAddrs.Addrs, newAddrs.Addrs) {
// this optimization allows to save syscalls to get addrs for every iface
// also we may receive a new ip address on the existing interface
l.discoveryPossibilitySwapState(func(currentState DiscoveryPossibility) DiscoveryPossibility {
if currentState != DiscoveryLocalNetworkRestricted {
return currentState
@ -166,7 +167,7 @@ func (l *localDiscovery) refreshInterfaces(ctx context.Context) (err error) {
// so this equal check is more precise
return
}
log.Info("net interfaces configuration changed, restarting mdns server")
log.With(zap.Strings("ifaces", newAddrs.InterfaceNames())).Info("net interfaces configuration changed, restarting mdns server")
l.interfacesAddrs = newAddrs
if l.server != nil {
l.cancel()
@ -174,11 +175,15 @@ func (l *localDiscovery) refreshInterfaces(ctx context.Context) (err error) {
l.closeWait.Wait()
l.closeWait = sync.WaitGroup{}
}
if len(l.interfacesAddrs.Interfaces) == 0 {
return nil
}
l.ctx, l.cancel = context.WithCancel(ctx)
if err = l.startServer(); err != nil {
return fmt.Errorf("starting mdns server: %w", err)
}
l.startQuerying(l.ctx)
log.Debug("mdns server started")
return
}
@ -228,7 +233,7 @@ func (l *localDiscovery) readAnswers(ch chan *zeroconf.ServiceEntry) {
Addrs: portAddrs,
PeerId: entry.Instance,
}
log.Debug("discovered peer", zap.Strings("addrs", peer.Addrs), zap.String("peerId", peer.PeerId))
log.Debug("discovered peer", zap.Strings("addrs", portAddrs), zap.String("peerId", peer.PeerId))
if l.notifier != nil {
l.notifier.PeerDiscovered(peer, OwnAddresses{
Addrs: l.ipv4,