From 53e020851693a78c34e542f38fc0a0cd0e98930c Mon Sep 17 00:00:00 2001 From: Thorsten Ball Date: Thu, 7 Jul 2022 14:58:00 +0200 Subject: [PATCH] Add comments and remove interface --- cmd/src/batch_common.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/cmd/src/batch_common.go b/cmd/src/batch_common.go index 1fd867f8c2a..fc29e934739 100644 --- a/cmd/src/batch_common.go +++ b/cmd/src/batch_common.go @@ -487,14 +487,12 @@ func executeBatchSpec(ctx context.Context, ui ui.ExecUI, opts executeBatchSpecOp return nil } -type ReadDeadliner interface { - SetReadDeadline(t time.Time) error -} - -func SetReadDeadlineOnCancel(ctx context.Context, d ReadDeadliner) { +func setReadDeadlineOnCancel(ctx context.Context, f *os.File) { go func() { + // When user cancels, we set the read deadline to now() so the runtime + // cancels the read and we don't block. <-ctx.Done() - d.SetReadDeadline(time.Now()) + f.SetReadDeadline(time.Now()) }() } @@ -510,9 +508,11 @@ func parseBatchSpec(ctx context.Context, file string, svc *service.Service, isRe } defer f.Close() + // Create new ctx so we ensure that the goroutine in + // setReadDeadlineOnCancel exits at end of function. ctx, cancel := context.WithCancel(ctx) defer cancel() - SetReadDeadlineOnCancel(ctx, f) + setReadDeadlineOnCancel(ctx, f) data, err := io.ReadAll(f) if err != nil {