mirror of
https://github.com/lordmathis/llamactl.git
synced 2025-11-06 09:04:27 +00:00
Add LastRequestTime method and LRU eviction logic for instance management
This commit is contained in:
@@ -144,6 +144,10 @@ func (i *Process) Stop() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (i *Process) LastRequestTime() int64 {
|
||||||
|
return i.lastRequestTime.Load()
|
||||||
|
}
|
||||||
|
|
||||||
func (i *Process) WaitForHealthy(timeout int) error {
|
func (i *Process) WaitForHealthy(timeout int) error {
|
||||||
if !i.IsRunning() {
|
if !i.IsRunning() {
|
||||||
return fmt.Errorf("instance %s is not running", i.Name)
|
return fmt.Errorf("instance %s is not running", i.Name)
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ type InstanceManager interface {
|
|||||||
StartInstance(name string) (*instance.Process, error)
|
StartInstance(name string) (*instance.Process, error)
|
||||||
IsMaxRunningInstancesReached() bool
|
IsMaxRunningInstancesReached() bool
|
||||||
StopInstance(name string) (*instance.Process, error)
|
StopInstance(name string) (*instance.Process, error)
|
||||||
|
EvictLRUInstance() error
|
||||||
RestartInstance(name string) (*instance.Process, error)
|
RestartInstance(name string) (*instance.Process, error)
|
||||||
GetInstanceLogs(name string) (string, error)
|
GetInstanceLogs(name string) (string, error)
|
||||||
Shutdown()
|
Shutdown()
|
||||||
|
|||||||
@@ -239,6 +239,36 @@ func (im *instanceManager) StopInstance(name string) (*instance.Process, error)
|
|||||||
return instance, nil
|
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.
|
// RestartInstance stops and then starts an instance, returning the updated instance.
|
||||||
func (im *instanceManager) RestartInstance(name string) (*instance.Process, error) {
|
func (im *instanceManager) RestartInstance(name string) (*instance.Process, error) {
|
||||||
instance, err := im.StopInstance(name)
|
instance, err := im.StopInstance(name)
|
||||||
|
|||||||
Reference in New Issue
Block a user