mirror of
https://github.com/tendermint/tendermint.git
synced 2026-01-10 06:57:24 +00:00
(cherry picked from commit 889341152a)
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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))
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user