Skip to content

Commit 6a3d61b

Browse files
Add reconnect cooldown period for relay limiter
1 parent 43e78df commit 6a3d61b

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

wsbroadcastserver/connectionlimiter.go

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package wsbroadcastserver
66
import (
77
"net"
88
"sync"
9+
"time"
910

1011
"github.com/ethereum/go-ethereum/log"
1112
"github.com/ethereum/go-ethereum/metrics"
@@ -17,24 +18,27 @@ var (
1718
)
1819

1920
type ConnectionLimiterConfig struct {
20-
Enable bool `koanf:"enable" reload:"hot"`
21-
PerIpLimit int `koanf:"per-ip-limit" reload:"hot"`
22-
PerIpv6Cidr48Limit int `koanf:"per-ipv6-cidr-48-limit" reload:"hot"`
23-
PerIpv6Cidr64Limit int `koanf:"per-ipv6-cidr-64-limit" reload:"hot"`
21+
Enable bool `koanf:"enable" reload:"hot"`
22+
PerIpLimit int `koanf:"per-ip-limit" reload:"hot"`
23+
PerIpv6Cidr48Limit int `koanf:"per-ipv6-cidr-48-limit" reload:"hot"`
24+
PerIpv6Cidr64Limit int `koanf:"per-ipv6-cidr-64-limit" reload:"hot"`
25+
ReconnectCooldownPeriod time.Duration `koanf:"reconnect-cooldown-period" reload:"hot"`
2426
}
2527

2628
var DefaultConnectionLimiterConfig = ConnectionLimiterConfig{
27-
Enable: false,
28-
PerIpLimit: 5,
29-
PerIpv6Cidr48Limit: 20,
30-
PerIpv6Cidr64Limit: 10,
29+
Enable: false,
30+
PerIpLimit: 5,
31+
PerIpv6Cidr48Limit: 20,
32+
PerIpv6Cidr64Limit: 10,
33+
ReconnectCooldownPeriod: 0,
3134
}
3235

3336
func ConnectionLimiterConfigAddOptions(prefix string, f *flag.FlagSet) {
3437
f.Bool(prefix+".enable", DefaultConnectionLimiterConfig.Enable, "enable broadcaster per-client connection limiting")
3538
f.Int(prefix+".per-ip-limit", DefaultConnectionLimiterConfig.PerIpLimit, "limit clients, as identified by IPv4/v6 address, to this many connections to this relay")
3639
f.Int(prefix+".per-ipv6-cidr-48-limit", DefaultConnectionLimiterConfig.PerIpv6Cidr48Limit, "limit ipv6 clients, as identified by IPv6 address masked with /48, to this many connections to this relay")
3740
f.Int(prefix+".per-ipv6-cidr-64-limit", DefaultConnectionLimiterConfig.PerIpv6Cidr64Limit, "limit ipv6 clients, as identified by IPv6 address masked with /64, to this many connections to this relay")
41+
f.Duration(prefix+".reconnect-cooldown-period", DefaultConnectionLimiterConfig.ReconnectCooldownPeriod, "time to wait after a relay client disconnects before the disconnect is registered with respect to the limit for this client")
3842
}
3943

4044
type ConnectionLimiterConfigFetcher func() *ConnectionLimiterConfig
@@ -148,9 +152,17 @@ func (l *ConnectionLimiter) Register(ip net.IP) bool {
148152
}
149153

150154
func (l *ConnectionLimiter) Release(ip net.IP) {
151-
l.Lock()
152-
defer l.Unlock()
153-
154-
l.updateUsage(ip, false)
155-
155+
p := l.config().ReconnectCooldownPeriod
156+
if p > 0 {
157+
go func() {
158+
time.Sleep(p)
159+
l.Lock()
160+
defer l.Unlock()
161+
l.updateUsage(ip, false)
162+
}()
163+
} else {
164+
l.Lock()
165+
defer l.Unlock()
166+
l.updateUsage(ip, false)
167+
}
156168
}

0 commit comments

Comments
 (0)