@@ -1404,37 +1404,34 @@ func (q *wantConnQueue) cleanFront() {
14041404 }
14051405}
14061406
1407+ // spawnConnection establishes a new connection and delivers it to a waiting
1408+ // request. It handles dialing, handshaking, and error handling. This function
1409+ // is intended to be run in its own goroutine.
14071410func (p * pool ) spawnConnection (w * wantConn , conn * connection ) {
1408- defer func () { <- p .connectionSem }() // Release slot when done, see maxConnecting.
1411+ // Release a slot from the connection semaphore when this function returns.
1412+ // This ensures that another connection can be spawned.
1413+ defer func () { <- p .connectionSem }()
14091414
1410- // Perform dial/handshake with optional timeout .
1415+ // Record the start time to calculate the total connection setup duration .
14111416 start := time .Now ()
14121417
1413- // Pass the createConnections context to connect to allow pool close to
1414- // cancel connection establishment so shutdown doesn't block indefinitely if
1415- // connectTimeout=0.
1416- //
1417- // Per the specifications, an explicit value of connectTimeout=0 means the
1418- // timeout is "infinite".
1418+ // Create a context for the dial operation. If a connection timeout is
1419+ // configured, the context will be set to time out after that duration.
14191420 dialCtx := context .Background ()
14201421 var cancel context.CancelFunc
14211422 if p .connectTimeout > 0 {
14221423 dialCtx , cancel = context .WithTimeout (dialCtx , p .connectTimeout )
14231424 defer cancel ()
14241425 }
14251426
1426- err := conn .connect (dialCtx )
1427-
1428- if err != nil {
1429- // Deliver error and run SDAM handshake error logic
1427+ // Attempt to connect
1428+ if err := conn .connect (dialCtx ); err != nil {
1429+ // If connection fails, deliver the error to the waiting requester.
14301430 w .tryDeliver (nil , err )
14311431
1432- // If there's an error connecting the new connection, call the handshake error handler
1433- // that implements the SDAM handshake error handling logic. This must be called after
1434- // delivering the connection error to the waiting wantConn. If it's called before, the
1435- // handshake error handler may clear the connection pool, leading to a different error
1436- // message being delivered to the same waiting wantConn in idleConnWait when the wait
1437- // queues are cleared.
1432+ // If a handshake error handler is defined, invoke it to handle SDAM state
1433+ // changes. This is done after delivering the error to prevent race
1434+ // conditions where the pool might be cleared before the error is delivered.
14381435 if p .handshakeErrFn != nil {
14391436 p .handshakeErrFn (err , conn .generation , conn .desc .ServiceID )
14401437 }
@@ -1475,7 +1472,9 @@ func (p *pool) spawnConnection(w *wantConn, conn *connection) {
14751472 }
14761473}
14771474
1478- // hasSpace checks if the pool has space for a new connection.
1475+ // hasSpace checks if the pool has space for a new connection. It returns
1476+ // "true" if the maximum size is unlimited (0) or if the current number of
1477+ // connections is less than the maximum size.
14791478func (p * pool ) hasSpace () bool {
14801479 return p .maxSize == 0 || uint64 (len (p .conns )) < p .maxSize
14811480}
0 commit comments