diff --git a/pkg/manager/operations.go b/pkg/manager/operations.go index 5d6396a..b347900 100644 --- a/pkg/manager/operations.go +++ b/pkg/manager/operations.go @@ -240,40 +240,6 @@ func (im *instanceManager) StopInstance(name string) (*instance.Process, error) return instance, nil } -// EvictLRUInstance finds and stops the least recently used running instance. -func (im *instanceManager) EvictLRUInstance() error { - im.mu.RLock() - var lruInstance *instance.Process - - for name, _ := range im.runningInstances { - inst := im.instances[name] - if inst == nil { - continue - } - - if inst.GetOptions() != nil && inst.GetOptions().IdleTimeout != nil && *inst.GetOptions().IdleTimeout <= 0 { - continue // Skip instances without idle timeout - } - - if lruInstance == nil { - lruInstance = inst - } - - if inst.LastRequestTime() < lruInstance.LastRequestTime() { - lruInstance = inst - } - } - im.mu.RUnlock() - - if lruInstance == nil { - return fmt.Errorf("failed to find lru instance") - } - - // Evict Instance - _, err := im.StopInstance(lruInstance.Name) - return err -} - // RestartInstance stops and then starts an instance, returning the updated instance. func (im *instanceManager) RestartInstance(name string) (*instance.Process, error) { instance, err := im.StopInstance(name) diff --git a/pkg/manager/timeout.go b/pkg/manager/timeout.go index a28bc67..c982f10 100644 --- a/pkg/manager/timeout.go +++ b/pkg/manager/timeout.go @@ -1,6 +1,10 @@ package manager -import "log" +import ( + "fmt" + "llamactl/pkg/instance" + "log" +) func (im *instanceManager) checkAllTimeouts() { im.mu.RLock() @@ -24,3 +28,37 @@ func (im *instanceManager) checkAllTimeouts() { } } } + +// EvictLRUInstance finds and stops the least recently used running instance. +func (im *instanceManager) EvictLRUInstance() error { + im.mu.RLock() + var lruInstance *instance.Process + + for name, _ := range im.runningInstances { + inst := im.instances[name] + if inst == nil { + continue + } + + if inst.GetOptions() != nil && inst.GetOptions().IdleTimeout != nil && *inst.GetOptions().IdleTimeout <= 0 { + continue // Skip instances without idle timeout + } + + if lruInstance == nil { + lruInstance = inst + } + + if inst.LastRequestTime() < lruInstance.LastRequestTime() { + lruInstance = inst + } + } + im.mu.RUnlock() + + if lruInstance == nil { + return fmt.Errorf("failed to find lru instance") + } + + // Evict Instance + _, err := im.StopInstance(lruInstance.Name) + return err +}