Skip to content

Commit

Permalink
style: resolves linter issues
Browse files Browse the repository at this point in the history
Signed-off-by: re-Tick <jain.ritik.1001@gmail.com>
  • Loading branch information
re-Tick committed Apr 12, 2024
1 parent 3fd8c38 commit 0c69bfd
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 deletions.
4 changes: 2 additions & 2 deletions pkg/core/proxy/integrations/mongo/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"go.uber.org/zap"
)

// decodeMongo decodes the mongo wire message from the client connection
// decodeMongo decodes the mongo wire message from the client connection
// and sends the response back to the client.
func decodeMongo(ctx context.Context, logger *zap.Logger, reqBuf []byte, clientConn net.Conn, dstCfg *integrations.ConditionalDstCfg, mockDb integrations.MockMemDb, opts models.OutgoingOptions) error {
startedDecoding := time.Now()
Expand Down Expand Up @@ -77,7 +77,7 @@ func decodeMongo(ctx context.Context, logger *zap.Logger, reqBuf []byte, clientC
Message: mongoRequest,
ReadDelay: int64(readRequestDelay),
})
// check for the more_to_come flag bit in the mongo request
// check for the more_to_come flag bit in the mongo request
// header to read the next chunks of the request
if val, ok := mongoRequest.(*models.MongoOpMessage); ok && hasSecondSetBit(val.FlagBits) {
for {
Expand Down
11 changes: 5 additions & 6 deletions pkg/core/proxy/integrations/mongo/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"go.uber.org/zap"
)

// encodeMongo records the outgoing mongo messages of the client connection,
// encodeMongo records the outgoing mongo messages of the client connection,
// decodes the wiremessage binary and writes readable string
// to the yaml file.
func (m *Mongo) encodeMongo(ctx context.Context, logger *zap.Logger, reqBuf []byte, clientConn, destConn net.Conn, mocks chan<- *models.Mock, _ models.OutgoingOptions) error {
Expand All @@ -35,7 +35,7 @@ func (m *Mongo) encodeMongo(ctx context.Context, logger *zap.Logger, reqBuf []by
for {
var err error
var readRequestDelay time.Duration

// reads the request packets from the client connection after the first request packet.
// Since, that is already read in the RecordOutgoing function.
if string(reqBuf) == "read form client conn" {
Expand All @@ -58,7 +58,7 @@ func (m *Mongo) encodeMongo(ctx context.Context, logger *zap.Logger, reqBuf []by
}

var (
mongoRequests []models.MongoRequest // stores the decoded binary packets for a request
mongoRequests []models.MongoRequest // stores the decoded binary packets for a request
mongoResponses []models.MongoResponse // stores the decoded binary packets for a response
)
// decode the binary packet and store the values in the corresponding struct
Expand Down Expand Up @@ -89,7 +89,7 @@ func (m *Mongo) encodeMongo(ctx context.Context, logger *zap.Logger, reqBuf []by
// check for the request packet streaming for the mongo wire message
if val, ok := mongoRequest.(*models.MongoOpMessage); ok && hasSecondSetBit(val.FlagBits) {
for {
// read the streaming request packets
// read the streaming request packets
requestBuffer1, err := pUtil.ReadBytes(ctx, logger, clientConn)
if err != nil {
if err == io.EOF {
Expand Down Expand Up @@ -117,7 +117,7 @@ func (m *Mongo) encodeMongo(ctx context.Context, logger *zap.Logger, reqBuf []by
logger.Debug("the response from the server is complete")
break
}
// decode the binary packet and return the values in the corresponding structs
// decode the binary packet and return the values in the corresponding structs
// for header and message.
_, reqHeader, mongoReq, err := Decode(requestBuffer1, logger)
if err != nil {
Expand Down Expand Up @@ -159,7 +159,6 @@ func (m *Mongo) encodeMongo(ctx context.Context, logger *zap.Logger, reqBuf []by
pckLength := getPacketLength(responsePckLengthBuffer)
logger.Debug("received pck length ", zap.Any("packet length", pckLength))


// read the entire response packet
responsePckDataBuffer, err := pUtil.ReadRequiredBytes(ctx, logger, destConn, int(pckLength)-4)

Expand Down
18 changes: 9 additions & 9 deletions pkg/core/proxy/integrations/mongo/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ func (m *Mongo) MatchType(_ context.Context, buffer []byte) bool {
if len(buffer) < 4 {
return false
}
// identifies by the starting 4 bytes of the message, since that
// identifies by the starting 4 bytes of the message, since that
// are the length of the message.
messageLength := binary.LittleEndian.Uint32(buffer[0:4])
return int(messageLength) == len(buffer)
}

// RecordOutgoing records the outgoing mongo messages of the client connection into the yaml file.
// RecordOutgoing records the outgoing mongo messages of the client connection into the yaml file.
// The database connection is keep-alive so, this function will be called during the connection establishment.
func (m *Mongo) RecordOutgoing(ctx context.Context, src net.Conn, dst net.Conn, mocks chan<- *models.Mock, opts models.OutgoingOptions) error {
logger := m.logger.With(zap.Any("Client IP Address", src.RemoteAddr().String()), zap.Any("Client ConnectionID", ctx.Value(models.ClientConnectionIDKey).(string)), zap.Any("Destination ConnectionID", ctx.Value(models.DestConnectionIDKey).(string)))
Expand All @@ -54,10 +54,10 @@ func (m *Mongo) RecordOutgoing(ctx context.Context, src net.Conn, dst net.Conn,
return err
}

// the mongo messages are converted to the yaml format.
// the mongo messages are converted to the yaml format.
//
// initially the reqBuf contains the first network packet
// from the client connection which is used to determine
// initially the reqBuf contains the first network packet
// from the client connection which is used to determine
// the packet type in MatchType.
err = m.encodeMongo(ctx, logger, reqBuf, src, dst, mocks, opts)
if err != nil {
Expand All @@ -71,8 +71,8 @@ func (m *Mongo) RecordOutgoing(ctx context.Context, src net.Conn, dst net.Conn,
// mocks the responses from the yaml file. The database connection is keep-alive
func (m *Mongo) MockOutgoing(ctx context.Context, src net.Conn, dstCfg *integrations.ConditionalDstCfg, mockDb integrations.MockMemDb, opts models.OutgoingOptions) error {
logger := m.logger.With(zap.Any("Client IP Address", src.RemoteAddr().String()), zap.Any("Client ConnectionID", util.GetNextID()), zap.Any("Destination ConnectionID", util.GetNextID()))
// read the initial buffer from the client connection. Initially the

// read the initial buffer from the client connection. Initially the
// reqBuf contains the first network packet from the client connection
// which is used to determine the packet type in MatchType.
reqBuf, err := util.ReadInitialBuf(ctx, logger, src)
Expand All @@ -92,13 +92,13 @@ func (m *Mongo) MockOutgoing(ctx context.Context, src net.Conn, dstCfg *integrat

// recordMessage records the mongo messages into the yaml file.
func (m *Mongo) recordMessage(_ context.Context, logger *zap.Logger, mongoRequests []models.MongoRequest, mongoResponses []models.MongoResponse, opReq Operation, reqTimestampMock time.Time, mocks chan<- *models.Mock) {
shouldRecordCalls := true // boolean to check for already saved config mocks
shouldRecordCalls := true // boolean to check for already saved config mocks
name := "mocks"
meta1 := map[string]string{
"operation": opReq.String(),
}

// check that the packet is heartbeat or not.
// check that the packet is heartbeat or not.
// See: https://github.com/mongodb/mongo-go-driver/blob/8489898c64a2d8c2e2160006eb851a11a9db9e9d/x/mongo/driver/operation/hello.go#L503
if isHeartBeat(logger, opReq, *mongoRequests[0].Header, mongoRequests[0].Message) {
meta1["type"] = "config"
Expand Down
4 changes: 2 additions & 2 deletions pkg/core/proxy/integrations/mongo/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type Operation interface {
}

// Decode decodes the wire message binary into the operation and the mongo message.
//
//
// see https://github.com/mongodb/mongo-go-driver/blob/v1.7.2/x/mongo/driver/operation.go#L1361-L1426
func Decode(wm []byte, logger *zap.Logger) (Operation, models.MongoHeader, interface{}, error) {
wmLength := len(wm)
Expand All @@ -66,7 +66,7 @@ func Decode(wm []byte, logger *zap.Logger) (Operation, models.MongoHeader, inter
err error
mongoMsg interface{}
)

switch opCode {
case wiremessage.OpQuery:
// decodeQuery is a helper function to decode the OpQuery operation
Expand Down

0 comments on commit 0c69bfd

Please sign in to comment.