Recover DB connection automatically after network returns
This commit is contained in:
@@ -94,6 +94,8 @@ func (cm *ConnectionManager) GetDB() (*gorm.DB, error) {
|
||||
// Attempt to connect
|
||||
err := cm.connect()
|
||||
if err != nil {
|
||||
// Drop stale handle so callers don't treat it as an active connection.
|
||||
cm.db = nil
|
||||
cm.lastError = err
|
||||
cm.lastCheck = time.Now()
|
||||
return nil, err
|
||||
@@ -147,23 +149,27 @@ func (cm *ConnectionManager) connect() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// IsOnline checks if the database is currently connected and responsive
|
||||
// Does not attempt to reconnect, only checks current state with caching
|
||||
// IsOnline checks if the database is currently connected and responsive.
|
||||
// If disconnected, it tries to reconnect (respecting cooldowns in GetDB).
|
||||
func (cm *ConnectionManager) IsOnline() bool {
|
||||
cm.mu.RLock()
|
||||
if cm.db == nil {
|
||||
cm.mu.RUnlock()
|
||||
return false
|
||||
}
|
||||
|
||||
// If we've checked recently, return cached result
|
||||
if time.Since(cm.lastCheck) < cm.pingInterval {
|
||||
cm.mu.RUnlock()
|
||||
return true
|
||||
}
|
||||
isDisconnected := cm.db == nil
|
||||
lastErr := cm.lastError
|
||||
checkedRecently := time.Since(cm.lastCheck) < cm.pingInterval
|
||||
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()
|
||||
defer cm.mu.Unlock()
|
||||
|
||||
@@ -282,7 +288,7 @@ func extractHostFromDSN(dsn string) string {
|
||||
}
|
||||
if parenEnd != -1 {
|
||||
// Extract host:port part between tcp( and )
|
||||
hostPort := dsn[tcpStart+1:parenEnd]
|
||||
hostPort := dsn[tcpStart+1 : parenEnd]
|
||||
return hostPort
|
||||
}
|
||||
}
|
||||
@@ -317,7 +323,7 @@ func extractHostFromDSN(dsn string) string {
|
||||
}
|
||||
|
||||
if parenEnd != -1 {
|
||||
hostPort := dsn[parenStart+1:parenEnd]
|
||||
hostPort := dsn[parenStart+1 : parenEnd]
|
||||
return hostPort
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user