Skip to content

Commit

Permalink
DeleteSectors supports context-based cancellation
Browse files Browse the repository at this point in the history
  • Loading branch information
jkawamoto committed Apr 19, 2021
1 parent d956dd5 commit a8855da
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions renter/renterutil/strategies.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,24 @@ func downloadCtx(ctx context.Context, sess *proto.Session, key renter.KeySeed, s
return
}

func deleteCtx(ctx context.Context, sess *proto.Session, roots []crypto.Hash) error {
if ctx.Err() != nil {
return ctx.Err()
}
done := make(chan error)
go func() {
done <- sess.DeleteSectors(roots)
}()
select {
case <-ctx.Done():
sess.Interrupt()
<-done // wait for goroutine to exit
return ctx.Err()
case err := <-done:
return err
}
}

// A ChunkUploader uploads shards, associating them with a given chunk.
type ChunkUploader interface {
UploadChunk(ctx context.Context, db MetaDB, c DBChunk, key renter.KeySeed, shards [][]byte) error
Expand Down Expand Up @@ -1299,12 +1317,11 @@ type SerialSectorDeleter struct {
// DeleteSectors implements SectorDeleter.
func (ssd SerialSectorDeleter) DeleteSectors(ctx context.Context, db MetaDB, sectors map[hostdb.HostPublicKey][]crypto.Hash) error {
for hostKey, roots := range sectors {
h, err := ssd.Hosts.acquire(hostKey)
h, err := acquireCtx(ctx, ssd.Hosts, hostKey, true)
if err != nil {
return err
}
// TODO: respect ctx
err = h.DeleteSectors(roots)
err = deleteCtx(ctx, h, roots)
ssd.Hosts.release(hostKey)
if err != nil && !errors.Is(err, contractmanager.ErrSectorNotFound) {
return err
Expand All @@ -1325,12 +1342,11 @@ func (psd ParallelSectorDeleter) DeleteSectors(ctx context.Context, db MetaDB, s
for hostKey, roots := range sectors {
go func(hostKey hostdb.HostPublicKey, roots []crypto.Hash) {
errCh <- func() *HostError {
h, err := psd.Hosts.acquire(hostKey)
h, err := acquireCtx(ctx, psd.Hosts, hostKey, true)
if err != nil {
return &HostError{hostKey, err}
}
// TODO: respect ctx
err = h.DeleteSectors(roots)
err = deleteCtx(ctx, h, roots)
psd.Hosts.release(hostKey)
if err != nil && !errors.Is(err, contractmanager.ErrSectorNotFound) {
return &HostError{hostKey, err}
Expand Down

0 comments on commit a8855da

Please sign in to comment.