Skip to content
This repository has been archived by the owner on May 11, 2022. It is now read-only.

Commit

Permalink
use the protocol list by identify, don't emit chatter on every connec…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
vyzo committed Sep 29, 2018
1 parent 0fdf1b0 commit 46d352f
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions notify.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package autonat

import (
"time"

inet "github.com/libp2p/go-libp2p-net"
peer "github.com/libp2p/go-libp2p-peer"
ma "github.com/multiformats/go-multiaddr"
)

Expand All @@ -14,18 +15,25 @@ func (as *AmbientAutoNAT) OpenedStream(net inet.Network, s inet.Stream) {}
func (as *AmbientAutoNAT) ClosedStream(net inet.Network, s inet.Stream) {}

func (as *AmbientAutoNAT) Connected(net inet.Network, c inet.Conn) {
go func(p peer.ID) {
s, err := as.host.NewStream(as.ctx, p, AutoNATProto)
p := c.RemotePeer()

go func() {
// add some delay for identify
time.Sleep(250 * time.Millisecond)

protos, err := as.host.Peerstore().SupportsProtocols(p, AutoNATProto)
if err != nil {
log.Debugf("error retrieving supported protocols for peer %s: %s", p, err)
return
}
s.Close()

log.Infof("Discovered AutoNAT peer %s", p.Pretty())
as.mx.Lock()
as.peers[p] = struct{}{}
as.mx.Unlock()
}(c.RemotePeer())
if len(protos) > 0 {
log.Infof("Discovered AutoNAT peer %s", p.Pretty())
as.mx.Lock()
as.peers[p] = struct{}{}
as.mx.Unlock()
}
}()
}

func (as *AmbientAutoNAT) Disconnected(net inet.Network, c inet.Conn) {}

0 comments on commit 46d352f

Please sign in to comment.