-
Notifications
You must be signed in to change notification settings - Fork 244
/
subscribe.go
68 lines (59 loc) · 2.45 KB
/
subscribe.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package node
import (
"context"
"errors"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/p2p"
)
// 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() {
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
}