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: fix swarm closed connection count #2115

Closed
wants to merge 1 commit into from
Closed
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
16 changes: 11 additions & 5 deletions p2p/net/swarm/limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"sync"
"time"

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

Expand All @@ -20,11 +21,12 @@ type dialResult struct {
}

type dialJob struct {
addr ma.Multiaddr
peer peer.ID
ctx context.Context
resp chan dialResult
timeout time.Duration
addr ma.Multiaddr
peer peer.ID
ctx context.Context
resp chan dialResult
timeout time.Duration
metricsTracer MetricsTracer
}

func (dj *dialJob) cancelled() bool {
Expand Down Expand Up @@ -217,11 +219,15 @@ func (dl *dialLimiter) executeDial(j *dialJob) {
defer cancel()

con, err := dl.dialFunc(dctx, j.peer, j.addr)
st := time.Now()
select {
case j.resp <- dialResult{Conn: con, Addr: j.addr, Err: err}:
case <-j.ctx.Done():
if con != nil {
con.Close()
if j.metricsTracer != nil {
j.metricsTracer.ClosedConnection(network.DirOutbound, time.Since(st), con.ConnState(), con.LocalMultiaddr())
}
}
}
}
6 changes: 6 additions & 0 deletions p2p/net/swarm/swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,9 @@ func (s *Swarm) addConn(tc transport.CapableConn, dir network.Direction) (*Conn,
if err != nil {
log.Warnf("failed to close connection with peer %s and addr %s; err: %s", p.Pretty(), addr, err)
}
if s.metricsTracer != nil {
s.metricsTracer.ClosedConnection(dir, time.Since(stat.Opened), c.ConnState(), c.LocalMultiaddr())
}
return nil, ErrGaterDisallowedConnection
}
}
Expand All @@ -315,6 +318,9 @@ func (s *Swarm) addConn(tc transport.CapableConn, dir network.Direction) (*Conn,
if s.conns.m == nil {
s.conns.Unlock()
tc.Close()
if s.metricsTracer != nil {
s.metricsTracer.ClosedConnection(dir, time.Since(stat.Opened), c.ConnState(), c.LocalMultiaddr())
}
return nil, ErrSwarmClosed
}

Expand Down
11 changes: 6 additions & 5 deletions p2p/net/swarm/swarm_dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,11 +468,12 @@ func (s *Swarm) limitedDial(ctx context.Context, p peer.ID, a ma.Multiaddr, resp
timeout = s.dialTimeoutLocal
}
s.limiter.AddDialJob(&dialJob{
addr: a,
peer: p,
resp: resp,
ctx: ctx,
timeout: timeout,
addr: a,
peer: p,
resp: resp,
ctx: ctx,
timeout: timeout,
metricsTracer: s.metricsTracer,
})
}

Expand Down