Skip to content

Commit

Permalink
fix getSysConn to work with TLS (#918)
Browse files Browse the repository at this point in the history
`tls.Conn` by default doesn't implement `syscall.Conn` and hence tchannel emits `onnection does not implement SyscallConn.` log a lot. 

Since go 1.18, `tls.Conn` exposes method `NetConn()` to expose raw underlying TCP connection. This is used for getting the `syscall.Conn`.
  • Loading branch information
DheerendraRathor authored May 2, 2024
1 parent 49a0061 commit 9e9c14d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
14 changes: 13 additions & 1 deletion connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package tchannel

import (
"crypto/tls"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -971,7 +972,18 @@ func (c *Connection) getLastActivityWriteTime() time.Time {
}

func getSysConn(conn net.Conn, log Logger) syscall.RawConn {
connSyscall, ok := conn.(syscall.Conn)
var (
connSyscall syscall.Conn
ok bool
)
switch v := conn.(type) {
case syscall.Conn:
connSyscall = v
ok = true
case *tls.Conn:
connSyscall, ok = v.NetConn().(syscall.Conn)
}

if !ok {
log.WithFields(LogField{"connectionType", fmt.Sprintf("%T", conn)}).
Error("Connection does not implement SyscallConn.")
Expand Down
43 changes: 43 additions & 0 deletions connection_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ package tchannel

import (
"bytes"
"crypto/tls"
"net"
"net/http/httptest"
"syscall"
"testing"

Expand Down Expand Up @@ -79,4 +81,45 @@ func TestGetSysConn(t *testing.T) {
require.NotNil(t, sysConn)
assert.Empty(t, loggerBuf.String(), "expected no logs on success")
})

t.Run("SyscallConn is successful with TLS", func(t *testing.T) {
var (
loggerBuf = &bytes.Buffer{}
logger = NewLogger(loggerBuf)
server = httptest.NewTLSServer(nil)
)
defer server.Close()

conn, err := tls.Dial("tcp", server.Listener.Addr().String(), &tls.Config{InsecureSkipVerify: true})
require.NoError(t, err, "failed to dial")
defer conn.Close()

sysConn := getSysConn(conn, logger)
require.NotNil(t, sysConn)
assert.Empty(t, loggerBuf.String(), "expected no logs on success")
})

t.Run("no SyscallConn - nil net.Conn", func(t *testing.T) {
var (
loggerBuf = &bytes.Buffer{}
logger = NewLogger(loggerBuf)
syscallConn = getSysConn(nil /* conn */, logger)
)

require.Nil(t, syscallConn, "expected no syscall.RawConn to be returned")
assert.Contains(t, loggerBuf.String(), "Connection does not implement SyscallConn", "missing log")
assert.Contains(t, loggerBuf.String(), "{connectionType <nil>}", "missing type in log")
})

t.Run("no SyscallConn - TLS with no net.Conn", func(t *testing.T) {
var (
loggerBuf = &bytes.Buffer{}
logger = NewLogger(loggerBuf)
syscallConn = getSysConn(&tls.Conn{}, logger)
)

require.Nil(t, syscallConn, "expected no syscall.RawConn to be returned")
assert.Contains(t, loggerBuf.String(), "Connection does not implement SyscallConn", "missing log")
assert.Contains(t, loggerBuf.String(), "{connectionType *tls.Conn}", "missing type in log")
})
}

0 comments on commit 9e9c14d

Please sign in to comment.