Skip to content

Commit a910708

Browse files
authored
chore: remove unreadable code, move a test function to test code, better locking in webrtc control reader
1 parent 16d3bff commit a910708

File tree

4 files changed

+15
-19
lines changed

4 files changed

+15
-19
lines changed

p2p/discovery/backoff/backoffcache.go

-8
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,6 @@ func (c realClock) Now() time.Time {
8282
return time.Now()
8383
}
8484

85-
// withClock lets you override the default time.Now() call. Useful for tests.
86-
func withClock(c clock) BackoffDiscoveryOption {
87-
return func(b *BackoffDiscovery) error {
88-
b.clock = c
89-
return nil
90-
}
91-
}
92-
9385
type backoffCache struct {
9486
// strat is assigned on creation and not written to
9587
strat BackoffStrategy

p2p/discovery/backoff/backoffcache_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,14 @@ func assertNumPeersWithLimit(t *testing.T, ctx context.Context, d discovery.Disc
8080
}
8181
}
8282

83+
// withClock lets you override the default time.Now() call. Useful for tests.
84+
func withClock(c clock) BackoffDiscoveryOption {
85+
return func(b *BackoffDiscovery) error {
86+
b.clock = c
87+
return nil
88+
}
89+
}
90+
8391
func TestBackoffDiscoverySingleBackoff(t *testing.T) {
8492
ctx, cancel := context.WithCancel(context.Background())
8593
defer cancel()

p2p/host/resource-manager/scope.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,10 @@ func (rc *resources) checkMemory(rsvp int64, prio uint8) error {
118118
threshold, mulOk := mulInt64WithOverflow(1+int64(prio), limit)
119119
if !mulOk {
120120
thresholdBig := big.NewInt(limit)
121-
thresholdBig = thresholdBig.Mul(thresholdBig, big.NewInt(1+int64(prio)))
121+
thresholdBig.Mul(thresholdBig, big.NewInt(1+int64(prio)))
122122
thresholdBig.Rsh(thresholdBig, 8) // Divide 256
123-
if !thresholdBig.IsInt64() {
124-
// Shouldn't happen since the threshold can only be <= limit
125-
threshold = limit
126-
}
123+
// necessarily a Int64 since we multiplied a int64 != MaxInt64 with
124+
// a uint8+1 (max 255+1 = 256) and divided by 256
127125
threshold = thresholdBig.Int64()
128126
} else {
129127
threshold = threshold / 256

p2p/transport/webrtc/stream.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -243,17 +243,15 @@ func (s *stream) spawnControlMessageReader() {
243243
s.setDataChannelReadDeadline(time.Now().Add(-1 * time.Hour))
244244

245245
s.readerMx.Lock()
246-
// We have the lock any readers blocked on reader.ReadMsg have exited.
246+
// We have the lock: any readers blocked on reader.ReadMsg have exited.
247+
s.mx.Lock()
248+
defer s.mx.Unlock()
247249
// From this point onwards only this goroutine will do reader.ReadMsg.
248-
249-
//lint:ignore SA2001 we just want to ensure any exising readers have exited.
250+
// We just wanted to ensure any exising readers have exited.
250251
// Read calls from this point onwards will exit immediately on checking
251252
// s.readState
252253
s.readerMx.Unlock()
253254

254-
s.mx.Lock()
255-
defer s.mx.Unlock()
256-
257255
if s.nextMessage != nil {
258256
s.processIncomingFlag(s.nextMessage.Flag)
259257
s.nextMessage = nil

0 commit comments

Comments
 (0)