Skip to content

Commit 75e40e3

Browse files
authored
timeout for migrate-lid (#1631)
* timeout for migrate-lid * add timeout context at 60sec. and hard cancel at 75sec.
1 parent 8818d91 commit 75e40e3

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

cmd/migrate-lid/migrate_lid.go

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,17 @@ func migrateIndices(ctx context.Context, logger *zap.SugaredLogger, bar *progres
245245

246246
start := time.Now()
247247

248-
indexed, err := migrateIndex(ctx, ipath, store, force)
248+
timeoutCtx, timeoutCancel := context.WithTimeout(ctx, 60*time.Second)
249+
defer timeoutCancel()
250+
251+
indexed, err := migrateIndexWithTimeout(timeoutCtx, ipath, store, force)
249252
bar.Add(1) //nolint:errcheck
250253
if err != nil {
251-
logger.Errorw("migrate index failed", "piece cid", ipath.name, "err", err)
254+
took := time.Since(start)
255+
indexTime += took
256+
257+
logger.Errorw("migrate index failed", "piece cid", ipath.name, "took", took.String(), "err", err)
258+
252259
errCount++
253260
continue
254261
}
@@ -268,6 +275,32 @@ func migrateIndices(ctx context.Context, logger *zap.SugaredLogger, bar *progres
268275
return errCount, nil
269276
}
270277

278+
type migrateIndexResult struct {
279+
Indexed bool
280+
Error error
281+
}
282+
283+
func migrateIndexWithTimeout(ctx context.Context, ipath idxPath, store StoreMigrationApi, force bool) (bool, error) {
284+
result := make(chan migrateIndexResult, 1)
285+
go func() {
286+
result <- doMigrateIndex(ctx, ipath, store, force)
287+
}()
288+
select {
289+
case <-time.After(75 * time.Second):
290+
return false, errors.New("index migration timed out after 75 seconds")
291+
case result := <-result:
292+
return result.Indexed, result.Error
293+
}
294+
}
295+
296+
func doMigrateIndex(ctx context.Context, ipath idxPath, store StoreMigrationApi, force bool) migrateIndexResult {
297+
indexed, err := migrateIndex(ctx, ipath, store, force)
298+
return migrateIndexResult{
299+
Indexed: indexed,
300+
Error: err,
301+
}
302+
}
303+
271304
func migrateIndex(ctx context.Context, ipath idxPath, store StoreMigrationApi, force bool) (bool, error) {
272305
pieceCid, err := cid.Parse(ipath.name)
273306
if err != nil {

0 commit comments

Comments
 (0)