| 482 | } |
| 483 | |
| 484 | func (p *ConnPool) checkMinIdleConns() { |
| 485 | // If a check is already in progress, mark that we need another check and return |
| 486 | if !p.idleCheckInProgress.CompareAndSwap(false, true) { |
| 487 | p.idleCheckNeeded.Store(true) |
| 488 | return |
| 489 | } |
| 490 | |
| 491 | if p.cfg.MinIdleConns == 0 { |
| 492 | p.idleCheckInProgress.Store(false) |
| 493 | return |
| 494 | } |
| 495 | |
| 496 | // Keep checking until no more checks are needed |
| 497 | // This handles the case where multiple Remove() calls happen concurrently |
| 498 | for { |
| 499 | // Clear the "check needed" flag before we start |
| 500 | p.idleCheckNeeded.Store(false) |
| 501 | |
| 502 | // Only create idle connections if we haven't reached the total pool size limit |
| 503 | // MinIdleConns should be a subset of PoolSize, not additional connections |
| 504 | for p.poolSize.Load() < p.cfg.PoolSize && p.idleConnsLen.Load() < p.cfg.MinIdleConns { |
| 505 | // Try to acquire a semaphore token |
| 506 | if !p.semaphore.TryAcquire() { |
| 507 | // Semaphore is full, can't create more connections right now |
| 508 | // Break out of inner loop to check if we need to retry |
| 509 | break |
| 510 | } |
| 511 | |
| 512 | p.poolSize.Add(1) |
| 513 | p.idleConnsLen.Add(1) |
| 514 | go func() { |
| 515 | defer func() { |
| 516 | if err := recover(); err != nil { |
| 517 | p.poolSize.Add(-1) |
| 518 | p.idleConnsLen.Add(-1) |
| 519 | |
| 520 | p.freeTurn() |
| 521 | internal.Logger.Printf(context.Background(), "addIdleConn panic: %+v", err) |
| 522 | } |
| 523 | }() |
| 524 | |
| 525 | err := p.addIdleConn() |
| 526 | if err != nil && err != ErrClosed { |
| 527 | p.poolSize.Add(-1) |
| 528 | p.idleConnsLen.Add(-1) |
| 529 | } |
| 530 | p.freeTurn() |
| 531 | }() |
| 532 | } |
| 533 | |
| 534 | // If no one requested another check while we were working, we're done |
| 535 | if !p.idleCheckNeeded.Load() { |
| 536 | p.idleCheckInProgress.Store(false) |
| 537 | return |
| 538 | } |
| 539 | |
| 540 | // Otherwise, loop again to handle the new requests |
| 541 | } |