Skip to content

Commit

Permalink
GODRIVER-2935 Add LegacyHandshake operation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
prestonvasquez committed Sep 6, 2023
1 parent 728f277 commit 9fec743
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
1 change: 1 addition & 0 deletions x/mongo/driver/legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ const (
LegacyKillCursors
LegacyListCollections
LegacyListIndexes
LegacyHandshake
)
13 changes: 3 additions & 10 deletions x/mongo/driver/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -1266,17 +1266,12 @@ func (op Operation) createMsgWireMessage(ctx context.Context, maxTimeMS uint64,
return bsoncore.UpdateLength(dst, wmindex, int32(len(dst[wmindex:]))), info, nil
}

// isLegacyHandshake returns "true" if the operation is the first message of
// the initial handshake and should use a legacy hello. The requirement for
// using a legacy hello as defined by the specifications is as follows:
//
// > If server API version is not requested and loadBalanced: False, drivers
// > MUST use legacy hello for the first message of the initial handshake with
// > the OP_QUERY protocol
// isLegacyHandshake returns True if the operation is the first message of
// the initial handshake and should use a legacy hello.
func isLegacyHandshake(op Operation, desc description.SelectedServer) bool {
isInitialHandshake := desc.WireVersion == nil || desc.WireVersion.Max == 0

return desc.Kind != description.LoadBalanced && op.ServerAPI == nil && isInitialHandshake
return op.Legacy == LegacyHandshake && isInitialHandshake
}

func (op Operation) createWireMessage(
Expand Down Expand Up @@ -1937,8 +1932,6 @@ func (op Operation) publishFinishedEvent(ctx context.Context, info finishedInfor
logger.KeyFailure, formattedReply)...)
}

//fmt.Println("->", redactFinishedInformationResponse(info))

// If the finished event cannot be published, return early.
if !op.canPublishFinishedEvent(info) {
return
Expand Down
16 changes: 15 additions & 1 deletion x/mongo/driver/operation/hello.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,8 +537,16 @@ func (h *Hello) StreamResponse(ctx context.Context, conn driver.StreamerConnecti
return h.createOperation().ExecuteExhaust(ctx, conn)
}

// isLegacyHandshake returns True if server API version is not requested and
// loadBalanced is False. If this is the case, then the drivers MUST use legacy
// hello for the first message of the initial handshake with the OP_QUERY
// protocol
func isLegacyHandshake(h *Hello) bool {
return h.serverAPI == nil && h.d.Kind() != description.LoadBalanced
}

func (h *Hello) createOperation() driver.Operation {
return driver.Operation{
op := driver.Operation{
Clock: h.clock,
CommandFn: h.command,
Database: "admin",
Expand All @@ -549,6 +557,12 @@ func (h *Hello) createOperation() driver.Operation {
},
ServerAPI: h.serverAPI,
}

if isLegacyHandshake(h) {
op.Legacy = driver.LegacyHandshake
}

return op
}

// GetHandshakeInformation performs the MongoDB handshake for the provided connection and returns the relevant
Expand Down

0 comments on commit 9fec743

Please sign in to comment.