Add LastRequestTime method and LRU eviction logic for instance management

This commit is contained in:
2025-08-30 23:59:37 +02:00
parent 4581d67165
commit c1fa0faf4b
3 changed files with 35 additions and 0 deletions

View File

@@ -239,6 +239,36 @@ 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 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)