Skip to content

Commit e4c2e4a

Browse files
committed
Add explicit port handling in RESP connection callbacks
Introduced a new function, explicitPortCallbacks, to customize logging behavior based on the connection port. The NewFromAddr function now determines the appropriate callbacks to use depending on whether the default Redis port (6379) is used or a different one. This enhances the logging for commands and values sent over the connection.
1 parent 8985e5c commit e4c2e4a

File tree

3 files changed

+132
-84
lines changed

3 files changed

+132
-84
lines changed

internal/instrumented_resp_connection/instrumented_resp_connection.go

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ package instrumented_resp_connection
22

33
import (
44
"net"
5+
"strconv"
56

67
resp_connection "github.com/codecrafters-io/redis-tester/internal/resp/connection"
78
resp_value "github.com/codecrafters-io/redis-tester/internal/resp/value"
89
"github.com/codecrafters-io/tester-utils/logger"
910
)
1011

12+
const REDIS_DEFAULT_PORT = "6379"
13+
1114
type InstrumentedRespConnection struct {
1215
*resp_connection.RespConnection
1316

@@ -43,10 +46,48 @@ func defaultCallbacks(logger *logger.Logger) resp_connection.RespConnectionCallb
4346
}
4447
}
4548

49+
func explicitPortCallbacks(logger *logger.Logger, port string) resp_connection.RespConnectionCallbacks {
50+
return resp_connection.RespConnectionCallbacks{
51+
BeforeSendCommand: func(reusedConnection bool, command string, args ...string) {
52+
var commandPrefix string
53+
if reusedConnection {
54+
commandPrefix = ">"
55+
} else {
56+
commandPrefix = "$ redis-cli -p " + port
57+
}
58+
59+
commandWithArgs := append([]string{command}, args...)
60+
logger.Infof("%s %s", commandPrefix, quoteCLICommand(commandWithArgs))
61+
},
62+
BeforeSendValue: func(value resp_value.Value) {
63+
logger.Infof("Sent %s", value.FormattedString())
64+
},
65+
BeforeSendBytes: func(bytes []byte) {
66+
logger.Debugf("Sent bytes: %q", string(bytes))
67+
},
68+
AfterBytesReceived: func(bytes []byte) {
69+
logger.Debugf("Received bytes: %q", string(bytes))
70+
},
71+
AfterReadValue: func(value resp_value.Value) {
72+
logger.Debugf("Received RESP %s: %s", value.Type, value.FormattedString())
73+
},
74+
}
75+
}
76+
4677
func NewFromAddr(baseLogger *logger.Logger, addr string, connIdentifier string) (*InstrumentedRespConnection, error) {
4778
logger := baseLogger.Clone()
4879
logger.PushSecondaryPrefix(connIdentifier)
49-
c, err := resp_connection.NewRespConnectionFromAddr(addr, defaultCallbacks(logger))
80+
81+
var callbacks resp_connection.RespConnectionCallbacks
82+
tcpAddr, _ := net.ResolveTCPAddr("tcp", addr)
83+
port := strconv.Itoa(tcpAddr.Port)
84+
if port == REDIS_DEFAULT_PORT {
85+
callbacks = defaultCallbacks(logger)
86+
} else {
87+
callbacks = explicitPortCallbacks(logger, port)
88+
}
89+
90+
c, err := resp_connection.NewRespConnectionFromAddr(addr, callbacks)
5091
if err != nil {
5192
return nil, err
5293
}
@@ -83,7 +124,14 @@ func (c *InstrumentedRespConnection) UpdateBaseLogger(l *logger.Logger) {
83124
newLogger := l.Clone()
84125
newLogger.PushSecondaryPrefix(c.GetIdentifier())
85126
c.logger = newLogger
86-
c.UpdateCallBacks(defaultCallbacks(c.logger))
127+
128+
tcpAddr, _ := c.Conn.RemoteAddr().(*net.TCPAddr)
129+
port := strconv.Itoa(tcpAddr.Port)
130+
if port == REDIS_DEFAULT_PORT {
131+
c.UpdateCallBacks(defaultCallbacks(c.logger))
132+
} else {
133+
c.UpdateCallBacks(explicitPortCallbacks(c.logger, port))
134+
}
87135
}
88136

89137
func (c *InstrumentedRespConnection) SetReadValueInterceptor(transformer func(value resp_value.Value) resp_value.Value) {

0 commit comments

Comments
 (0)