Initialize creates and connects clients for all profiles in the group. Returns an error only if ALL connections fail; partial failures are logged.
(ctx context.Context, profiles []ProfileEntry)
| 96 | // Initialize creates and connects clients for all profiles in the group. |
| 97 | // Returns an error only if ALL connections fail; partial failures are logged. |
| 98 | func (m *GroupClientManager) Initialize(ctx context.Context, profiles []ProfileEntry) error { |
| 99 | m.mu.Lock() |
| 100 | defer m.mu.Unlock() |
| 101 | |
| 102 | // Clear existing clients |
| 103 | m.clients = make(map[string]*ProfileClient) |
| 104 | |
| 105 | if len(profiles) == 0 { |
| 106 | return fmt.Errorf("no profiles provided for group '%s'", m.groupName) |
| 107 | } |
| 108 | |
| 109 | var wg sync.WaitGroup |
| 110 | errChan := make(chan error, len(profiles)) |
| 111 | clientChan := make(chan *ProfileClient, len(profiles)) |
| 112 | |
| 113 | // Connect to all profiles concurrently |
| 114 | for _, entry := range profiles { |
| 115 | wg.Add(1) |
| 116 | go func(pe ProfileEntry) { |
| 117 | defer wg.Done() |
| 118 | |
| 119 | pc := &ProfileClient{ |
| 120 | ProfileName: pe.Name, |
| 121 | Status: ProfileStatusUnknown, |
| 122 | } |
| 123 | |
| 124 | // Create cache key prefix for this profile |
| 125 | // Using simple key prefixing instead of namespace |
| 126 | cacheKeyPrefix := fmt.Sprintf("group:%s:profile:%s:", |
| 127 | m.groupName, pe.Name) |
| 128 | |
| 129 | // Create client with prefixed cache keys |
| 130 | // For now, we'll just use the shared cache with prefixed keys |
| 131 | client, err := NewClient(pe.Config, |
| 132 | WithLogger(m.logger), |
| 133 | WithCache(m.cache), |
| 134 | ) |
| 135 | |
| 136 | _ = cacheKeyPrefix // Will be used when we implement cache key prefixing |
| 137 | |
| 138 | if err != nil { |
| 139 | pc.SetStatus(ProfileStatusError, err) |
| 140 | m.logger.Error("Failed to connect to profile %s: %v", pe.Name, err) |
| 141 | errChan <- fmt.Errorf("profile %s: %w", pe.Name, err) |
| 142 | } else { |
| 143 | pc.Client = client |
| 144 | pc.SetStatus(ProfileStatusConnected, nil) |
| 145 | m.logger.Debug("Successfully connected to profile %s", pe.Name) |
| 146 | } |
| 147 | |
| 148 | clientChan <- pc |
| 149 | }(entry) |
| 150 | } |
| 151 | |
| 152 | // Wait for all connections to complete |
| 153 | wg.Wait() |
| 154 | close(errChan) |
| 155 | close(clientChan) |