Skip to content

Commit 18c0cc7

Browse files
committed
p2p/discover: simplify table meterics tracking
1 parent 88353a8 commit 18c0cc7

File tree

4 files changed

+30
-14
lines changed

4 files changed

+30
-14
lines changed

p2p/discover/table.go

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ type Table struct {
8181
closeReq chan struct{}
8282
closed chan struct{}
8383

84-
nodeAddedHook func(*node) // for testing
84+
nodeAddedHook func(*bucket, *node)
85+
nodeRemovedHook func(*bucket, *node)
8586
}
8687

8788
// transport is implemented by the UDP transports.
@@ -131,6 +132,22 @@ func newTable(t transport, db *enode.DB, cfg Config) (*Table, error) {
131132
return tab, nil
132133
}
133134

135+
func newMeteredTable(t transport, db *enode.DB, cfg Config) (*Table, error) {
136+
tab, err := newTable(t, db, cfg)
137+
if err != nil {
138+
return nil, err
139+
}
140+
if metrics.Enabled {
141+
tab.nodeAddedHook = func(b *bucket, n *node) {
142+
bucketsCounter[b.index].Inc(1)
143+
}
144+
tab.nodeRemovedHook = func(b *bucket, n *node) {
145+
bucketsCounter[b.index].Dec(1)
146+
}
147+
}
148+
return tab, nil
149+
}
150+
134151
// Nodes returns all nodes contained in the table.
135152
func (tab *Table) Nodes() []*enode.Node {
136153
if !tab.isInitDone() {
@@ -498,10 +515,7 @@ func (tab *Table) addSeenNode(n *node) {
498515
n.addedAt = time.Now()
499516

500517
if tab.nodeAddedHook != nil {
501-
tab.nodeAddedHook(n)
502-
}
503-
if metrics.Enabled {
504-
bucketsCounter[b.index].Inc(1)
518+
tab.nodeAddedHook(b, n)
505519
}
506520
}
507521

@@ -545,10 +559,7 @@ func (tab *Table) addVerifiedNode(n *node) {
545559
n.addedAt = time.Now()
546560

547561
if tab.nodeAddedHook != nil {
548-
tab.nodeAddedHook(n)
549-
}
550-
if metrics.Enabled {
551-
bucketsCounter[b.index].Inc(1)
562+
tab.nodeAddedHook(b, n)
552563
}
553564
}
554565

@@ -647,11 +658,16 @@ func (tab *Table) bumpInBucket(b *bucket, n *node) bool {
647658
}
648659

649660
func (tab *Table) deleteInBucket(b *bucket, n *node) {
650-
if metrics.Enabled && contains(b.entries, n.ID()) {
651-
bucketsCounter[b.index].Dec(1)
661+
// Check if the node is actually in the bucket so the removed hook
662+
// isn't called multiple times for the same node.
663+
if !contains(b.entries, n.ID()) {
664+
return
652665
}
653666
b.entries = deleteNode(b.entries, n)
654667
tab.removeIP(b, n.IP())
668+
if tab.nodeRemovedHook != nil {
669+
tab.nodeRemovedHook(b, n)
670+
}
655671
}
656672

657673
func contains(ns []*node, id enode.ID) bool {

p2p/discover/v4_udp.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func ListenV4(c UDPConn, ln *enode.LocalNode, cfg Config) (*UDPv4, error) {
142142
log: cfg.Log,
143143
}
144144

145-
tab, err := newTable(t, ln.Database(), cfg)
145+
tab, err := newMeteredTable(t, ln.Database(), cfg)
146146
if err != nil {
147147
return nil, err
148148
}

p2p/discover/v4_udp_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ func TestUDPv4_pingMatchIP(t *testing.T) {
394394
func TestUDPv4_successfulPing(t *testing.T) {
395395
test := newUDPTest(t)
396396
added := make(chan *node, 1)
397-
test.table.nodeAddedHook = func(n *node) { added <- n }
397+
test.table.nodeAddedHook = func(b *bucket, n *node) { added <- n }
398398
defer test.close()
399399

400400
// The remote side sends a ping packet to initiate the exchange.

p2p/discover/v5_udp.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ func newUDPv5(conn UDPConn, ln *enode.LocalNode, cfg Config) (*UDPv5, error) {
174174
cancelCloseCtx: cancelCloseCtx,
175175
}
176176
t.talk = newTalkSystem(t)
177-
tab, err := newTable(t, t.db, cfg)
177+
tab, err := newMeteredTable(t, t.db, cfg)
178178
if err != nil {
179179
return nil, err
180180
}

0 commit comments

Comments
 (0)