use a channel for dump err

This commit is contained in:
Gareth
2025-09-28 01:03:11 -07:00
parent 1a1afd0b7c
commit fcebc9b693

View File

@@ -14,7 +14,6 @@ import (
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
"sync"
"time" "time"
v1 "github.com/garethgeorge/backrest/gen/go/v1" v1 "github.com/garethgeorge/backrest/gen/go/v1"
@@ -73,14 +72,11 @@ func handleIndexSnapshotDownload(w http.ResponseWriter, r *http.Request, orchest
return return
} }
var dumpErrMu sync.Mutex dumpErrCh := make(chan error, 1)
var dumpErr error
piper, pipew := io.Pipe() piper, pipew := io.Pipe()
go func() { go func() {
dumpErrMu.Lock() dumpErrCh <- repo.Dump(r.Context(), indexOp.OperationIndexSnapshot.Snapshot.GetId(), filePath, pipew)
dumpErr = repo.Dump(r.Context(), indexOp.OperationIndexSnapshot.Snapshot.GetId(), filePath, pipew)
dumpErrMu.Unlock()
pipew.Close() pipew.Close()
}() }()
@@ -92,14 +88,15 @@ func handleIndexSnapshotDownload(w http.ResponseWriter, r *http.Request, orchest
return return
} }
dumpErrMu.Lock() select {
if dumpErr != nil { case dumpErr := <-dumpErrCh:
zap.S().Errorf("error dumping snapshot: %v", dumpErr) if dumpErr != nil {
http.Error(w, fmt.Sprintf("error dumping snapshot: %v", dumpErr), http.StatusInternalServerError) zap.S().Errorf("error dumping snapshot: %v", dumpErr)
dumpErrMu.Unlock() http.Error(w, fmt.Sprintf("error dumping snapshot: %v", dumpErr), http.StatusInternalServerError)
return return
}
default:
} }
dumpErrMu.Unlock()
if IsTarArchive(bytes.NewReader(firstBytesBuffer.Bytes())) { if IsTarArchive(bytes.NewReader(firstBytesBuffer.Bytes())) {
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%v.tar", filePath)) w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%v.tar", filePath))