launchPrimarySession launches a new primary session
(ctx context.Context)
| 451 | |
| 452 | // launchPrimarySession launches a new primary session |
| 453 | func (cm *ConnManager) launchPrimarySession(ctx context.Context) { |
| 454 | defer func() { |
| 455 | if r := recover(); r != nil { |
| 456 | shared.LogErrorf("ConnManager: Panic in launchPrimarySession: %v", r) |
| 457 | cm.clearLaunchState(true, false) |
| 458 | } |
| 459 | }() |
| 460 | defer cm.clearLaunchState(true, false) // Default to failure, update on success |
| 461 | |
| 462 | // Check if we already have a primary (race condition guard) |
| 463 | cm.mu.Lock() |
| 464 | for _, session := range cm.sessions { |
| 465 | if session.IsPrimary() { |
| 466 | cm.mu.Unlock() |
| 467 | shared.LogInfo("ConnManager: Primary session already exists, skipping launch") |
| 468 | cm.clearLaunchState(true, true) // Not a failure, just redundant |
| 469 | return |
| 470 | } |
| 471 | } |
| 472 | cm.mu.Unlock() |
| 473 | |
| 474 | session, err := cm.launchSession(ctx) |
| 475 | if err != nil { |
| 476 | shared.LogErrorf("ConnManager: Failed to launch primary session: %v", err) |
| 477 | metrics.RecordSessionFailure() |
| 478 | return |
| 479 | } |
| 480 | |
| 481 | metrics.RecordSessionLaunch() |
| 482 | |
| 483 | session.Role = RolePrimary |
| 484 | |
| 485 | cm.mu.Lock() |
| 486 | // Double-check after acquiring lock (race condition guard) |
| 487 | for _, existingSession := range cm.sessions { |
| 488 | if existingSession.IsPrimary() { |
| 489 | cm.mu.Unlock() |
| 490 | shared.LogInfof("ConnManager: Primary session already exists, discarding new session %s", session.ID) |
| 491 | cm.cleanupSession(session) // Use proper cleanup |
| 492 | cm.clearLaunchState(true, true) // Not a failure, just redundant |
| 493 | return |
| 494 | } |
| 495 | } |
| 496 | cm.sessions = append(cm.sessions, session) |
| 497 | cm.mu.Unlock() |
| 498 | |
| 499 | cm.clearLaunchState(true, true) // Success |
| 500 | shared.LogSuccessf("ConnManager: Successfully launched primary session %s", session.ID) |
| 501 | } |
| 502 | |
| 503 | // launchSecondarySession launches a new secondary session for rotation |
| 504 | func (cm *ConnManager) launchSecondarySession(ctx context.Context) { |
no test coverage detected