putConn is the internal implementation of Put that optionally frees a turn.
(ctx context.Context, cn *Conn, freeTurn bool)
| 1212 | |
| 1213 | // putConn is the internal implementation of Put that optionally frees a turn. |
| 1214 | func (p *ConnPool) putConn(ctx context.Context, cn *Conn, freeTurn bool) { |
| 1215 | // Guard against nil connection |
| 1216 | if cn == nil { |
| 1217 | internal.Logger.Printf(ctx, "putConn called with nil connection") |
| 1218 | if freeTurn { |
| 1219 | p.freeTurn() |
| 1220 | } |
| 1221 | return |
| 1222 | } |
| 1223 | |
| 1224 | // Process connection using the hooks system |
| 1225 | shouldPool := true |
| 1226 | shouldRemove := false |
| 1227 | var err error |
| 1228 | |
| 1229 | if reason := cn.CloseOnPutReason(); reason != "" { |
| 1230 | p.removeConnInternal(ctx, cn, errors.New(reason), freeTurn) |
| 1231 | return |
| 1232 | } |
| 1233 | |
| 1234 | if cn.HasBufferedData() { |
| 1235 | // Peek at the reply type to check if it's a push notification |
| 1236 | if replyType, err := cn.PeekReplyTypeSafe(); err != nil || replyType != proto.RespPush { |
| 1237 | // Not a push notification or error peeking, remove connection |
| 1238 | internal.Logger.Printf(ctx, "Conn has unread data (not push notification), removing it") |
| 1239 | p.removeConnInternal(ctx, cn, err, freeTurn) |
| 1240 | return |
| 1241 | } |
| 1242 | // It's a push notification, allow pooling (client will handle it) |
| 1243 | } |
| 1244 | |
| 1245 | // Lock-free atomic read - no mutex overhead! |
| 1246 | hookManager := p.hookManager.Load() |
| 1247 | |
| 1248 | if hookManager != nil { |
| 1249 | shouldPool, shouldRemove, err = hookManager.ProcessOnPut(ctx, cn) |
| 1250 | if err != nil { |
| 1251 | internal.Logger.Printf(ctx, "Connection hook error: %v", err) |
| 1252 | p.removeConnInternal(ctx, cn, err, freeTurn) |
| 1253 | return |
| 1254 | } |
| 1255 | } |
| 1256 | |
| 1257 | // Combine all removal checks into one - reduces branches |
| 1258 | if shouldRemove || !shouldPool { |
| 1259 | p.removeConnInternal(ctx, cn, errHookRequestedRemoval, freeTurn) |
| 1260 | return |
| 1261 | } |
| 1262 | |
| 1263 | if !cn.pooled { |
| 1264 | p.removeConnInternal(ctx, cn, errConnNotPooled, freeTurn) |
| 1265 | return |
| 1266 | } |
| 1267 | |
| 1268 | var shouldCloseConn bool |
| 1269 | var removedFromPool bool |
| 1270 | |
| 1271 | if p.cfg.MaxIdleConns == 0 || p.idleConnsLen.Load() < p.cfg.MaxIdleConns { |
no test coverage detected