-
Notifications
You must be signed in to change notification settings - Fork 1
Open
Description
π― 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.go2,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