Skip to content

Commit

Permalink
fix(p2p/discovery): Unregister metrics callback on close (celestiaorg…
Browse files Browse the repository at this point in the history
…#3280)

Self explanatory
  • Loading branch information
renaynay authored Apr 5, 2024
1 parent e338d38 commit 6242fc1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion share/p2p/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (d *Discovery) Start(context.Context) error {

func (d *Discovery) Stop(context.Context) error {
d.cancel()
return nil
return d.metrics.close()
}

// Peers provides a list of discovered peers in the given topic.
Expand Down
13 changes: 12 additions & 1 deletion share/p2p/discovery/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ type metrics struct {
advertise metric.Int64Counter // attributes: failed[bool]
peerAdded metric.Int64Counter
peerRemoved metric.Int64Counter

clientReg metric.Registration
}

// WithMetrics turns on metric collection in discoery.
Expand Down Expand Up @@ -107,13 +109,22 @@ func initMetrics(d *Discovery) (*metrics, error) {
observer.ObserveInt64(backOffSize, int64(d.connector.Size()))
return nil
}
_, err = meter.RegisterCallback(callback, peersAmount, backOffSize)

metrics.clientReg, err = meter.RegisterCallback(callback, peersAmount, backOffSize)
if err != nil {
return nil, fmt.Errorf("registering metrics callback: %w", err)
}

return metrics, nil
}

func (m *metrics) close() error {
if m == nil {
return nil
}
return m.clientReg.Unregister()
}

func (m *metrics) observeFindPeers(ctx context.Context, isEnoughPeers bool) {
if m == nil {
return
Expand Down

0 comments on commit 6242fc1

Please sign in to comment.