Skip to content

Commit

Permalink
feat: upload reconstructed chunks during downloading
Browse files Browse the repository at this point in the history
  • Loading branch information
istae committed May 15, 2024
1 parent 92de285 commit a7a84fd
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion pkg/api/bzz.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,7 @@ func (s *Service) downloadHandler(logger log.Logger, w http.ResponseWriter, r *h
ChunkRetrievalTimeout *string `map:"Swarm-Chunk-Retrieval-Timeout"`
LookaheadBufferSize *int `map:"Swarm-Lookahead-Buffer-Size"`
Cache *bool `map:"Swarm-Cache"`
BatchID []byte `map:"Swarm-Postage-Batch-Id"`
}{}

if response := s.mapStructure(r.Header, &headers); response != nil {
Expand All @@ -543,7 +544,31 @@ func (s *Service) downloadHandler(logger log.Logger, w http.ResponseWriter, r *h
return
}

reader, l, err := joiner.New(ctx, s.storer.Download(cache), s.storer.Cache(), reference)
putter := s.storer.Cache()

if headers.BatchID != nil {
// Creates a direct uploader
stampUploadPutter, err := s.newStamperPutter(ctx, putterOptions{
BatchID: headers.BatchID,
Deferred: false,
})
if err != nil {
// TODO: error checking see line 109
return
}
defer func() {
// waits for any pending uploads
if err := stampUploadPutter.Done(swarm.ZeroAddress); err != nil {
logger.Error(err, "self repair uploader failed")
}
}()

putter = storage.PutterFunc(func(ctx context.Context, c swarm.Chunk) error {
return errors.Join(s.storer.Cache().Put(ctx, c), stampUploadPutter.Put(ctx, c))
})
}

reader, l, err := joiner.New(ctx, s.storer.Download(cache), putter, reference)
if err != nil {
if errors.Is(err, storage.ErrNotFound) || errors.Is(err, topology.ErrNotFound) {
logger.Debug("api download: not found ", "address", reference, "error", err)
Expand Down

0 comments on commit a7a84fd

Please sign in to comment.