chore: drop double call to stop all backends, refactors

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
This commit is contained in:
Ettore Di Giacinto 2025-03-31 22:11:34 +02:00
parent 65470b0ab1
commit 04aafc4173

View file

@ -473,8 +473,6 @@ func (ml *ModelLoader) backendLoader(opts ...Option) (client grpc.Backend, err e
backend = realBackend backend = realBackend
} }
ml.stopActiveBackends(o.modelID, o.singleActiveBackend)
var backendToConsume string var backendToConsume string
switch backend { switch backend {
@ -497,13 +495,17 @@ func (ml *ModelLoader) backendLoader(opts ...Option) (client grpc.Backend, err e
} }
func (ml *ModelLoader) stopActiveBackends(modelID string, singleActiveBackend bool) { func (ml *ModelLoader) stopActiveBackends(modelID string, singleActiveBackend bool) {
if !singleActiveBackend {
return
}
// If we can have only one backend active, kill all the others (except external backends) // If we can have only one backend active, kill all the others (except external backends)
if singleActiveBackend {
log.Debug().Msgf("Stopping all backends except '%s'", modelID) // Stop all backends except the one we are going to load
err := ml.StopGRPC(allExcept(modelID)) log.Debug().Msgf("Stopping all backends except '%s'", modelID)
if err != nil { err := ml.StopGRPC(allExcept(modelID))
log.Error().Err(err).Str("keptModel", modelID).Msg("error while shutting down all backends except for the keptModel - greedyloader continuing") if err != nil {
} log.Error().Err(err).Str("keptModel", modelID).Msg("error while shutting down all backends except for the keptModel - greedyloader continuing")
} }
} }
@ -520,10 +522,12 @@ func (ml *ModelLoader) Load(opts ...Option) (grpc.Backend, error) {
ml.stopActiveBackends(o.modelID, o.singleActiveBackend) ml.stopActiveBackends(o.modelID, o.singleActiveBackend)
// if a backend is defined, return the loader directly
if o.backendString != "" { if o.backendString != "" {
return ml.backendLoader(opts...) return ml.backendLoader(opts...)
} }
// Otherwise scan for backends in the asset directory
var err error var err error
// get backends embedded in the binary // get backends embedded in the binary