Skip to content

Commit

Permalink
add support for conn manager
Browse files Browse the repository at this point in the history
  • Loading branch information
sukunrt committed Nov 21, 2024
1 parent 58f5b89 commit e5acd28
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
12 changes: 12 additions & 0 deletions core/network/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 3 additions & 2 deletions p2p/net/connmgr/connmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}
}

Expand Down
8 changes: 8 additions & 0 deletions p2p/net/connmgr/connmgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit e5acd28

Please sign in to comment.