@@ -35,6 +35,11 @@ import {
3535/** @public */
3636export type Stream = Socket | TLSSocket ;
3737
38+ function applyBackpressureLabels ( error : MongoError ) {
39+ error . addErrorLabel ( MongoErrorLabel . SystemOverloadedError ) ;
40+ error . addErrorLabel ( MongoErrorLabel . RetryableError ) ;
41+ }
42+
3843export async function connect ( options : ConnectionOptions ) : Promise < Connection > {
3944 let connection : Connection | null = null ;
4045 try {
@@ -103,6 +108,8 @@ export async function performInitialHandshake(
103108 const authContext = new AuthContext ( conn , credentials , options ) ;
104109 conn . authContext = authContext ;
105110
111+ // If we encounter an error preparing the handshake document, do NOT apply backpressure labels. Errors
112+ // encountered building the handshake document are all client-side, and do not indicate an overloaded server.
106113 const handshakeDoc = await prepareHandshakeDocument ( authContext ) ;
107114
108115 // @ts -expect-error: TODO(NODE-5141): The options need to be filtered properly, Connection options differ from Command options
@@ -162,13 +169,19 @@ export async function performInitialHandshake(
162169
163170 try {
164171 await provider . auth ( authContext ) ;
165- } catch ( error ) {
166- if ( error instanceof MongoError ) {
167- error . addErrorLabel ( MongoErrorLabel . HandshakeError ) ;
168- if ( needsRetryableWriteLabel ( error , response . maxWireVersion , conn . description . type ) ) {
169- error . addErrorLabel ( MongoErrorLabel . RetryableWriteError ) ;
170- }
172+ } catch ( cause ) {
173+ // If we encounter an error authenticating a connection, do NOT apply backpressure labels.
174+
175+ const error =
176+ cause instanceof MongoError
177+ ? cause
178+ : new MongoRuntimeError ( 'unexpected error during authentication' , cause ) ;
179+
180+ error . addErrorLabel ( MongoErrorLabel . HandshakeError ) ;
181+ if ( needsRetryableWriteLabel ( error , response . maxWireVersion , conn . description . type ) ) {
182+ error . addErrorLabel ( MongoErrorLabel . RetryableWriteError ) ;
171183 }
184+
172185 throw error ;
173186 }
174187 }
@@ -189,6 +202,9 @@ export async function performInitialHandshake(
189202 if ( error instanceof MongoError ) {
190203 error . addErrorLabel ( MongoErrorLabel . HandshakeError ) ;
191204 }
205+ // If we encounter an error executing the initial handshake, apply backpressure labels.
206+ applyBackpressureLabels ( error ) ;
207+
192208 throw error ;
193209 }
194210 }
@@ -424,6 +440,8 @@ export async function makeSocket(options: MakeConnectionOptions): Promise<Stream
424440 socket = await connectedSocket ;
425441 return socket ;
426442 } catch ( error ) {
443+ // If we encounter a SystemOverloaded error while establishing a socket, apply the backpressure labels to it.
444+ applyBackpressureLabels ( error ) ;
427445 socket . destroy ( ) ;
428446 throw error ;
429447 } finally {
0 commit comments