status-go/metrics/node/subscribe.go
frank 38308d48f2
feat_: log on panic (#5849)
* feat_: log error and stacktrace when panic in goroutine

* test_: add test TestSafeGo

* chore_: rename logAndCall to call

* chore_: rename SafeGo to Go

* chore_: make lint-fix

* chore_: use t.Cleanup

* chore_: Revert "chore_: use t.Cleanup"

This reverts commit 4eb420d179cc0e208e84c13cb941e6b3d1ed9819.

* chore_: Revert "chore_: make lint-fix"

This reverts commit fcc995f157e671a4229b47419c3a0e4004b5fdab.

* chore_: Revert "chore_: rename SafeGo to Go"

This reverts commit a6d73d6df583f313032d79aac62f66328039cb55.

* chore_: Revert "chore_: rename logAndCall to call"

This reverts commit 8fbe993bedb9fbba67349a44f151e2dd5e3bc4cc.

* chore_: Revert "test_: add test TestSafeGo"

This reverts commit a1fa91839f3960398980c6bf456e6462ec944819.

* chore_: Revert "feat_: log error and stacktrace when panic in goroutine"

This reverts commit f612dd828fa2ce410d0e806fe773ecbe3e86a68a.

* feat_: log error and stacktrace when panic in goroutine

* chore_: make lint-fix

* chore_: rename logAndCall to call

* chore_: renaming LogOnPanic

* chore_: update rest goroutine function calls

* chore_: make lint-fix
2024-09-27 06:37:32 +08:00

71 lines
2.5 KiB
Go

package node
import (
"context"
"errors"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/p2p"
"github.com/status-im/status-go/common"
)
// All general log messages in this package should be routed through this logger.
var logger = log.New("package", "status-go/metrics/node")
// SubscribeServerEvents subscribes to server and listens to
// PeerEventTypeAdd and PeerEventTypeDrop events.
func SubscribeServerEvents(ctx context.Context, node *node.Node) error {
server := node.Server()
if server == nil {
return errors.New("server is unavailable")
}
ch := make(chan *p2p.PeerEvent, server.MaxPeers)
subscription := server.SubscribeEvents(ch)
defer subscription.Unsubscribe()
logger.Debug("Subscribed to server events")
for {
select {
case event := <-ch:
if isAddDropPeerEvent(event.Type) {
// We start a goroutine here because updateNodeMetrics
// is calling a method on the p2p server, which
// blocks until the server is available:
// https://github.com/status-im/status-go/blob/e60f425b45d00d3880b42fdd77b460ec465a9f55/vendor/github.com/ethereum/go-ethereum/p2p/server.go#L301
// https://github.com/status-im/status-go/blob/e60f425b45d00d3880b42fdd77b460ec465a9f55/vendor/github.com/ethereum/go-ethereum/p2p/server.go#L746
// If there's back-pressure on the peer event feed
// https://github.com/status-im/status-go/blob/e60f425b45d00d3880b42fdd77b460ec465a9f55/vendor/github.com/ethereum/go-ethereum/p2p/server.go#L783
// The event channel above might become full while updateNodeMetrics
// is called, which means is never consumed, the server blocks on publishing on
// it, and the two will deadlock (server waits for the channel above to be consumed,
// this code waits for the server to respond to peerCount, which is in the same
// event loop).
// Calling it in a different go-routine will allow this code to keep
// processing peer added events, therefore the server will not lock and
// keep processing requests.
go func() {
defer common.LogOnPanic()
if err := updateNodeMetrics(node, event.Type); err != nil {
logger.Error("failed to update node metrics", "err", err)
}
}()
}
case err := <-subscription.Err():
if err != nil {
logger.Error("Subscription failed", "err", err)
}
return err
case <-ctx.Done():
return nil
}
}
}
func isAddDropPeerEvent(eventType p2p.PeerEventType) bool {
return eventType == p2p.PeerEventTypeAdd || eventType == p2p.PeerEventTypeDrop
}