Skip to content

Commit

Permalink
chore: remove unreadable code, move a test function to test code, bet…
Browse files Browse the repository at this point in the history
…ter locking in webrtc control reader
  • Loading branch information
AnomalRoil authored Mar 6, 2024
1 parent 16d3bff commit a910708
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 19 deletions.
8 changes: 0 additions & 8 deletions p2p/discovery/backoff/backoffcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,6 @@ func (c realClock) Now() time.Time {
return time.Now()
}

// withClock lets you override the default time.Now() call. Useful for tests.
func withClock(c clock) BackoffDiscoveryOption {
return func(b *BackoffDiscovery) error {
b.clock = c
return nil
}
}

type backoffCache struct {
// strat is assigned on creation and not written to
strat BackoffStrategy
Expand Down
8 changes: 8 additions & 0 deletions p2p/discovery/backoff/backoffcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ func assertNumPeersWithLimit(t *testing.T, ctx context.Context, d discovery.Disc
}
}

// withClock lets you override the default time.Now() call. Useful for tests.
func withClock(c clock) BackoffDiscoveryOption {
return func(b *BackoffDiscovery) error {
b.clock = c
return nil
}
}

func TestBackoffDiscoverySingleBackoff(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down
8 changes: 3 additions & 5 deletions p2p/host/resource-manager/scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,10 @@ func (rc *resources) checkMemory(rsvp int64, prio uint8) error {
threshold, mulOk := mulInt64WithOverflow(1+int64(prio), limit)
if !mulOk {
thresholdBig := big.NewInt(limit)
thresholdBig = thresholdBig.Mul(thresholdBig, big.NewInt(1+int64(prio)))
thresholdBig.Mul(thresholdBig, big.NewInt(1+int64(prio)))
thresholdBig.Rsh(thresholdBig, 8) // Divide 256
if !thresholdBig.IsInt64() {
// Shouldn't happen since the threshold can only be <= limit
threshold = limit
}
// necessarily a Int64 since we multiplied a int64 != MaxInt64 with
// a uint8+1 (max 255+1 = 256) and divided by 256
threshold = thresholdBig.Int64()
} else {
threshold = threshold / 256
Expand Down
10 changes: 4 additions & 6 deletions p2p/transport/webrtc/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,17 +243,15 @@ func (s *stream) spawnControlMessageReader() {
s.setDataChannelReadDeadline(time.Now().Add(-1 * time.Hour))

s.readerMx.Lock()
// We have the lock any readers blocked on reader.ReadMsg have exited.
// We have the lock: any readers blocked on reader.ReadMsg have exited.
s.mx.Lock()
defer s.mx.Unlock()
// From this point onwards only this goroutine will do reader.ReadMsg.

//lint:ignore SA2001 we just want to ensure any exising readers have exited.
// We just wanted to ensure any exising readers have exited.
// Read calls from this point onwards will exit immediately on checking
// s.readState
s.readerMx.Unlock()

s.mx.Lock()
defer s.mx.Unlock()

if s.nextMessage != nil {
s.processIncomingFlag(s.nextMessage.Flag)
s.nextMessage = nil
Expand Down

0 comments on commit a910708

Please sign in to comment.