From e5acd2822e554dd42dd93b90fed9a82d824586d7 Mon Sep 17 00:00:00 2001 From: sukun Date: Thu, 21 Nov 2024 12:02:53 +0530 Subject: [PATCH] add support for conn manager --- core/network/conn.go | 12 ++++++++++++ p2p/net/connmgr/connmgr.go | 5 +++-- p2p/net/connmgr/connmgr_test.go | 8 ++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/core/network/conn.go b/core/network/conn.go index 64aee09ffb..914d3ba878 100644 --- a/core/network/conn.go +++ b/core/network/conn.go @@ -35,6 +35,18 @@ func (c *ConnError) Unwrap() error { return c.TransportError } +const ( + ConnNoError ConnErrorCode = 0 + ConnProtocolNegotiationFailed ConnErrorCode = 1001 + ConnResourceLimitExceeded ConnErrorCode = 1002 + ConnRateLimited ConnErrorCode = 1003 + ConnProtocolViolation ConnErrorCode = 1004 + ConnSupplanted ConnErrorCode = 1005 + ConnGarbageCollected ConnErrorCode = 1006 + ConnShutdown ConnErrorCode = 1007 + ConnGated ConnErrorCode = 1008 +) + // Conn is a connection to a remote peer. It multiplexes streams. // Usually there is no need to use a Conn directly, but it may // be useful to get information about the peer on the other side: diff --git a/p2p/net/connmgr/connmgr.go b/p2p/net/connmgr/connmgr.go index 5033538e3b..c2cd307259 100644 --- a/p2p/net/connmgr/connmgr.go +++ b/p2p/net/connmgr/connmgr.go @@ -175,7 +175,8 @@ func (cm *BasicConnMgr) memoryEmergency() { // Trim connections without paying attention to the silence period. for _, c := range cm.getConnsToCloseEmergency(target) { log.Infow("low on memory. closing conn", "peer", c.RemotePeer()) - c.Close() + + c.CloseWithError(network.ConnGarbageCollected) } // finally, update the last trim time. @@ -388,7 +389,7 @@ func (cm *BasicConnMgr) trim() { // do the actual trim. for _, c := range cm.getConnsToClose() { log.Debugw("closing conn", "peer", c.RemotePeer()) - c.Close() + c.CloseWithError(network.ConnGarbageCollected) } } diff --git a/p2p/net/connmgr/connmgr_test.go b/p2p/net/connmgr/connmgr_test.go index 5955265f9b..b0beecf4e2 100644 --- a/p2p/net/connmgr/connmgr_test.go +++ b/p2p/net/connmgr/connmgr_test.go @@ -33,6 +33,14 @@ func (c *tconn) Close() error { return nil } +func (c *tconn) CloseWithError(code network.ConnErrorCode) error { + atomic.StoreUint32(&c.closed, 1) + if c.disconnectNotify != nil { + c.disconnectNotify(nil, c) + } + return nil +} + func (c *tconn) isClosed() bool { return atomic.LoadUint32(&c.closed) == 1 }