Skip to content

Commit

Permalink
Fix usage report block of k6 execution if it takes longer than 3s
Browse files Browse the repository at this point in the history
  • Loading branch information
na-- committed Feb 25, 2023
1 parent 2305316 commit 15a2e28
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,22 +307,17 @@ func (c *cmdRun) run(cmd *cobra.Command, args []string) (err error) {
// Init has passed successfully, so unless disabled, make sure we send a
// usage report after the context is done.
if !conf.NoUsageReport.Bool {
backgroundProcesses.Add(2)
reportCtx, reportCancel := context.WithCancel(globalCtx)
reportDone := make(chan error)
backgroundProcesses.Add(1)
go func() {
defer backgroundProcesses.Done()
reportCtx, reportCancel := context.WithTimeout(globalCtx, 3*time.Second)
defer reportCancel()
logger.Debug("Sending usage report...")
reportDone <- reportUsage(reportCtx, execScheduler)
close(reportDone)
backgroundProcesses.Done()
}()
go func() {
select {
case <-reportDone:
case <-time.After(3 * time.Second):
if rerr := reportUsage(reportCtx, execScheduler); rerr != nil {
logger.WithError(rerr).Debug("Error sending usage report")
} else {
logger.Debug("Usage report sent successfully")
}
reportCancel()
backgroundProcesses.Done()
}()
}

Expand Down

0 comments on commit 15a2e28

Please sign in to comment.