Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

swarm: add ListenClose #1586

Merged
merged 3 commits into from
Jun 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 41 additions & 7 deletions p2p/net/swarm/swarm_listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/transport"

ma "github.com/multiformats/go-multiaddr"
)
Expand Down Expand Up @@ -35,6 +36,27 @@ func (s *Swarm) Listen(addrs ...ma.Multiaddr) error {
return nil
}

// ListenClose stop and delete listeners for all of the given addresses.
func (s *Swarm) ListenClose(addrs ...ma.Multiaddr) {
var listenersToClose []transport.Listener

s.listeners.Lock()
for l := range s.listeners.m {
if !containsMultiaddr(addrs, l.Multiaddr()) {
continue
}

delete(s.listeners.m, l)
listenersToClose = append(listenersToClose, l)
}
s.listeners.cacheEOL = time.Time{}
s.listeners.Unlock()

for _, l := range listenersToClose {
l.Close()
birros marked this conversation as resolved.
Show resolved Hide resolved
}
}

// AddListenAddr tells the swarm to listen on a single address. Unlike Listen,
// this method does not attempt to filter out bad addresses.
func (s *Swarm) AddListenAddr(a ma.Multiaddr) error {
Expand Down Expand Up @@ -78,12 +100,19 @@ func (s *Swarm) AddListenAddr(a ma.Multiaddr) error {

go func() {
defer func() {
list.Close()
s.listeners.Lock()
delete(s.listeners.m, list)
s.listeners.cacheEOL = time.Time{}
_, ok := s.listeners.m[list]
if ok {
delete(s.listeners.m, list)
s.listeners.cacheEOL = time.Time{}
}
s.listeners.Unlock()

if ok {
list.Close()
log.Errorf("swarm listener unintentionally closed")
}

// signal to our notifiees on listen close.
s.notifyAll(func(n network.Notifiee) {
n.ListenClose(s, maddr)
Expand All @@ -93,10 +122,6 @@ func (s *Swarm) AddListenAddr(a ma.Multiaddr) error {
for {
c, err := list.Accept()
if err != nil {
if s.ctx.Err() == nil {
birros marked this conversation as resolved.
Show resolved Hide resolved
// only log if the swarm is still running.
log.Errorf("swarm listener accept error: %s", err)
}
return
}

Expand All @@ -119,3 +144,12 @@ func (s *Swarm) AddListenAddr(a ma.Multiaddr) error {
}()
return nil
}

func containsMultiaddr(addrs []ma.Multiaddr, addr ma.Multiaddr) bool {
for _, a := range addrs {
if addr == a {
return true
}
}
return false
}
19 changes: 19 additions & 0 deletions p2p/net/swarm/swarm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,3 +542,22 @@ func TestResourceManagerAcceptStream(t *testing.T) {
}
require.EqualError(t, err, "stream reset")
}

func TestListenCloseCount(t *testing.T) {
s := GenSwarm(t, OptDialOnly)
addrsToListen := []ma.Multiaddr{
ma.StringCast("/ip4/0.0.0.0/tcp/0"),
ma.StringCast("/ip4/0.0.0.0/udp/0/quic"),
}

if err := s.Listen(addrsToListen...); err != nil {
t.Fatal(err)
}
listenedAddrs := s.ListenAddresses()
require.Equal(t, 2, len(listenedAddrs))

s.ListenClose(listenedAddrs...)

remainingAddrs := s.ListenAddresses()
require.Equal(t, 0, len(remainingAddrs))
}