Skip to content

Commit

Permalink
timeout for migrate-lid (#1631)
Browse files Browse the repository at this point in the history
* timeout for migrate-lid

* add timeout context at 60sec. and hard cancel at 75sec.
  • Loading branch information
nonsense authored Aug 17, 2023
1 parent 8818d91 commit 75e40e3
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions cmd/migrate-lid/migrate_lid.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,17 @@ func migrateIndices(ctx context.Context, logger *zap.SugaredLogger, bar *progres

start := time.Now()

indexed, err := migrateIndex(ctx, ipath, store, force)
timeoutCtx, timeoutCancel := context.WithTimeout(ctx, 60*time.Second)
defer timeoutCancel()

indexed, err := migrateIndexWithTimeout(timeoutCtx, ipath, store, force)
bar.Add(1) //nolint:errcheck
if err != nil {
logger.Errorw("migrate index failed", "piece cid", ipath.name, "err", err)
took := time.Since(start)
indexTime += took

logger.Errorw("migrate index failed", "piece cid", ipath.name, "took", took.String(), "err", err)

errCount++
continue
}
Expand All @@ -268,6 +275,32 @@ func migrateIndices(ctx context.Context, logger *zap.SugaredLogger, bar *progres
return errCount, nil
}

type migrateIndexResult struct {
Indexed bool
Error error
}

func migrateIndexWithTimeout(ctx context.Context, ipath idxPath, store StoreMigrationApi, force bool) (bool, error) {
result := make(chan migrateIndexResult, 1)
go func() {
result <- doMigrateIndex(ctx, ipath, store, force)
}()
select {
case <-time.After(75 * time.Second):
return false, errors.New("index migration timed out after 75 seconds")
case result := <-result:
return result.Indexed, result.Error
}
}

func doMigrateIndex(ctx context.Context, ipath idxPath, store StoreMigrationApi, force bool) migrateIndexResult {
indexed, err := migrateIndex(ctx, ipath, store, force)
return migrateIndexResult{
Indexed: indexed,
Error: err,
}
}

func migrateIndex(ctx context.Context, ipath idxPath, store StoreMigrationApi, force bool) (bool, error) {
pieceCid, err := cid.Parse(ipath.name)
if err != nil {
Expand Down

0 comments on commit 75e40e3

Please sign in to comment.