Skip to content

Commit b634709

Browse files
committed
simplify
1 parent 4369667 commit b634709

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

ipld/unixfs/io/directory.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -502,9 +502,8 @@ func (d *HAMTDirectory) sizeBelowThreshold(ctx context.Context, sizeChange int)
502502

503503
// We stop the enumeration once we have enough information and exit this function.
504504
ctx, cancel := context.WithCancel(ctx)
505-
defer cancel()
506-
507505
linkResults := d.EnumLinksAsync(ctx)
506+
508507
for linkResult := range linkResults {
509508
if linkResult.Err != nil {
510509
below = false
@@ -514,21 +513,21 @@ func (d *HAMTDirectory) sizeBelowThreshold(ctx context.Context, sizeChange int)
514513

515514
partialSize += linksize.LinkSizeFunction(linkResult.Link.Name, linkResult.Link.Cid)
516515
if partialSize+sizeChange >= HAMTShardingSize {
517-
// We have already fetched enough shards to assert we are
518-
// above the threshold, so no need to keep fetching.
516+
// We have already fetched enough shards to assert we are above the
517+
// threshold, so no need to keep fetching.
519518
below = false
520519
break
521520
}
522521
}
522+
cancel()
523523

524524
if !below {
525-
cancel()
525+
// Wait for channel to close so links are not being read after return.
526526
for range linkResults {
527527
}
528528
return false, err
529-
}
529+
} // else enumerated all links in all shards before threshold reached.
530530

531-
// We enumerated *all* links in all shards and didn't reach the threshold.
532531
return true, nil
533532
}
534533

0 commit comments

Comments
 (0)