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

Commit

Permalink
cancel the ctx when closing, use a sync.Once to only close once
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Sep 8, 2021
1 parent 654e685 commit 7ad413c
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ type Swarm struct {
limiter *dialLimiter
gater connmgr.ConnectionGater

closeOnce sync.Once
ctx context.Context // is canceled when Close is called
ctxCancel context.CancelFunc

Expand Down Expand Up @@ -131,8 +132,14 @@ func NewSwarm(local peer.ID, peers peerstore.Peerstore, bwc metrics.Reporter, ex
}

func (s *Swarm) Close() error {
// Prevents new connections and/or listeners from being added to the swarm.
s.closeOnce.Do(s.close)
return nil
}

func (s *Swarm) close() {
s.ctxCancel()

// Prevents new connections and/or listeners from being added to the swarm.
s.listeners.Lock()
listeners := s.listeners.m
s.listeners.m = nil
Expand Down Expand Up @@ -187,8 +194,6 @@ func (s *Swarm) Close() error {
}
}
wg.Wait()

return nil
}

func (s *Swarm) addConn(tc transport.CapableConn, dir network.Direction) (*Conn, error) {
Expand Down

0 comments on commit 7ad413c

Please sign in to comment.