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