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

fix(p2p/discovery): Unregister metrics callback on close #3280

Merged
merged 2 commits into from
Apr 5, 2024
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
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
Loading