Skip to content

Commit 2454a92

Browse files
committed
feat(net):optimize rate limit logic
1 parent 3b3ddac commit 2454a92

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

framework/src/main/java/org/tron/core/net/messagehandler/SyncBlockChainMsgHandler.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,6 @@ public void processMessage(PeerConnection peer, TronMessage msg) throws P2pExcep
3131

3232
SyncBlockChainMessage syncBlockChainMessage = (SyncBlockChainMessage) msg;
3333

34-
if (!peer.getP2pRateLimiter().tryAcquire(msg.getType().asByte())) {
35-
// Discard messages that exceed the rate limit
36-
logger.warn("{} message from peer {} exceeds the rate limit",
37-
msg.getType(), peer.getInetSocketAddress());
38-
return;
39-
}
40-
4134
if (!check(peer, syncBlockChainMessage)) {
4235
peer.disconnect(Protocol.ReasonCode.BAD_PROTOCOL);
4336
return;
@@ -65,6 +58,14 @@ public void processMessage(PeerConnection peer, TronMessage msg) throws P2pExcep
6558
}
6659

6760
private boolean check(PeerConnection peer, SyncBlockChainMessage msg) throws P2pException {
61+
if (peer.getRemainNum() > 0
62+
&& !peer.getP2pRateLimiter().tryAcquire(msg.getType().asByte())) {
63+
// Discard messages that exceed the rate limit
64+
logger.warn("{} message from peer {} exceeds the rate limit",
65+
msg.getType(), peer.getInetSocketAddress());
66+
return false;
67+
}
68+
6869
List<BlockId> blockIds = msg.getBlockIds();
6970
if (CollectionUtils.isEmpty(blockIds)) {
7071
throw new P2pException(TypeEnum.BAD_MESSAGE, "SyncBlockChain blockIds is empty");

0 commit comments

Comments
 (0)