diff --git a/api/backup_endpoints.go b/api/backup_endpoints.go index c3d2df1..a57b16a 100644 --- a/api/backup_endpoints.go +++ b/api/backup_endpoints.go @@ -2,14 +2,13 @@ package api import ( "fmt" + "github.com/patrickmn/go-cache" "strconv" - "sync" + "time" ) -var ( - backupUploadIDsMx sync.Mutex - backupUploadIDs = map[string]string{} -) +// backupUploadIDs stores a cache of active S3 backups. +var backupUploadIDs *cache.Cache type BackupRemoteUploadResponse struct { UploadID string `json:"upload_id"` @@ -34,9 +33,9 @@ func (r *Request) GetBackupRemoteUploadURLs(backup string, size int64) (*BackupR } // Store the backup upload id for later use, this is a janky way to be able to use it later with SendBackupStatus. - backupUploadIDsMx.Lock() - backupUploadIDs[backup] = res.UploadID - backupUploadIDsMx.Unlock() + // Yes, the timeout of 3 hours is intentional, if this value is removed before the backup completes, + // the backup will fail even if it uploaded properly. + backupUploadIDs.Set(backup, res.UploadID, time.Hour*3) return &res, nil } @@ -53,11 +52,9 @@ type BackupRequest struct { // available for a user to view and download. func (r *Request) SendBackupStatus(backup string, data BackupRequest) error { // Set the UploadID on the data. - backupUploadIDsMx.Lock() - if v, ok := backupUploadIDs[backup]; ok { - data.UploadID = v + if v, ok := backupUploadIDs.Get(backup); ok { + data.UploadID = v.(string) } - backupUploadIDsMx.Unlock() resp, err := r.Post(fmt.Sprintf("/backups/%s", backup), data) if err != nil {