Recover DB connection automatically after network returns

This commit is contained in:
Mikhail Chusavitin
2026-02-04 11:43:31 +03:00
parent d094d39427
commit f42b850734

View File

@@ -94,6 +94,8 @@ func (cm *ConnectionManager) GetDB() (*gorm.DB, error) {
// Attempt to connect // Attempt to connect
err := cm.connect() err := cm.connect()
if err != nil { if err != nil {
// Drop stale handle so callers don't treat it as an active connection.
cm.db = nil
cm.lastError = err cm.lastError = err
cm.lastCheck = time.Now() cm.lastCheck = time.Now()
return nil, err return nil, err
@@ -147,23 +149,27 @@ func (cm *ConnectionManager) connect() error {
return nil return nil
} }
// IsOnline checks if the database is currently connected and responsive // IsOnline checks if the database is currently connected and responsive.
// Does not attempt to reconnect, only checks current state with caching // If disconnected, it tries to reconnect (respecting cooldowns in GetDB).
func (cm *ConnectionManager) IsOnline() bool { func (cm *ConnectionManager) IsOnline() bool {
cm.mu.RLock() cm.mu.RLock()
if cm.db == nil { isDisconnected := cm.db == nil
cm.mu.RUnlock() lastErr := cm.lastError
return false checkedRecently := time.Since(cm.lastCheck) < cm.pingInterval
}
// If we've checked recently, return cached result
if time.Since(cm.lastCheck) < cm.pingInterval {
cm.mu.RUnlock()
return true
}
cm.mu.RUnlock() cm.mu.RUnlock()
// Need to perform actual ping // Try reconnect in disconnected state.
if isDisconnected {
_, err := cm.GetDB()
return err == nil
}
// If we've checked recently, return cached result.
if checkedRecently {
return lastErr == nil
}
// Need to perform actual ping.
cm.mu.Lock() cm.mu.Lock()
defer cm.mu.Unlock() defer cm.mu.Unlock()