Skip to content

Commit 7289141

Browse files
committed
bitswap/httpnet: error_tracker: use normal mutex-locked map.
1 parent ac569b2 commit 7289141

File tree

4 files changed

+19
-77
lines changed

4 files changed

+19
-77
lines changed

bitswap/network/httpnet/error_tracker.go

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package httpnet
33
import (
44
"errors"
55
"sync"
6-
"sync/atomic"
76

87
"github.com/libp2p/go-libp2p/core/peer"
98
)
@@ -14,50 +13,34 @@ type errorTracker struct {
1413
ht *Network
1514

1615
mux sync.RWMutex
17-
errors map[peer.ID]*atomic.Uint64
16+
errors map[peer.ID]int
1817
}
1918

2019
func newErrorTracker(ht *Network) *errorTracker {
2120
return &errorTracker{
2221
ht: ht,
23-
errors: make(map[peer.ID]*atomic.Uint64),
24-
}
25-
}
26-
27-
func (et *errorTracker) startTracking(p peer.ID) {
28-
et.mux.Lock()
29-
defer et.mux.Unlock()
30-
31-
// Initialize the error count for the peer if it doesn't exist
32-
if _, exists := et.errors[p]; !exists {
33-
et.errors[p] = &atomic.Uint64{}
22+
errors: make(map[peer.ID]int),
3423
}
3524
}
3625

3726
func (et *errorTracker) stopTracking(p peer.ID) {
3827
et.mux.Lock()
39-
defer et.mux.Unlock()
4028
delete(et.errors, p)
29+
et.mux.Unlock()
4130
}
4231

