From c81477f2bf9240fa13cbff65720f09d70ad4d2d3 Mon Sep 17 00:00:00 2001 From: Shaunak Kashyap Date: Thu, 12 Oct 2023 05:47:51 -0700 Subject: [PATCH] More optimizations --- .../download/http/progress_reporter.go | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/internal/pkg/agent/application/upgrade/artifact/download/http/progress_reporter.go b/internal/pkg/agent/application/upgrade/artifact/download/http/progress_reporter.go index 9834b3488ae..f50168c88ca 100644 --- a/internal/pkg/agent/application/upgrade/artifact/download/http/progress_reporter.go +++ b/internal/pkg/agent/application/upgrade/artifact/download/http/progress_reporter.go @@ -89,6 +89,13 @@ func (dp *downloadProgressReporter) Report() { // either ReportComplete or ReportFailed when they no longer need the downloadProgressReporter // to avoid resource leaks. func (dp *downloadProgressReporter) ReportComplete() { + defer dp.close() + + // If there are no observers to report progress to, there is nothing to do! + if len(dp.progressObservers) == 0 { + return + } + now := time.Now() timePast := now.Sub(dp.started) downloaded := float64(dp.downloaded.Load()) @@ -97,14 +104,19 @@ func (dp *downloadProgressReporter) ReportComplete() { for _, obs := range dp.progressObservers { obs.ReportCompleted(dp.sourceURI, timePast, bytesPerSecond) } - - dp.close() } // ReportFailed reports the failure of a download to registered observers. Callers MUST call // either ReportFailed or ReportComplete when they no longer need the downloadProgressReporter // to avoid resource leaks. func (dp *downloadProgressReporter) ReportFailed(err error) { + defer dp.close() + + // If there are no observers to report progress to, there is nothing to do! + if len(dp.progressObservers) == 0 { + return + } + now := time.Now() timePast := now.Sub(dp.started) downloaded := float64(dp.downloaded.Load()) @@ -117,8 +129,6 @@ func (dp *downloadProgressReporter) ReportFailed(err error) { for _, obs := range dp.progressObservers { obs.ReportFailed(dp.sourceURI, timePast, downloaded, dp.length, percentComplete, bytesPerSecond, err) } - - dp.close() } func (dp *downloadProgressReporter) close() {