Skip to content

Alpamon Agent RefactoringΒ #131

@geunwoonoh

Description

@geunwoonoh

🎯 Objectives

Refactor Alpamon into a stable and maintainable system agent. Apply the Agent Pattern proven by successful agents.

Key Improvements

  • βœ… Goroutine Management: Unlimited creation β†’ Limited pool (prevent memory leaks)
  • βœ… God Object Resolution: command.go 2,189 lines β†’ Split into handlers under 200 lines each
  • βœ… Context Propagation: Non-cancellable operations β†’ All operations cancellable (graceful shutdown)
  • βœ… Testability: Massive switch statement β†’ Mockable Handler interfaces

πŸ“Š Current Problems

1. God Object Anti-pattern

  • pkg/runner/command.go: 2,189 lines (41% of entire runner package)
  • 14+ responsibilities mixed in a single file
  • Untestable massive switch statement

2. Goroutine Memory Leak Risk

// pkg/runner/client.go:230-231
case "command":
    commandRunner := NewCommandRunner(...)
    go commandRunner.Run()  // ❌ Unlimited goroutine creation
  • No limit on concurrent commands
  • Goroutine leak if command hangs
  • No context propagation = cannot cancel

3. Circular Dependencies

  • runner β†’ utils β†’ runner (via function pointers)
  • 10+ global variables (utils/firewall.go)

πŸš€ Progress

  • Phase 1: Goroutine Pool + Context Management
  • Phase 2: Command Handler Separation
  • Phase 3: Firewall Refactoring
  • Phase 4: Transport Layer Separation
  • Phase 5: Testing & Validation

πŸ“ˆ Success Metrics

Metric Current Target Improvement
Max File Size 2,189 lines < 300 lines 86% reduction
Goroutine Management Unlimited Pool limit (10) Memory stabilization
Context Propagation 0% 100% Full coverage
Test Coverage Nearly impossible 80%+ Mock-based testing

πŸ—οΈ New Architecture

Agent Pattern-based Structure

alpamon/
β”œβ”€β”€ cmd/alpamon/           # CLI entry point
β”œβ”€β”€ pkg/
β”‚   β”œβ”€β”€ agent/            # Core agent runtime
β”‚   β”‚   β”œβ”€β”€ agent.go      # Agent orchestration
β”‚   β”‚   β”œβ”€β”€ lifecycle.go  # Start/stop/restart management
β”‚   β”‚   └── context.go    # Context management
β”‚   β”œβ”€β”€ executor/         # Command execution (runner refactored)
β”‚   β”‚   β”œβ”€β”€ executor.go   # Base executor + goroutine pool
β”‚   β”‚   β”œβ”€β”€ handlers/     # Command handlers (command.go decomposed)
β”‚   β”‚   β”‚   β”œβ”€β”€ handler.go    # Handler interface
β”‚   β”‚   β”‚   β”œβ”€β”€ system.go     # upgrade, restart, reboot
β”‚   β”‚   β”‚   β”œβ”€β”€ user.go       # User/group management
β”‚   β”‚   β”‚   β”œβ”€β”€ firewall.go   # Firewall operations
β”‚   β”‚   β”‚   β”œβ”€β”€ file.go       # File upload/download
β”‚   β”‚   β”‚   └── shell.go      # Shell command execution
β”‚   β”‚   └── pool.go       # Worker Pool implementation
β”‚   β”œβ”€β”€ transport/        # Communication layer (separated from runner)
β”‚   β”‚   β”œβ”€β”€ websocket.go  # WebSocket client
β”‚   β”‚   β”œβ”€β”€ protocol.go   # Protocol definition
β”‚   β”‚   └── dispatcher.go # Command dispatcher
β”‚   └── collector/        # Metrics collection (keep existing - excellent)
└── internal/             # Internal packages
    β”œβ”€β”€ pool/            # Goroutine pool implementation
    └── shutdown/        # Graceful shutdown
  • This architecture is subject to change based on future development.

Core Design Patterns

  • Handler Pattern: Independent handler per command type
  • Object Pool: Goroutine reuse and memory management
  • Strategy Pattern: Firewall backend selection (iptables/nftables)
  • Registry Pattern: Dynamic handler registration and management

βœ… Success Criteria

Must-Achieve Goals

  • Max 300 lines per file (current: 2,189 lines)
  • Zero goroutine leaks (verified by tests)
  • 100% context cancellation support (all long-running operations)
  • 80%+ unit test coverage (each handler independently testable)
  • Memory < 50MB (idle state)
  • Startup time < 1 second

Safety Guarantees

  • 100% existing functionality preserved (backward compatible)
  • Canary testing before production deployment
  • Rollback plan ready (tag: v1.0-pre-refactor)

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions