Module ApplicationState

Module ApplicationState 

Source
Expand description

§Application State Management

This module provides comprehensive application state management for the Air daemon, including configuration, service status, resource tracking, and connection management.

§Architecture Overview

The ApplicationState acts as the central coordination point for the Air daemon, tracking all active connections, service states, requests, and system resources. It follows the VSCode daemon state management pattern with thread-safe, async-friendly data structures.

§Core Responsibilities

  1. Connection Management

    • Track all connected clients (Mountain, Cocoon, Wind, etc.)
    • Heartbeat monitoring and validation
    • Connection pooling for Mountain clients
    • Graceful disconnection handling
    • Stale connection cleanup
  2. Service Status Tracking

    • Monitor authentication service
    • Track update service state
    • Monitor indexing service
    • Track downloader service
    • gRPC service status
  3. Request Management

    • Active request tracking
    • Request lifecycle management
    • Progress monitoring
    • Request cancellation support
  4. Resource Monitoring

    • Memory usage tracking
    • CPU usage monitoring
    • Disk usage tracking
    • Network usage monitoring
    • Resource limit enforcement
  5. Performance Metrics

    • Request count tracking
    • Success/failure rates
    • Average response time calculation
    • Uptime tracking

§Connection Types

  • MountainMain: Main editor process connection
  • MountainWorker: Background worker processes
  • Cocoon: Deployment and build system
  • Wind: Communication layer
  • External: Third-party integrations

§TODO Items

  • Implement connection pooling for Mountain clients with load balancing
  • Add connection encryption and authentication
  • Implement connection rate limiting
  • Add connection-based resource quotas
  • Implement connection health checks with automatic recovery
  • Add connection-based feature flags
  • Implement connection audit logging
  • Add connection-based request prioritization
  • Implement connection state persistence across restarts
  • Add distributed connection support for multi-node deployments

§Thread Safety

All state access is protected by async-friendly locks:

  • RwLock<T> for read-heavy data (configuration, metrics, connections)
  • Mutex<T> for write-heavy data (active requests, background tasks)

This ensures concurrent access from multiple async tasks and threads is safe and efficient.

§Performance Considerations

  • Use read locks where possible to allow concurrent access
  • Minimize lock duration
  • Use async-friendly lock types to avoid blocking runtime
  • Consider lock-free alternatives for high-frequency updates

§Error Handling

All methods return Result<T> with comprehensive error types:

  • Configuration: Invalid configuration values
  • ResourceLimit: Resource limits exceeded
  • Internal: Internal state inconsistencies

Structs§

ApplicationState
Application state structure
ConnectionHealthReport
Connection health report
ConnectionInfo
Connection information for Mountain clients
PerformanceMetrics
Performance metrics
RequestStatus
Request status tracking
ResourceUsage
Resource usage tracking

Enums§

ConnectionType
Connection type enum
RequestState
Request state enum
ServiceStatus
Service status enum