From 889341152a09a67df8f83ad5a52766700ef4ff34 Mon Sep 17 00:00:00 2001 From: Sam Kleinman Date: Mon, 18 Apr 2022 15:49:11 -0400 Subject: [PATCH] p2p: fix setting in con-tracker (#8370) --- internal/p2p/conn_tracker.go | 3 ++- internal/p2p/conn_tracker_test.go | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/internal/p2p/conn_tracker.go b/internal/p2p/conn_tracker.go index 09673c093..54f9c8980 100644 --- a/internal/p2p/conn_tracker.go +++ b/internal/p2p/conn_tracker.go @@ -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, } } @@ -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 { diff --git a/internal/p2p/conn_tracker_test.go b/internal/p2p/conn_tracker_test.go index 66656e114..daa3351f2 100644 --- a/internal/p2p/conn_tracker_test.go +++ b/internal/p2p/conn_tracker_test.go @@ -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)) + }) + }