4332
// logErrors adds n to the current error count for p. If the total count is above the threshold, then an error is returned. If n is 0, the the total count is reset to 0.
44-
func (et *errorTracker) logErrors(p peer.ID, n uint64, threshold uint64) error {
45-
et.mux.RLock()
46-
count, ok := et.errors[p]
47-
et.mux.RUnlock()
48-
49-
if !ok {
50-
// i.e. we disconnected but there were pending requests
51-
log.Debug("logging errors for untracked peer: %s", p)
52-
return nil
53-
}
33+
func (et *errorTracker) logErrors(p peer.ID, n int, threshold int) error {
34+
et.mux.Lock()
35+
defer et.mux.Unlock()
5436

5537
if n == 0 { // reset error count
56-
count.Store(0)
38+
et.errors[p] = 0
5739
return nil
5840
}
59-
60-
total := count.Add(n)
41+
count, _ := et.errors[p]
42+
total := count + n
43+
et.errors[p] = total
6144
if total > threshold {
6245
return errThresholdCrossed
6346
}

bitswap/network/httpnet/error_tracker_test.go

Lines changed: 6 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,10 @@ import (
88
"github.com/libp2p/go-libp2p/core/peer"
99
)
1010

11-
func TestErrorTracker_StartTracking(t *testing.T) {
12-
et := newErrorTracker(&Network{})
13-
p := peer.ID("testpeer")
14-
15-
// Start tracking the peer
16-
et.startTracking(p)
17-
18-
// Check if the error count is initialized to 0
19-
count, ok := et.errors[p]
20-
if !ok {
21-
t.Errorf("Expected peer %s to be tracked but it was not", p)
22-
}
23-
if count.Load() != 0 {
24-
t.Errorf("Expected initial error count for peer %s to be 0 but got %d", p, count.Load())
25-
}
26-
}
27-
2811
func TestErrorTracker_StopTracking(t *testing.T) {
2912
et := newErrorTracker(&Network{})
3013
p := peer.ID("testpeer")
3114

32-
// Start tracking the peer
33-
et.startTracking(p)
34-
3515
// Stop tracking the peer
3616
et.stopTracking(p)
3717

@@ -45,9 +25,6 @@ func TestErrorTracker_LogErrors_Reset(t *testing.T) {
4525
et := newErrorTracker(&Network{})
4626
p := peer.ID("testpeer")
4727

48-
// Start tracking the peer
49-
et.startTracking(p)
50-
5128
// Log some errors
5229
err := et.logErrors(p, 5, 10)
5330
if err != nil {
@@ -65,18 +42,15 @@ func TestErrorTracker_LogErrors_Reset(t *testing.T) {
6542
if !ok {
6643
t.Errorf("Expected peer %s to be tracked but it was not", p)
6744
}
68-
if count.Load() != 0 {
69-
t.Errorf("Expected error count for peer %s to be 0 after reset but got %d", p, count.Load())
45+
if count != 0 {
46+
t.Errorf("Expected error count for peer %s to be 0 after reset but got %d", p, count)
7047
}
7148
}
7249

7350
func TestErrorTracker_LogErrors_ThresholdCrossed(t *testing.T) {
7451
et := newErrorTracker(&Network{})
7552
p := peer.ID("testpeer")
7653

77-
// Start tracking the peer
78-
et.startTracking(p)
79-
8054
// Log errors until threshold is crossed
8155
err := et.logErrors(p, 11, 10)
8256
if err != errThresholdCrossed {
@@ -88,19 +62,8 @@ func TestErrorTracker_LogErrors_ThresholdCrossed(t *testing.T) {
8862
if !ok {
8963
t.Errorf("Expected peer %s to be tracked but it was not", p)
9064
}
91-
if count.Load() != 11 {
92-
t.Errorf("Expected error count for peer %s to be 10 after logging errors above threshold but got %d", p, count.Load())
93-
}
94-
}
95-
96-
func TestErrorTracker_LogErrors_UntrackedPeer(t *testing.T) {
97-
et := newErrorTracker(&Network{})
98-
p := peer.ID("testpeer")
99-
100-
// Log errors for an untracked peer
101-
err := et.logErrors(p, 5, 10)
102-
if err != nil {
103-
t.Errorf("Expected no error when logging errors for an untracked peer but got %v", err)
65+
if count != 11 {
66+
t.Errorf("Expected error count for peer %s to be 10 after logging errors above threshold but got %d", p, count)
10467
}
10568
}
10669

@@ -109,12 +72,9 @@ func TestErrorTracker_ConcurrentAccess(t *testing.T) {
10972
et := newErrorTracker(&Network{})
11073
p := peer.ID("testpeer")
11174

112-
// Start tracking the peer
113-
et.startTracking(p)
114-
11575
var wg sync.WaitGroup
11676
numRoutines := 10
117-
threshold := uint64(100)
77+
threshold := 100
11878

11979
for i := 0; i < numRoutines; i++ {
12080
wg.Add(1)
@@ -134,7 +94,7 @@ func TestErrorTracker_ConcurrentAccess(t *testing.T) {
13494
t.Errorf("Expected peer %s to be tracked but it was not", p)
13595
}
13696
expectedCount := threshold
137-
actualCount := count.Load()
97+
actualCount := count
13898
if actualCount != expectedCount {
13999
t.Errorf("Expected error count for peer %s to be %d after concurrent logging but got %d", p, expectedCount, actualCount)
140100
}

bitswap/network/httpnet/httpnet.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ func WithHTTPWorkers(n int) Option {
163163
// be that pending requests will still happen. The HTTP connection might be
164164
// kept until it times-out per the IdleConnTimeout. Requests will resume if a
165165
// provider record is found causing us to "reconnect" to the peer.
166-
func WithMaxDontHaveErrors(threshold uint64) Option {
166+
func WithMaxDontHaveErrors(threshold int) Option {
167167
return func(net *Network) {
168168
net.maxDontHaveErrors = threshold
169169
}
@@ -195,7 +195,7 @@ type Network struct {
195195
maxIdleConns int
196196
insecureSkipVerify bool
197197
maxHTTPAddressesPerPeer int
198-
maxDontHaveErrors uint64
198+
maxDontHaveErrors int
199199
httpWorkers int
200200
allowlist map[string]struct{}
201201
denylist map[string]struct{}
@@ -484,7 +484,6 @@ func (ht *Network) Connect(ctx context.Context, pi peer.AddrInfo) error {
484484

485485
ht.connEvtMgr.Connected(p)
486486
ht.pinger.startPinging(p)
487-
ht.errorTracker.startTracking(p)
488487
log.Debugf("connect success to %s (supports HEAD: %t)", p, supportsHead)
489488
// We "connected"
490489
return nil

bitswap/network/httpnet/msg_sender.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ WANTLIST_LOOP:
502502
go func() {
503503
bsresp := bsmsg.New(false)
504504
totalResponses := 0
505-
totalClientErrors := uint64(0)
505+
totalClientErrors := 0
506506

507507
for result := range resultsCollector {
508508
// Record total request time.

0 commit comments

Comments
 (0)