removeConnInternal is the internal implementation of Remove that optionally frees a turn.
(ctx context.Context, cn *Conn, reason error, freeTurn bool)
| 1408 | |
| 1409 | // removeConnInternal is the internal implementation of Remove that optionally frees a turn. |
| 1410 | func (p *ConnPool) removeConnInternal(ctx context.Context, cn *Conn, reason error, freeTurn bool) { |
| 1411 | // Lock-free atomic read - no mutex overhead! |
| 1412 | hookManager := p.hookManager.Load() |
| 1413 | |
| 1414 | if hookManager != nil { |
| 1415 | hookManager.ProcessOnRemove(ctx, cn, reason) |
| 1416 | } |
| 1417 | |
| 1418 | removed := p.removeConnWithLock(cn) |
| 1419 | |
| 1420 | if freeTurn { |
| 1421 | p.freeTurn() |
| 1422 | } |
| 1423 | |
| 1424 | // Only emit metric decrements if we actually removed the connection from the map. |
| 1425 | // If removed is false, Close() already removed it and emitted the -1 delta. |
| 1426 | if removed { |
| 1427 | // Notify metrics: connection removed (assume from used state) |
| 1428 | if cb := getMetricConnectionStateChangeCallback(); cb != nil { |
| 1429 | cb(ctx, cn, MetricStateUsed, "") |
| 1430 | } |
| 1431 | // Record connection count decrement (connection removed, assume from used state) |
| 1432 | if cb := getMetricConnectionCountCallback(); cb != nil { |
| 1433 | cb(ctx, -1, cn, "used", false) |
| 1434 | } |
| 1435 | } |
| 1436 | |
| 1437 | // Only emit connection closed if we actually owned the removal. |
| 1438 | // If removed is false, Close() already emitted connectionClosed for this conn. |
| 1439 | if removed { |
| 1440 | if cb := getMetricConnectionClosedCallback(); cb != nil { |
| 1441 | reasonStr := "unknown" |
| 1442 | if reason != nil { |
| 1443 | reasonStr = reason.Error() |
| 1444 | } |
| 1445 | cb(ctx, cn, reasonStr, reason) |
| 1446 | } |
| 1447 | } |
| 1448 | |
| 1449 | _ = p.closeConn(cn) |
| 1450 | |
| 1451 | // Check if we need to create new idle connections to maintain MinIdleConns |
| 1452 | p.checkMinIdleConns() |
| 1453 | } |
| 1454 | |
| 1455 | // CloseConn closes a connection and records metrics. |
| 1456 | // Parameters: |
no test coverage detected