Change backup upload id cache to use the cache package

This commit is contained in:
Matthew Penner 2020-12-06 15:23:44 -07:00
parent ee08829a28
commit bc3d92f9e6

View File

@ -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 {