Files
mirror-3x-ui/internal/web/middleware/database_work.go
T
Hamed 12e0b076a8 fix(database): coordinate migrations and restores across processes (#7322)
Coordinate database maintenance across panel, CLI, node synchronization, and restore paths.

- Add cross-process PostgreSQL advisory locks and pinned execution fences; cancel CLI work when its lease session is lost.
- Serialize SQLite file replacement with OS-level process locks and stable lock identity.
- Drain traffic writers and node-sync work before restore, then resume after migration and validation.
- Persist PostgreSQL restore-pending state and recover interrupted restores on startup.
- Add regression coverage for concurrent migrations/restores, session-loss fencing, CLI work cancellation, and recovery.

Closes #7318.
2026-12-03 12:05:51 +03:30

34 lines
751 B
Go

package middleware
import (
"net/http"
"sync"
"github.com/gin-gonic/gin"
)
const databaseWorkKey = "database-work-release"
// DatabaseWork prevents requests from using a database while its pool is replaced.
func DatabaseWork(begin func() (func(), bool)) gin.HandlerFunc {
return func(c *gin.Context) {
end, ok := begin()
if !ok {
c.AbortWithStatus(http.StatusServiceUnavailable)
return
}
release := sync.OnceFunc(end)
c.Set(databaseWorkKey, release)
defer release()
c.Next()
}
}
// ReleaseDatabaseWork lets the authenticated restore handler drain other requests
// without waiting for its own admission.
func ReleaseDatabaseWork(c *gin.Context) {
if release, ok := c.Get(databaseWorkKey); ok {
release.(func())()
}
}