Skip to content

Commit 4f7f008

Browse files
committed
eth/protocols/handler: add packet sending condition, prevent send small packets frequently;
1 parent 5742cc2 commit 4f7f008

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

eth/protocols/eth/broadcast.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package eth
1818

1919
import (
2020
"math/big"
21+
"time"
2122

2223
"github.com/ethereum/go-ethereum/common"
2324
"github.com/ethereum/go-ethereum/common/gopool"
@@ -27,7 +28,9 @@ import (
2728
const (
2829
// This is the target size for the packs of transactions or announcements. A
2930
// pack can get larger than this if a single transactions exceeds this size.
30-
maxTxPacketSize = 100 * 1024
31+
maxTxPacketSize = 100 * 1024
32+
minTxPacketSize = 1 * 1024
33+
sendPacketTimeout = 300 * time.Millisecond
3134
)
3235

3336
// blockPropagation is a block propagation event, waiting for its turn in the
@@ -136,14 +139,15 @@ func (p *Peer) broadcastTransactions() {
136139
// node internals and at the same time rate limits queued data.
137140
func (p *Peer) announceTransactions() {
138141
var (
139-
queue []common.Hash // Queue of hashes to announce as transaction stubs
140-
done chan struct{} // Non-nil if background announcer is running
141-
fail = make(chan error, 1) // Channel used to receive network error
142-
failed bool // Flag whether a send failed, discard everything onward
142+
queue []common.Hash // Queue of hashes to announce as transaction stubs
143+
done chan struct{} // Non-nil if background announcer is running
144+
fail = make(chan error, 1) // Channel used to receive network error
145+
failed bool // Flag whether a send failed, discard everything onward
146+
lastSentTime = time.Now()
143147
)
144148
for {
145149
// If there's no in-flight announce running, check if a new one is needed
146-
if done == nil && len(queue) > 0 {
150+
if done == nil && triggerPacketSending(len(queue)*common.HashLength, lastSentTime) {
147151
// Pile transaction hashes until we reach our allowed network limit
148152
var (
149153
count int
@@ -170,6 +174,7 @@ func (p *Peer) announceTransactions() {
170174
close(done)
171175
//p.Log().Trace("Sent transaction announcements", "count", len(pending))
172176
})
177+
lastSentTime = time.Now()
173178
}
174179
}
175180
// Transfer goroutine may or may not have been started, listen for events
@@ -200,3 +205,16 @@ func (p *Peer) announceTransactions() {
200205
}
201206
}
202207
}
208+
209+
// triggerPacketSending if packet reach minTxPacketSize or sendPacketTimeout, it will trigger packet sending
210+
// to prevent only small packets sent frequently in network
211+
func triggerPacketSending(estimateSize int, lastSentTime time.Time) bool {
212+
if estimateSize >= minTxPacketSize {
213+
return true
214+
}
215+
216+
if time.Since(lastSentTime) >= sendPacketTimeout && estimateSize > 0 {
217+
return true
218+
}
219+
return false
220+
}

0 commit comments

Comments
 (0)