Refactor NodeConfig handling to use a map

This commit is contained in:
2025-10-08 19:24:24 +02:00
parent 3418735204
commit 7f6725da96
8 changed files with 25 additions and 52 deletions

View File

@@ -63,15 +63,16 @@ type instanceManager struct {
}
// NewInstanceManager creates a new instance of InstanceManager.
func NewInstanceManager(backendsConfig config.BackendConfig, instancesConfig config.InstancesConfig, nodesConfig []config.NodeConfig) InstanceManager {
func NewInstanceManager(backendsConfig config.BackendConfig, instancesConfig config.InstancesConfig, nodesConfig map[string]config.NodeConfig) InstanceManager {
if instancesConfig.TimeoutCheckInterval <= 0 {
instancesConfig.TimeoutCheckInterval = 5 // Default to 5 minutes if not set
}
// Build node config map for quick lookup
nodeConfigMap := make(map[string]*config.NodeConfig)
for i := range nodesConfig {
nodeConfigMap[nodesConfig[i].Name] = &nodesConfig[i]
for name := range nodesConfig {
nodeCopy := nodesConfig[name]
nodeConfigMap[name] = &nodeCopy
}
im := &instanceManager{

View File

@@ -34,7 +34,7 @@ func TestNewInstanceManager(t *testing.T) {
TimeoutCheckInterval: 5,
}
mgr := manager.NewInstanceManager(backendConfig, cfg, []config.NodeConfig{})
mgr := manager.NewInstanceManager(backendConfig, cfg, map[string]config.NodeConfig{})
if mgr == nil {
t.Fatal("NewInstanceManager returned nil")
}
@@ -69,7 +69,7 @@ func TestPersistence(t *testing.T) {
}
// Test instance persistence on creation
manager1 := manager.NewInstanceManager(backendConfig, cfg, []config.NodeConfig{})
manager1 := manager.NewInstanceManager(backendConfig, cfg, map[string]config.NodeConfig{})
options := &instance.CreateInstanceOptions{
BackendType: backends.BackendTypeLlamaCpp,
LlamaServerOptions: &llamacpp.LlamaServerOptions{
@@ -90,7 +90,7 @@ func TestPersistence(t *testing.T) {
}
// Test loading instances from disk
manager2 := manager.NewInstanceManager(backendConfig, cfg, []config.NodeConfig{})
manager2 := manager.NewInstanceManager(backendConfig, cfg, map[string]config.NodeConfig{})
instances, err := manager2.ListInstances()
if err != nil {
t.Fatalf("ListInstances failed: %v", err)
@@ -207,7 +207,7 @@ func createTestManager() manager.InstanceManager {
DefaultRestartDelay: 5,
TimeoutCheckInterval: 5,
}
return manager.NewInstanceManager(backendConfig, cfg, []config.NodeConfig{})
return manager.NewInstanceManager(backendConfig, cfg, map[string]config.NodeConfig{})
}
func TestAutoRestartDisabledInstanceStatus(t *testing.T) {
@@ -227,7 +227,7 @@ func TestAutoRestartDisabledInstanceStatus(t *testing.T) {
}
// Create first manager and instance with auto-restart disabled
manager1 := manager.NewInstanceManager(backendConfig, cfg, []config.NodeConfig{})
manager1 := manager.NewInstanceManager(backendConfig, cfg, map[string]config.NodeConfig{})
autoRestart := false
options := &instance.CreateInstanceOptions{
@@ -252,7 +252,7 @@ func TestAutoRestartDisabledInstanceStatus(t *testing.T) {
manager1.Shutdown()
// Create second manager (simulating restart of llamactl)
manager2 := manager.NewInstanceManager(backendConfig, cfg, []config.NodeConfig{})
manager2 := manager.NewInstanceManager(backendConfig, cfg, map[string]config.NodeConfig{})
// Get the loaded instance
loadedInst, err := manager2.GetInstance("test-instance")

View File

@@ -75,7 +75,7 @@ func TestCreateInstance_ValidationAndLimits(t *testing.T) {
MaxInstances: 1, // Very low limit for testing
TimeoutCheckInterval: 5,
}
limitedManager := manager.NewInstanceManager(backendConfig, cfg, nil)
limitedManager := manager.NewInstanceManager(backendConfig, cfg, map[string]config.NodeConfig{})
_, err = limitedManager.CreateInstance("instance1", options)
if err != nil {

View File

@@ -23,7 +23,7 @@ func TestTimeoutFunctionality(t *testing.T) {
MaxInstances: 5,
}
manager := manager.NewInstanceManager(backendConfig, cfg, nil)
manager := manager.NewInstanceManager(backendConfig, cfg, map[string]config.NodeConfig{})
if manager == nil {
t.Fatal("Manager should be initialized with timeout checker")
}