Explorar el Código

增加了拉黑peer功能,避免一直循环相同peer

410 hace 2 años
padre
commit
bfaef9f0b0
Se han modificado 1 ficheros con 39 adiciones y 24 borrados
  1. 39 24
      p2p/server.go

+ 39 - 24
p2p/server.go

@@ -43,28 +43,28 @@ import (
 )
 
 const (
+	// 默认的拨号超时时间,用于控制拨号操作的最长等待时间,默认为15秒。
 	defaultDialTimeout = 15 * time.Second
 
-	// This is the fairness knob for the discovery mixer. When looking for peers, we'll
-	// wait this long for a single source of candidates before moving on and trying other
-	// sources.
+	// 发现混合器的公平性参数。在寻找对等方时,如果从一个源获取候选列表的时间超过该阈值,将切换到其他源进行尝试,默认为5秒。
 	discmixTimeout = 5 * time.Second
 
-	// Connectivity defaults.
-	defaultMaxPendingPeers = 50
-	defaultDialRatio       = 3
+	// 最大挂起对等方数,用于限制同时处于挂起状态的最大对等方连接数量,默认为100个。
+	defaultMaxPendingPeers = 100
 
-	// This time limits inbound connection attempts per source IP.
+	// 拨号比例,默认为2。它用于计算每个主动拨号任务允许的最大并发拨号数量,即拨号槽数量为拨号比例乘以最大挂起对等方数。
+	defaultDialRatio = 2
+
+	// 每个源IP允许的入站连接尝试的最大时间间隔,默认为30秒。超过该时间间隔后,将允许再次尝试建立连接。
 	inboundThrottleTime = 30 * time.Second
 
-	// Maximum time allowed for reading a complete message.
-	// This is effectively the amount of time a connection can be idle.
+	// 读取完整消息的最大时间限制,也是连接可以空闲的最长时间。如果连接处于空闲状态超过该时间,则可能会关闭连接,默认为30秒。
 	frameReadTimeout = 30 * time.Second
 
-	// Maximum amount of time allowed for writing a complete message.
+	// 写入完整消息的最大时间限制。如果写入操作超过该时间,则可能会导致写入失败,默认为20秒。
 	frameWriteTimeout = 20 * time.Second
 
-	// Maximum time to wait before stop the p2p server
+	// 停止P2P服务器的最大等待时间。如果在该时间内无法正常停止服务器,则可能会强制关闭服务器连接,默认为5秒。
 	stopTimeout = 5 * time.Second
 )
 
@@ -221,7 +221,7 @@ const (
 )
 
 // conn wraps a network connection with information gathered
-// during the two handshakes.
+// during the two handshakes.newDialScheduler
 type conn struct {
 	fd net.Conn
 	transport
@@ -605,6 +605,7 @@ func (srv *Server) setupDiscovery() error {
 			return err
 		}
 		srv.ntab = ntab
+		fmt.Println(time.Now().Format("2006-01-02 15:04:05.000000"), "p2p/server.go RandomNodes")
 		srv.discmix.AddSource(ntab.RandomNodes())
 	}
 
@@ -717,6 +718,7 @@ func (srv *Server) run() {
 		peers        = make(map[enode.ID]*Peer)
 		inboundCount = 0
 		trusted      = make(map[enode.ID]bool, len(srv.TrustedNodes))
+		black        = make(map[enode.ID]time.Time)
 	)
 	// Put trusted nodes into a map to speed up checks.
 	// Trusted peers are loaded on startup or added via AddTrustedPeer RPC.
@@ -765,26 +767,39 @@ running:
 			c.cont <- srv.postHandshakeChecks(peers, inboundCount, c)
 
 		case c := <-srv.checkpointAddPeer:
-			// At this point the connection is past the protocol handshake.
-			// Its capabilities are known and the remote identity is verified.
-			err := srv.addPeerChecks(peers, inboundCount, c)
-			if err == nil {
-				// The handshakes are done and it passed all checks.
-				p := srv.launchPeer(c)
-				peers[c.node.ID()] = p
-				srv.log.Debug("Adding p2p peer", "peercount", len(peers), "id", p.ID(), "conn", c.flags, "addr", p.RemoteAddr(), "name", p.Name())
-				srv.dialsched.peerAdded(c)
-				if p.Inbound() {
-					inboundCount++
+
+			currentTime := time.Now()
+
+			if currentTime.Sub(black[c.node.ID()]) > 600*time.Second {
+				// At this point the connection is past the protocol handshake.
+				// Its capabilities are known and the remote identity is verified.
+				err := srv.addPeerChecks(peers, inboundCount, c)
+				if err == nil {
+					//The handshakes are done and it passed all checks.
+					p := srv.launchPeer(c)
+					peers[c.node.ID()] = p
+					srv.log.Debug("Adding p2p peer", "peercount", len(peers), "id", p.ID(), "conn", c.flags, "addr", p.RemoteAddr(), "name", p.Name())
+					srv.dialsched.peerAdded(c)
+					if p.Inbound() {
+						inboundCount++
+					}
 				}
+				c.cont <- err
 			}
-			c.cont <- err
 
 		case pd := <-srv.delpeer:
 			// A peer disconnected.
 			d := common.PrettyDuration(mclock.Now() - pd.created)
 			delete(peers, pd.ID())
 			srv.log.Debug("Removing p2p peer", "peercount", len(peers), "id", pd.ID(), "duration", d, "req", pd.requested, "err", pd.err)
+
+			if trusted[pd.ID()] {
+				fmt.Println(time.Now().Format("2006-01-02 15:04:05.000000"), "p2p/server.go Removing p2p peer", "peercount", len(peers), "addr", pd.RemoteAddr(), "err", pd.err)
+
+			}
+
+			black[pd.ID()] = time.Now()
+
 			srv.dialsched.peerRemoved(pd.rw)
 			if pd.Inbound() {
 				inboundCount--