Skip to content

Commit

Permalink
Revert "GODRIVER-3054 Handshake connection should not use legacy for LB"
Browse files Browse the repository at this point in the history
This reverts commit d77f8cc.
  • Loading branch information
prestonvasquez committed Nov 29, 2023
1 parent bb39541 commit e458f9d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
13 changes: 9 additions & 4 deletions x/mongo/driver/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -1357,6 +1357,14 @@ func (op Operation) createMsgWireMessage(
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.
func isLegacyHandshake(op Operation, desc description.SelectedServer) bool {
isInitialHandshake := desc.WireVersion == nil || desc.WireVersion.Max == 0

return op.Legacy == LegacyHandshake && isInitialHandshake
}

func (op Operation) createWireMessage(
ctx context.Context,
maxTimeMS uint64,
Expand All @@ -1365,10 +1373,7 @@ func (op Operation) createWireMessage(
conn Connection,
requestID int32,
) ([]byte, startedInformation, error) {
isInitialHandshake := desc.WireVersion == nil || desc.WireVersion.Max == 0

// Use the OP_LEGACY on non-load-balanced connection handshakes.
if op.ServerAPI == nil && desc.Kind != description.LoadBalanced && isInitialHandshake {
if isLegacyHandshake(op, desc) {
return op.createLegacyHandshakeWireMessage(maxTimeMS, dst, desc)
}

Expand Down
16 changes: 16 additions & 0 deletions x/mongo/driver/operation/hello.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,14 @@ 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(srvAPI *driver.ServerAPIOptions, deployment driver.Deployment) bool {
return srvAPI == nil && deployment.Kind() != description.LoadBalanced
}

func (h *Hello) createOperation() driver.Operation {
op := driver.Operation{
Clock: h.clock,
Expand All @@ -584,6 +592,10 @@ func (h *Hello) createOperation() driver.Operation {
ServerAPI: h.serverAPI,
}

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

return op
}

Expand All @@ -604,6 +616,10 @@ func (h *Hello) GetHandshakeInformation(ctx context.Context, _ address.Address,
ServerAPI: h.serverAPI,
}

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

if err := op.Execute(ctx); err != nil {
return driver.HandshakeInformation{}, err
}
Expand Down

0 comments on commit e458f9d

Please sign in to comment.