Remove redundant code

This commit is contained in:
2025-10-27 18:18:25 +01:00
parent e65f4f1641
commit 14131a6274
3 changed files with 6 additions and 45 deletions

View File

@@ -54,16 +54,10 @@ func New(globalConfig *config.AppConfig) InstanceManager {
// Initialize port allocator // Initialize port allocator
portRange := globalConfig.Instances.PortRange portRange := globalConfig.Instances.PortRange
ports, err := newPortAllocator(portRange[0], portRange[1]) ports := newPortAllocator(portRange[0], portRange[1])
if err != nil {
log.Fatalf("Failed to create port allocator: %w", err)
}
// Initialize persistence // Initialize persistence
persistence, err := newInstancePersister(globalConfig.Instances.InstancesDir) persistence := newInstancePersister(globalConfig.Instances.InstancesDir)
if err != nil {
log.Fatalf("Failed to create instance persister: %w", err)
}
// Initialize remote manager // Initialize remote manager
remote := newRemoteManager(globalConfig.Nodes, 30*time.Second) remote := newRemoteManager(globalConfig.Nodes, 30*time.Second)

View File

@@ -15,35 +15,18 @@ import (
type instancePersister struct { type instancePersister struct {
mu sync.Mutex mu sync.Mutex
instancesDir string instancesDir string
enabled bool
} }
// newInstancePersister creates a new instance persister. // newInstancePersister creates a new instance persister.
// If instancesDir is empty, persistence is disabled. // If instancesDir is empty, persistence is disabled.
func newInstancePersister(instancesDir string) (*instancePersister, error) { func newInstancePersister(instancesDir string) *instancePersister {
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)
}
return &instancePersister{ return &instancePersister{
instancesDir: instancesDir, instancesDir: instancesDir,
enabled: true, }
}, nil
} }
// Save persists an instance to disk with atomic write // Save persists an instance to disk with atomic write
func (p *instancePersister) save(inst *instance.Instance) error { func (p *instancePersister) save(inst *instance.Instance) error {
if !p.enabled {
return nil
}
if inst == nil { if inst == nil {
return fmt.Errorf("cannot save nil instance") 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. // Delete removes an instance's persistence file from disk.
func (p *instancePersister) delete(name string) error { func (p *instancePersister) delete(name string) error {
if !p.enabled {
return nil
}
validatedName, err := p.validateInstanceName(name) validatedName, err := p.validateInstanceName(name)
if err != nil { if err != nil {
return err return err
@@ -131,10 +110,6 @@ func (p *instancePersister) delete(name string) error {
// LoadAll loads all persisted instances from disk. // LoadAll loads all persisted instances from disk.
// Returns a slice of instances and any errors encountered during loading. // Returns a slice of instances and any errors encountered during loading.
func (p *instancePersister) loadAll() ([]*instance.Instance, error) { func (p *instancePersister) loadAll() ([]*instance.Instance, error) {
if !p.enabled {
return nil, nil
}
p.mu.Lock() p.mu.Lock()
defer p.mu.Unlock() defer p.mu.Unlock()

View File

@@ -24,15 +24,7 @@ type portAllocator struct {
} }
// newPortAllocator creates a new port allocator for the given port range. // 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 {
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)
}
rangeSize := maxPort - minPort + 1 rangeSize := maxPort - minPort + 1
bitmapSize := (rangeSize + 63) / 64 // Round up to nearest uint64 bitmapSize := (rangeSize + 63) / 64 // Round up to nearest uint64
@@ -42,7 +34,7 @@ func newPortAllocator(minPort, maxPort int) (*portAllocator, error) {
minPort: minPort, minPort: minPort,
maxPort: maxPort, maxPort: maxPort,
rangeSize: rangeSize, rangeSize: rangeSize,
}, nil }
} }
// allocate finds and allocates the first available port for the given instance. // allocate finds and allocates the first available port for the given instance.