2021-04-06 10:08:43 +00:00
|
|
|
package offlinegate
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2022-03-18 16:20:10 +00:00
|
|
|
"strings"
|
2021-04-06 10:08:43 +00:00
|
|
|
"time"
|
|
|
|
|
2023-09-01 22:27:02 +00:00
|
|
|
httperror "github.com/portainer/portainer/pkg/libhttp/error"
|
2022-09-16 16:18:44 +00:00
|
|
|
|
|
|
|
"github.com/rs/zerolog/log"
|
2022-03-18 16:20:10 +00:00
|
|
|
lock "github.com/viney-shih/go-lock"
|
2021-04-06 10:08:43 +00:00
|
|
|
)
|
|
|
|
|
2022-03-18 16:20:10 +00:00
|
|
|
// OfflineGate is an entity that works similar to a mutex with signaling
|
|
|
|
// Only the caller that has Locked a gate can unlock it, otherwise it will be blocked with a call to Lock.
|
2021-04-06 10:08:43 +00:00
|
|
|
// Gate provides a passthrough http middleware that will wait for a locked gate to be unlocked.
|
2022-03-18 16:20:10 +00:00
|
|
|
// For safety reasons, the middleware will timeout
|
2021-04-06 10:08:43 +00:00
|
|
|
type OfflineGate struct {
|
2022-03-18 16:20:10 +00:00
|
|
|
lock *lock.CASMutex
|
2021-04-06 10:08:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewOfflineGate creates a new gate
|
|
|
|
func NewOfflineGate() *OfflineGate {
|
|
|
|
return &OfflineGate{
|
2022-03-18 16:20:10 +00:00
|
|
|
lock: lock.NewCASMutex(),
|
2021-04-06 10:08:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lock locks readonly gate and returns a function to unlock
|
|
|
|
func (o *OfflineGate) Lock() func() {
|
|
|
|
o.lock.Lock()
|
2022-03-18 16:20:10 +00:00
|
|
|
return o.lock.Unlock
|
2021-04-06 10:08:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WaitingMiddleware returns an http handler that waits for the gate to be unlocked before continuing
|
|
|
|
func (o *OfflineGate) WaitingMiddleware(timeout time.Duration, next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2022-03-18 16:20:10 +00:00
|
|
|
if r.Method == "GET" || r.Method == "HEAD" || r.Method == "OPTIONS" || strings.HasPrefix(r.URL.Path, "/api/backup") || strings.HasPrefix(r.URL.Path, "/api/restore") {
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
return
|
2021-04-06 10:08:43 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 16:20:10 +00:00
|
|
|
if !o.lock.RTryLockWithTimeout(timeout) {
|
2022-09-16 16:18:44 +00:00
|
|
|
log.Error().Msg("timeout waiting for the offline gate to signal")
|
2022-03-18 16:20:10 +00:00
|
|
|
httperror.WriteError(w, http.StatusRequestTimeout, "Timeout waiting for the offline gate to signal", http.ErrHandlerTimeout)
|
|
|
|
return
|
|
|
|
}
|
2021-04-06 10:08:43 +00:00
|
|
|
next.ServeHTTP(w, r)
|
2022-03-18 16:20:10 +00:00
|
|
|
o.lock.RUnlock()
|
2021-04-06 10:08:43 +00:00
|
|
|
})
|
|
|
|
}
|