1+ package org .tron .common .overlay .server ;
2+
3+ import java .util .ArrayList ;
4+ import java .util .Collections ;
5+ import java .util .List ;
6+ import java .util .concurrent .Executors ;
7+ import java .util .concurrent .ScheduledExecutorService ;
8+ import java .util .concurrent .TimeUnit ;
9+ import javax .annotation .PostConstruct ;
10+ import javax .annotation .PreDestroy ;
11+ import lombok .extern .slf4j .Slf4j ;
12+ import org .springframework .beans .factory .annotation .Autowired ;
13+ import org .springframework .stereotype .Service ;
14+ import org .tron .common .overlay .discover .node .statistics .NodeStatistics ;
15+ import org .tron .core .config .args .Args ;
16+ import org .tron .core .net .peer .PeerConnection ;
17+ import org .tron .protos .Protocol .ReasonCode ;
18+
19+ @ Slf4j
20+ @ Service
21+ public class PeerConnectionCheckService {
22+
23+ public static final long CHECK_TIME = 5 * 60 * 1000L ;
24+ private double disconnectNumberFactor = Args .getInstance ().getDisconnectNumberFactor ();
25+ private double maxConnectNumberFactor = Args .getInstance ().getMaxConnectNumberFactor ();
26+
27+ @ Autowired
28+ private SyncPool pool ;
29+
30+ @ Autowired
31+ private ChannelManager channelManager ;
32+
33+ private ScheduledExecutorService scheduledExecutorService = Executors .newScheduledThreadPool (1 ,
34+ r -> new Thread (r , "check-peer-connect" ));
35+
36+ @ PostConstruct
37+ public void check () {
38+ logger .info ("start the PeerConnectionCheckService" );
39+ scheduledExecutorService
40+ .scheduleWithFixedDelay (new CheckDataTransferTask (), 5 , 5 , TimeUnit .MINUTES );
41+ }
42+
43+ @ PreDestroy
44+ public void destroy () {
45+ scheduledExecutorService .shutdown ();
46+ }
47+
48+ private class CheckDataTransferTask implements Runnable {
49+
50+ @ Override
51+ public void run () {
52+ List <PeerConnection > peerConnectionList = pool .getActivePeers ();
53+ List <Channel > willDisconnectPeerList = new ArrayList <>();
54+ for (PeerConnection peerConnection : peerConnectionList ) {
55+ NodeStatistics nodeStatistics = peerConnection .getNodeStatistics ();
56+ if (!nodeStatistics .nodeIsHaveDataTransfer ()
57+ && System .currentTimeMillis () - peerConnection .getStartTime () >= CHECK_TIME
58+ && !channelManager .getTrustPeers ().containsKey (peerConnection .getInetAddress ())
59+ && !nodeStatistics .isPredefined ()) {
60+ //&& !peerConnection.isActive()
61+ //if xxx minutes not have data transfer,disconnect the peer,exclude trust peer and active peer
62+ willDisconnectPeerList .add (peerConnection );
63+ }
64+ nodeStatistics .resetTcpFlow ();
65+ }
66+ if (!willDisconnectPeerList .isEmpty () && peerConnectionList .size ()
67+ > Args .getInstance ().getNodeMaxActiveNodes () * maxConnectNumberFactor ) {
68+ Collections .shuffle (willDisconnectPeerList );
69+ for (int i = 0 ; i < willDisconnectPeerList .size () * disconnectNumberFactor ; i ++) {
70+ logger .error ("{} not have data transfer, disconnect the peer" ,
71+ willDisconnectPeerList .get (i ).getInetAddress ());
72+ willDisconnectPeerList .get (i ).disconnect (ReasonCode .TOO_MANY_PEERS );
73+ }
74+ }
75+ }
76+ }
77+
78+ }
0 commit comments