feat:make discv5 timeout configuable

Signed-off-by: Chen Kai <281165273grape@gmail.com>
This commit is contained in:
Chen Kai 2025-02-03 16:34:41 +08:00
parent 55a18616b1
commit 6078fe2adc
2 changed files with 26 additions and 22 deletions

View file

@ -49,8 +49,9 @@ type Config struct {
// All remaining settings are optional.
// Packet handling configuration:
NetRestrict *netutil.Netlist // list of allowed IP networks
Unhandled chan<- ReadPacket // unhandled packets are sent on this channel
NetRestrict *netutil.Netlist // list of allowed IP networks
Unhandled chan<- ReadPacket // unhandled packets are sent on this channel
V5RespTimeout time.Duration // timeout for v5 queries
// Node table configuration:
Bootnodes []*enode.Node // list of bootstrap nodes
@ -73,6 +74,9 @@ func (cfg Config) withDefaults() Config {
if cfg.RefreshInterval == 0 {
cfg.RefreshInterval = 30 * time.Minute
}
if cfg.V5RespTimeout == 0 {
cfg.V5RespTimeout = 700 * time.Millisecond
}
// Debug/test settings:
if cfg.Log == nil {

View file

@ -42,8 +42,6 @@ const (
lookupRequestLimit = 3 // max requests against a single node during lookup
findnodeResultLimit = 16 // applies in FINDNODE handler
totalNodesResponseLimit = 5 // applies in waitForNodes
respTimeoutV5 = 700 * time.Millisecond
)
// codecV5 is implemented by v5wire.Codec (and testCodec).
@ -62,15 +60,16 @@ type codecV5 interface {
// UDPv5 is the implementation of protocol version 5.
type UDPv5 struct {
// static fields
conn UDPConn
tab *Table
netrestrict *netutil.Netlist
priv *ecdsa.PrivateKey
localNode *enode.LocalNode
db *enode.DB
log log.Logger
clock mclock.Clock
validSchemes enr.IdentityScheme
conn UDPConn
tab *Table
netrestrict *netutil.Netlist
priv *ecdsa.PrivateKey
localNode *enode.LocalNode
db *enode.DB
log log.Logger
clock mclock.Clock
validSchemes enr.IdentityScheme
v5respTimeout time.Duration
// misc buffers used during message handling
logcontext []interface{}
@ -150,14 +149,15 @@ func newUDPv5(conn UDPConn, ln *enode.LocalNode, cfg Config) (*UDPv5, error) {
cfg = cfg.withDefaults()
t := &UDPv5{
// static fields
conn: newMeteredConn(conn),
localNode: ln,
db: ln.Database(),
netrestrict: cfg.NetRestrict,
priv: cfg.PrivateKey,
log: cfg.Log,
validSchemes: cfg.ValidSchemes,
clock: cfg.Clock,
conn: newMeteredConn(conn),
localNode: ln,
db: ln.Database(),
netrestrict: cfg.NetRestrict,
priv: cfg.PrivateKey,
log: cfg.Log,
validSchemes: cfg.ValidSchemes,
clock: cfg.Clock,
v5respTimeout: cfg.V5RespTimeout,
// channels into dispatch
packetInCh: make(chan ReadPacket, 1),
readNextCh: make(chan struct{}, 1),
@ -576,7 +576,7 @@ func (t *UDPv5) startResponseTimeout(c *callV5) {
timer mclock.Timer
done = make(chan struct{})
)
timer = t.clock.AfterFunc(respTimeoutV5, func() {
timer = t.clock.AfterFunc(t.v5respTimeout, func() {
<-done
select {
case t.respTimeoutCh <- &callTimeout{c, timer}: