mirror of
https://github.com/lordmathis/llamactl.git
synced 2025-11-05 16:44:22 +00:00
Remove redundant code
This commit is contained in:
@@ -54,16 +54,10 @@ func New(globalConfig *config.AppConfig) InstanceManager {
|
||||
|
||||
// Initialize port allocator
|
||||
portRange := globalConfig.Instances.PortRange
|
||||
ports, err := newPortAllocator(portRange[0], portRange[1])
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to create port allocator: %w", err)
|
||||
}
|
||||
ports := newPortAllocator(portRange[0], portRange[1])
|
||||
|
||||
// Initialize persistence
|
||||
persistence, err := newInstancePersister(globalConfig.Instances.InstancesDir)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to create instance persister: %w", err)
|
||||
}
|
||||
persistence := newInstancePersister(globalConfig.Instances.InstancesDir)
|
||||
|
||||
// Initialize remote manager
|
||||
remote := newRemoteManager(globalConfig.Nodes, 30*time.Second)
|
||||
|
||||
@@ -15,35 +15,18 @@ import (
|
||||
type instancePersister struct {
|
||||
mu sync.Mutex
|
||||
instancesDir string
|
||||
enabled bool
|
||||
}
|
||||
|
||||
// newInstancePersister creates a new instance persister.
|
||||
// If instancesDir is empty, persistence is disabled.
|
||||
func newInstancePersister(instancesDir string) (*instancePersister, error) {
|
||||
if instancesDir == "" {
|
||||
return &instancePersister{
|
||||
enabled: false,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Ensure the instances directory exists
|
||||
if err := os.MkdirAll(instancesDir, 0755); err != nil {
|
||||
return nil, fmt.Errorf("failed to create instances directory: %w", err)
|
||||
}
|
||||
|
||||
func newInstancePersister(instancesDir string) *instancePersister {
|
||||
return &instancePersister{
|
||||
instancesDir: instancesDir,
|
||||
enabled: true,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
// Save persists an instance to disk with atomic write
|
||||
func (p *instancePersister) save(inst *instance.Instance) error {
|
||||
if !p.enabled {
|
||||
return nil
|
||||
}
|
||||
|
||||
if inst == nil {
|
||||
return fmt.Errorf("cannot save nil instance")
|
||||
}
|
||||
@@ -103,10 +86,6 @@ func (p *instancePersister) save(inst *instance.Instance) error {
|
||||
|
||||
// Delete removes an instance's persistence file from disk.
|
||||
func (p *instancePersister) delete(name string) error {
|
||||
if !p.enabled {
|
||||
return nil
|
||||
}
|
||||
|
||||
validatedName, err := p.validateInstanceName(name)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -131,10 +110,6 @@ func (p *instancePersister) delete(name string) error {
|
||||
// LoadAll loads all persisted instances from disk.
|
||||
// Returns a slice of instances and any errors encountered during loading.
|
||||
func (p *instancePersister) loadAll() ([]*instance.Instance, error) {
|
||||
if !p.enabled {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
|
||||
|
||||
@@ -24,15 +24,7 @@ type portAllocator struct {
|
||||
}
|
||||
|
||||
// newPortAllocator creates a new port allocator for the given port range.
|
||||
// Returns an error if the port range is invalid.
|
||||
func newPortAllocator(minPort, maxPort int) (*portAllocator, error) {
|
||||
if minPort <= 0 || maxPort <= 0 {
|
||||
return nil, fmt.Errorf("invalid port range: min=%d, max=%d (must be > 0)", minPort, maxPort)
|
||||
}
|
||||
if minPort > maxPort {
|
||||
return nil, fmt.Errorf("invalid port range: min=%d > max=%d", minPort, maxPort)
|
||||
}
|
||||
|
||||
func newPortAllocator(minPort, maxPort int) *portAllocator {
|
||||
rangeSize := maxPort - minPort + 1
|
||||
bitmapSize := (rangeSize + 63) / 64 // Round up to nearest uint64
|
||||
|
||||
@@ -42,7 +34,7 @@ func newPortAllocator(minPort, maxPort int) (*portAllocator, error) {
|
||||
minPort: minPort,
|
||||
maxPort: maxPort,
|
||||
rangeSize: rangeSize,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
// allocate finds and allocates the first available port for the given instance.
|
||||
|
||||
Reference in New Issue
Block a user