AvailableGalleryModelsCached returns gallery models from an in-memory cache. Local-only fields (installed status) are refreshed on every call. A background goroutine is triggered to re-fetch the full model list (including network calls) so subsequent requests pick up changes without blocking the cal
(galleries []config.Gallery, systemState *system.SystemState)
| 315 | // calls) so subsequent requests pick up changes without blocking the caller. |
| 316 | // The first call with an empty cache blocks until the initial load completes. |
| 317 | func AvailableGalleryModelsCached(galleries []config.Gallery, systemState *system.SystemState) (GalleryElements[*GalleryModel], error) { |
| 318 | availableModelsMu.RLock() |
| 319 | cached := availableModelsCache |
| 320 | availableModelsMu.RUnlock() |
| 321 | |
| 322 | if cached != nil { |
| 323 | // Refresh installed status under write lock to avoid races with |
| 324 | // concurrent readers and the background refresh goroutine. |
| 325 | availableModelsMu.Lock() |
| 326 | for _, m := range cached { |
| 327 | _, err := os.Stat(filepath.Join(systemState.Model.ModelsPath, fmt.Sprintf("%s.yaml", m.GetName()))) |
| 328 | m.SetInstalled(err == nil) |
| 329 | } |
| 330 | availableModelsMu.Unlock() |
| 331 | // Trigger a background refresh if one is not already running. |
| 332 | triggerGalleryRefresh(galleries, systemState) |
| 333 | return cached, nil |
| 334 | } |
| 335 | |
| 336 | // No cache yet — must do a blocking load. |
| 337 | models, err := AvailableGalleryModels(galleries, systemState) |
| 338 | if err != nil { |
| 339 | return nil, err |
| 340 | } |
| 341 | |
| 342 | availableModelsMu.Lock() |
| 343 | availableModelsCache = models |
| 344 | galleryGeneration.Add(1) |
| 345 | availableModelsMu.Unlock() |
| 346 | |
| 347 | return models, nil |
| 348 | } |
| 349 | |
| 350 | // triggerGalleryRefresh starts a background goroutine that refreshes the |
| 351 | // gallery model cache. Only one refresh runs at a time; concurrent calls |
no test coverage detected