Skip to content

Commit

Permalink
fix getSysConn to work with TLS
Browse files Browse the repository at this point in the history
  • Loading branch information
DheerendraRathor committed Apr 30, 2024
1 parent c54d098 commit 8d7c128
Show file tree
Hide file tree
Showing 2 changed files with 30 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
17 changes: 17 additions & 0 deletions connection_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package tchannel
import (
"bytes"
"net"
"net/http/httptest"
"syscall"
"testing"

Expand Down Expand Up @@ -79,4 +80,20 @@ 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) {
loggerBuf := &bytes.Buffer{}
logger := NewLogger(loggerBuf)

server := httptest.NewTLSServer(nil)
defer server.Close()

conn, err := net.Dial("tcp", server.Listener.Addr().String())
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")
})
}

0 comments on commit 8d7c128

Please sign in to comment.