Skip to content

Commit

Permalink
make transport level methods optional
Browse files Browse the repository at this point in the history
  • Loading branch information
sukunrt committed Sep 13, 2024
1 parent ff0b90a commit 235a2da
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 18 deletions.
10 changes: 6 additions & 4 deletions core/network/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
type ConnErrorCode uint32

type ConnError struct {
Remote bool
ErrorCode uint32
}

Expand All @@ -30,15 +31,16 @@ func (c *ConnError) Error() string {
type Conn interface {
io.Closer

// CloseWithError closes the connection with errCode. The errCode is sent
// to the peer.
CloseWithError(errCode ConnErrorCode) error

ConnSecurity
ConnMultiaddrs
ConnStat
ConnScoper

// CloseWithError closes the connection with errCode. The errCode is sent to the
// peer on a best effort basis. For transports that do not support sending error
// codes on connection close, the behavior is identical to calling Close.
CloseWithError(errCode ConnErrorCode) error

// ID returns an identifier that uniquely identifies this Conn within this
// host, during this run. Connection IDs may repeat across restarts.
ID() string
Expand Down
21 changes: 13 additions & 8 deletions core/network/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,18 @@ type MuxedStream interface {
// side to hang up and go away.
Reset() error

// ResetWithError closes both ends of the stream with errCode. The errCode is sent
// to the peer.
ResetWithError(errCode StreamErrorCode) error

SetDeadline(time.Time) error
SetReadDeadline(time.Time) error
SetWriteDeadline(time.Time) error
}

type ResetWithErrorer interface {
// ResetWithError closes both ends of the stream with errCode. The errCode is sent
// to the peer on a best effort basis. For transports that do not support sending
// error codes to remote peer, the behavior is identical to calling Reset
ResetWithError(errCode StreamErrorCode) error
}

// MuxedConn represents a connection to a remote peer that has been
// extended to support stream multiplexing.
//
Expand All @@ -95,10 +98,6 @@ type MuxedConn interface {
// Close closes the stream muxer and the the underlying net.Conn.
io.Closer

// CloseWithError closes the connection with errCode. The errCode is sent
// to the peer.
CloseWithError(errCode ConnErrorCode) error

// IsClosed returns whether a connection is fully closed, so it can
// be garbage collected.
IsClosed() bool
Expand All @@ -110,6 +109,12 @@ type MuxedConn interface {
AcceptStream() (MuxedStream, error)
}

type ConnWithErrorer interface {
// CloseWithError closes the connection with errCode. The errCode is sent
// to the peer.
ConnWithError(errCode ConnErrorCode) error
}

// Multiplexer wraps a net.Conn with a stream multiplexing
// implementation and returns a MuxedConn that supports opening
// multiple streams over the underlying net.Conn
Expand Down
4 changes: 4 additions & 0 deletions core/network/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ type Stream interface {

// Scope returns the user's view of this stream's resource scope
Scope() StreamScope

// ResetWithError closes both ends of the stream with errCode. The errCode is sent
// to the peer.
ResetWithError(errCode StreamErrorCode) error
}
4 changes: 0 additions & 4 deletions p2p/net/mock/mock_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,6 @@ func (c *conn) Close() error {
return nil
}

func (c *conn) CloseWithError(errCode network.ConnErrorCode) error {
panic("not implemented")
}

func (c *conn) teardown() {
for _, s := range c.allStreams() {
s.Reset()
Expand Down
12 changes: 11 additions & 1 deletion p2p/net/swarm/swarm_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,17 @@ func (c *Conn) doClose(errCode network.ConnErrorCode) {
c.streams.m = nil
c.streams.Unlock()

c.err = c.conn.CloseWithError(errCode)
if errCode != 0 {
if ce, ok := c.conn.(interface {
CloseWithError(network.ConnErrorCode) error
}); ok {
c.err = ce.CloseWithError(errCode)
} else {
c.err = c.conn.Close()
}
} else {
c.err = c.conn.Close()
}

// Send the connectedness event after closing the connection.
// This ensures that both remote connection close and local connection
Expand Down
7 changes: 6 additions & 1 deletion p2p/net/swarm/swarm_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,12 @@ func (s *Stream) Reset() error {
}

func (s *Stream) ResetWithError(errCode network.StreamErrorCode) error {
err := s.stream.ResetWithError(errCode)
var err error
if se, ok := s.stream.(network.ResetWithErrorer); ok {
err = se.ResetWithError(errCode)
} else {
err = s.stream.Reset()
}
s.closeAndRemoveStream()
return err
}
Expand Down

0 comments on commit 235a2da

Please sign in to comment.