Skip to content

Commit

Permalink
Add comments and remove interface
Browse files Browse the repository at this point in the history
  • Loading branch information
mrnugget committed Jul 7, 2022
1 parent 3d3ce9b commit 53e0208
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions cmd/src/batch_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}()
}

Expand All @@ -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 {
Expand Down

0 comments on commit 53e0208

Please sign in to comment.