Skip to content

Commit

Permalink
p2p: fix setting in con-tracker (#8370) (#8371)
Browse files Browse the repository at this point in the history
(cherry picked from commit 8893411)
  • Loading branch information
mergify[bot] authored Apr 20, 2022
1 parent 21d6844 commit 14f0d60
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
3 changes: 2 additions & 1 deletion internal/p2p/conn_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func newConnTracker(max uint, window time.Duration) connectionTracker {
cache: make(map[string]uint),
lastConnect: make(map[string]time.Time),
max: max,
window: window,
}
}

Expand All @@ -43,7 +44,7 @@ func (rat *connTrackerImpl) AddConn(addr net.IP) error {
if num := rat.cache[address]; num >= rat.max {
return fmt.Errorf("%q has %d connections [max=%d]", address, num, rat.max)
} else if num == 0 {
// if there is already at least connection, check to
// if there is already at least one connection, check to
// see if it was established before within the window,
// and error if so.
if last := rat.lastConnect[address]; time.Since(last) < rat.window {
Expand Down
11 changes: 11 additions & 0 deletions internal/p2p/conn_tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,15 @@ func TestConnTracker(t *testing.T) {
}
require.Equal(t, 10, ct.Len())
})
t.Run("Window", func(t *testing.T) {
const window = 100 * time.Millisecond
ct := newConnTracker(10, window)
ip := randLocalIPv4()
require.NoError(t, ct.AddConn(ip))
ct.RemoveConn(ip)
require.Error(t, ct.AddConn(ip))
time.Sleep(window)
require.NoError(t, ct.AddConn(ip))
})

}

0 comments on commit 14f0d60

Please sign in to comment.