Skip to content

Commit

Permalink
swarm: record conn metrics only once (#3091)
Browse files Browse the repository at this point in the history
  • Loading branch information
sukunrt authored Dec 17, 2024
1 parent 4e85c96 commit 37fbd7e
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions p2p/net/swarm/swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -823,31 +823,36 @@ type connWithMetrics struct {
opened time.Time
dir network.Direction
metricsTracer MetricsTracer
once sync.Once
closeErr error
}

func wrapWithMetrics(capableConn transport.CapableConn, metricsTracer MetricsTracer, opened time.Time, dir network.Direction) connWithMetrics {
c := connWithMetrics{CapableConn: capableConn, opened: opened, dir: dir, metricsTracer: metricsTracer}
func wrapWithMetrics(capableConn transport.CapableConn, metricsTracer MetricsTracer, opened time.Time, dir network.Direction) *connWithMetrics {
c := &connWithMetrics{CapableConn: capableConn, opened: opened, dir: dir, metricsTracer: metricsTracer}
c.metricsTracer.OpenedConnection(c.dir, capableConn.RemotePublicKey(), capableConn.ConnState(), capableConn.LocalMultiaddr())
return c
}

func (c connWithMetrics) completedHandshake() {
func (c *connWithMetrics) completedHandshake() {
c.metricsTracer.CompletedHandshake(time.Since(c.opened), c.ConnState(), c.LocalMultiaddr())
}

func (c connWithMetrics) Close() error {
c.metricsTracer.ClosedConnection(c.dir, time.Since(c.opened), c.ConnState(), c.LocalMultiaddr())
return c.CapableConn.Close()
func (c *connWithMetrics) Close() error {
c.once.Do(func() {
c.metricsTracer.ClosedConnection(c.dir, time.Since(c.opened), c.ConnState(), c.LocalMultiaddr())
c.closeErr = c.CapableConn.Close()
})
return c.closeErr
}

func (c connWithMetrics) Stat() network.ConnStats {
func (c *connWithMetrics) Stat() network.ConnStats {
if cs, ok := c.CapableConn.(network.ConnStat); ok {
return cs.Stat()
}
return network.ConnStats{}
}

var _ network.ConnStat = connWithMetrics{}
var _ network.ConnStat = &connWithMetrics{}

type ResolverFromMaDNS struct {
*madns.Resolver
Expand Down

0 comments on commit 37fbd7e

Please sign in to comment.