Skip to content

Commit

Permalink
Merge pull request #27 from m-lab/replace-tcpconn-with-interface
Browse files Browse the repository at this point in the history
Replace *net.TCPConn with a TCPLikeConn abstraction
  • Loading branch information
robertodauria authored Sep 28, 2023
2 parents 0694eb1 + 6b4f1fc commit d233487
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion internal/handler/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func setupTestWSDialer(u *url.URL) *websocket.Dialer {
if err != nil {
return nil, err
}
return netx.FromTCPConn(conn.(*net.TCPConn))
return netx.FromTCPLikeConn(conn.(*net.TCPConn))
},
}
}
Expand Down
12 changes: 10 additions & 2 deletions internal/netx/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ type ConnInfo interface {
SaveUUID(context.Context) context.Context
}

// TCPLikeConn is a net.Conn with a File() method. This is useful for creating a
// netx.Conn based on a custom TCPConn-like type - e.g. for testing.
type TCPLikeConn interface {
net.Conn
File() (*os.File, error)
}

// ToConnInfo is a helper function to convert a net.Conn into a netx.ConnInfo.
// It panics if netConn does not contain a type supporting ConnInfo.
func ToConnInfo(netConn net.Conn) ConnInfo {
Expand All @@ -57,8 +64,9 @@ type Conn struct {
bytesWritten atomic.Uint64
}

func FromTCPConn(tcpConn *net.TCPConn) (*Conn, error) {
return fromTCPConn(tcpConn)
// FromTCPLikeConn creates a netx.Conn from a TCPLikeConn.
func FromTCPLikeConn(tcpConn TCPLikeConn) (*Conn, error) {
return fromTCPLikeConn(tcpConn)
}

// Read reads from the underlying net.Conn and updates the read bytes counter.
Expand Down
3 changes: 1 addition & 2 deletions internal/netx/conn_linux.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package netx

import (
"net"
"time"
)

func fromTCPConn(tcpConn *net.TCPConn) (*Conn, error) {
func fromTCPLikeConn(tcpConn TCPLikeConn) (*Conn, error) {
// On Linux system, this can only fail when the file duplication fails.
fp, err := tcpConn.File()
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions internal/netx/conn_stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
package netx

import (
"net"
"time"
)

func fromTCPConn(tcpConn *net.TCPConn) (*Conn, error) {
func fromTCPLikeConn(tcpConn TCPLikeConn) (*Conn, error) {
// On non-Linux systems, TCPInfo/BBRInfo aren't supported, the file pointer
// is not needed.
return &Conn{
Expand Down
2 changes: 1 addition & 1 deletion internal/netx/listener_stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ func (ln *Listener) accept() (net.Conn, error) {
return nil, err
}

return fromTCPConn(tc)
return fromTCPLikeConn(tc)
}
2 changes: 1 addition & 1 deletion pkg/throughput1/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func TestProtocol_Download(t *testing.T) {
if err != nil {
return nil, err
}
return netx.FromTCPConn(conn.(*net.TCPConn))
return netx.FromTCPLikeConn(conn.(*net.TCPConn))
},
}

Expand Down

0 comments on commit d233487

Please sign in to comment.