This document describes the automatic connection management feature implemented in HeyNATS.
The connection management system automatically handles NATS connections by:
- Activity Tracking: Monitoring when connections are last used
- Idle Timeout: Automatically closing connections after a period of inactivity
- Automatic Reconnection: Reconnecting when the frontend sends new requests
- Idle Timeout: 30 minutes (connections are closed after 30 minutes of inactivity)
- Check Interval: 5 minutes (system checks for idle connections every 5 minutes)
You can customize the connection management settings by modifying the NewNatsConnection() call in main.go:
// Custom configuration example
config := api.ConnectionStoreConfig{
IdleTimeout: 15 * time.Minute, // Close after 15 minutes
CheckInterval: 2 * time.Minute, // Check every 2 minutes
}
natsConnections := api.NewNatsConnectionWithConfig(config)- Every API request updates the
LastActivitytimestamp for the connection - The middleware automatically handles this tracking
- No changes needed in existing API handlers
- A background goroutine runs every
checkIntervalminutes - It checks all connections for inactivity exceeding
idleTimeout - Idle connections are gracefully closed and removed from the store
- When a request comes in for a closed/dead connection:
- The system attempts to reconnect using stored credentials
- If successful, the connection is restored transparently
- If reconnection fails, appropriate error responses are returned
GET /api/nats/connection/stats
Returns information about the current connection:
{
"connection_id": "uuid-string",
"last_activity": "2024-01-01T12:00:00Z",
"idle_timeout": "30m0s",
"time_remaining": "25m30s",
"is_healthy": true,
"is_connected": true
}All existing NATS endpoints continue to work as before, with automatic connection management happening transparently.
- Resource Efficiency: Prevents accumulation of unused connections
- Automatic Recovery: Seamless reconnection when needed
- Transparent Operation: Existing frontend code requires no changes
- Configurable: Timeout values can be adjusted based on usage patterns
- Monitoring: Connection statistics available for debugging
type ConnectionInfo struct {
Connection *pkg.NATSCredential
LastActivity time.Time
Config *pkg.ConnectionRequest // For reconnection
}GetOrReconnect(): Retrieves connection, reconnecting if necessaryUpdateActivity(): Updates last activity timestampcleanupIdleConnections(): Background cleanup process
The system uses enhanced health checking:
- Basic connectivity test
- Ping/flush timeout validation
- Connection state verification
The system logs important connection events:
- Connection cleanups
- Reconnection attempts
- Health check failures
Use /api/nats/connection/stats to monitor:
- Connection health status
- Time until idle timeout
- Last activity timestamp
The application handles graceful shutdown by:
- Stopping the cleanup goroutine
- Closing all active connections
- Cleaning up resources properly