Skip to content

Commit c2adb7d

Browse files
committed
fix(net):fix P2P message rate limit issue
1 parent e50e11f commit c2adb7d

File tree

4 files changed

+13
-9
lines changed

4 files changed

+13
-9
lines changed

framework/src/main/java/org/tron/core/net/P2pRateLimiter.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,23 @@
66

77
public class P2pRateLimiter {
88
private final Cache<Byte, RateLimiter> rateLimiters = CacheBuilder.newBuilder()
9-
.maximumSize(256).build();
9+
.maximumSize(32).build();
1010

11-
public void register (Byte type, double rate) {
12-
rateLimiters.put(type, RateLimiter.create(rate));
11+
public void register(Byte type, double rate) {
12+
RateLimiter rateLimiter = RateLimiter.create(Double.POSITIVE_INFINITY);
13+
rateLimiter.setRate(rate);
14+
rateLimiters.put(type, rateLimiter);
1315
}
1416

15-
public void acquire (Byte type) {
17+
public void acquire(Byte type) {
1618
RateLimiter rateLimiter = rateLimiters.getIfPresent(type);
1719
if (rateLimiter == null) {
1820
return;
1921
}
2022
rateLimiter.acquire();
2123
}
2224

23-
public boolean tryAcquire (Byte type) {
25+
public boolean tryAcquire(Byte type) {
2426
RateLimiter rateLimiter = rateLimiters.getIfPresent(type);
2527
if (rateLimiter == null) {
2628
return true;

framework/src/main/java/org/tron/core/net/peer/PeerConnection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public void setChannel(Channel channel) {
173173
this.nodeStatistics = TronStatsManager.getNodeStatistics(channel.getInetAddress());
174174
lastInteractiveTime = System.currentTimeMillis();
175175
p2pRateLimiter.register(SYNC_BLOCK_CHAIN.asByte(), 2);
176-
p2pRateLimiter.register(FETCH_INV_DATA.asByte(), 1);
176+
p2pRateLimiter.register(FETCH_INV_DATA.asByte(), 2);
177177
p2pRateLimiter.register(P2P_DISCONNECT.asByte(), 1);
178178
}
179179

framework/src/test/java/org/tron/core/net/P2pRateLimiterTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ public void test() {
1212
P2pRateLimiter limiter = new P2pRateLimiter();
1313
limiter.register(SYNC_BLOCK_CHAIN.asByte(), 2);
1414
limiter.acquire(SYNC_BLOCK_CHAIN.asByte());
15-
limiter.tryAcquire(SYNC_BLOCK_CHAIN.asByte());
1615
boolean ret = limiter.tryAcquire(SYNC_BLOCK_CHAIN.asByte());
16+
Assert.assertTrue(ret);
17+
limiter.tryAcquire(SYNC_BLOCK_CHAIN.asByte());
18+
ret = limiter.tryAcquire(SYNC_BLOCK_CHAIN.asByte());
1719
Assert.assertFalse(ret);
1820
ret = limiter.tryAcquire(FETCH_INV_DATA.asByte());
1921
Assert.assertTrue(ret);

framework/src/test/java/org/tron/core/net/messagehandler/FetchInvDataMsgHandlerTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.tron.core.net.messagehandler;
22

3+
import static org.tron.core.net.message.MessageTypes.FETCH_INV_DATA;
4+
35
import com.google.common.cache.Cache;
46
import com.google.common.cache.CacheBuilder;
57
import java.lang.reflect.Field;
@@ -22,8 +24,6 @@
2224
import org.tron.core.net.service.adv.AdvService;
2325
import org.tron.protos.Protocol;
2426

25-
import static org.tron.core.net.message.MessageTypes.FETCH_INV_DATA;
26-
2727
public class FetchInvDataMsgHandlerTest {
2828

2929
@Test

0 commit comments

Comments
 (0)