Skip to content

Commit

Permalink
unixfs/feather: move retry logic to it's own next function
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorropo committed Nov 18, 2023
1 parent ca14179 commit c3929ff
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
51 changes: 31 additions & 20 deletions unixfs/feather/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,26 +210,9 @@ func (d *downloader) Read(b []byte) (_ int, err error) {
data = c.Hash()
data = data[len(data)-pref.MhLength:] // extract digest
default:
if err := verifcid.ValidateCid(verifcid.DefaultAllowlist, c); err != nil {
return 0, fmt.Errorf("cid %s don't pass safe test: %w", cidStringTruncate(c), err)
}
var errStartStream, errRead error
for {
if d.stream == nil {
if attempts := d.remainingAttempts; attempts != math.MaxUint {
if attempts == 0 {
return 0, errors.Join(errRead, errStartStream)
}
d.remainingAttempts = attempts - 1
}
errStartStream = d.startStream(todo)
}
data, errRead = d.readBlockFromStream(c)
if errRead == nil {
break
}
d.stream.Close()
d.stream = nil
data, err = d.next(todo)
if err != nil {
return 0, err
}
}

Expand Down Expand Up @@ -275,6 +258,34 @@ func (d *downloader) Read(b []byte) (_ int, err error) {
return n, nil
}

// next download the next block, it also handles performing retries if needed.
// The data return is hash correct.
func (d *downloader) next(todo region) ([]byte, error) {
c := todo.c
if err := verifcid.ValidateCid(verifcid.DefaultAllowlist, c); err != nil {
return nil, fmt.Errorf("cid %s don't pass safe test: %w", cidStringTruncate(c), err)
}
var errStartStream, errRead error
for {
if d.stream == nil {
if attempts := d.remainingAttempts; attempts != math.MaxUint {
if attempts == 0 {
return nil, fmt.Errorf("could not download next block: %w", errors.Join(errRead, errStartStream))
}
d.remainingAttempts = attempts - 1
}
errStartStream = d.startStream(todo)
}
var data []byte
data, errRead = d.readBlockFromStream(c)
if errRead == nil {
return data, nil
}
d.stream.Close()
d.stream = nil
}
}

// readBlockFromStream must perform hash verification on the input.
// The slice returned only has to be valid between two readBlockFromStream and Close calls.
// Implementations should reuse buffers to avoid allocations.
Expand Down
1 change: 0 additions & 1 deletion unixfs/feather/feather_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ func newGateway(t *testing.T, fixture string) (*httptest.Server, cid.Cid) {

ts := httptest.NewServer(handler)
t.Cleanup(func() { ts.Close() })
t.Logf("test server url: %s", ts.URL)

return ts, cids[0]
}
Expand Down

0 comments on commit c3929ff

Please sign in to comment.