From 78bbf75eec49ecbd86e56449c9c76e7b5ab5970a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jose=20Ramon=20Ma=C3=B1es?= Date: Mon, 13 Nov 2023 13:10:37 +0100 Subject: [PATCH] refactor(torch): split in priv funcs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jose Ramon MaƱes --- pkg/http/server.go | 78 +++++++++++++++++++++++++++------------------- 1 file changed, 46 insertions(+), 32 deletions(-) diff --git a/pkg/http/server.go b/pkg/http/server.go index fe89925..50840d4 100644 --- a/pkg/http/server.go +++ b/pkg/http/server.go @@ -164,44 +164,58 @@ func WatchHashMetric(cfg config.MutualPeersConfig, ctx context.Context) error { // Run the WatchHashMetric function in a separate goroutine eg.Go(func() error { - // Continue generating metrics with retries - for { - select { - case <-ctx.Done(): - // Context canceled, stop the process - log.Info("Context canceled, stopping WatchHashMetric.") - return ctx.Err() - default: - err := GenerateHashMetrics(cfg) - // Check if err is nil, if so, Torch was able to generate the metric. - if err == nil { - log.Info("Metric generated for the first block, let's stop the process successfully...") - // The metric was successfully generated, stop the retries. - return nil - } - - // Log the error - log.Error("Error generating hash metrics: ", err) - - // Wait for the retry interval before the next execution using a timer - timer := time.NewTimer(retryInterval) - select { - case <-ctx.Done(): - // Context canceled, stop the process - log.Info("Context canceled, stopping WatchHashMetric.") - timer.Stop() - return ctx.Err() - case <-timer.C: - // Continue to the next iteration - } - } - } + return watchMetricsWithRetry(cfg, ctx) }) // Wait for all goroutines to finish return eg.Wait() } +// watchMetricsWithRetry is a helper function for WatchHashMetric that encapsulates the retry logic. +func watchMetricsWithRetry(cfg config.MutualPeersConfig, ctx context.Context) error { + // Continue generating metrics with retries + for { + select { + case <-ctx.Done(): + // Context canceled, stop the process + log.Info("Context canceled, stopping WatchHashMetric.") + return ctx.Err() + default: + err := GenerateHashMetrics(cfg) + // Check if err is nil, if so, Torch was able to generate the metric. + if err == nil { + log.Info("Metric generated for the first block, let's stop the process successfully...") + // The metric was successfully generated, stop the retries. + return nil + } + + // Log the error + log.Error("Error generating hash metrics: ", err) + + // Wait for the retry interval before the next execution using a timer + if err := waitForRetry(ctx); err != nil { + return err + } + } + } +} + +// waitForRetry is a helper function to wait for the retry interval or stop if the context is canceled. +func waitForRetry(ctx context.Context) error { + timer := time.NewTimer(retryInterval) + defer timer.Stop() + + select { + case <-ctx.Done(): + // Context canceled, stop the process + log.Info("Context canceled, stopping WatchHashMetric.") + return ctx.Err() + case <-timer.C: + // Continue to the next iteration + return nil + } +} + // GenerateHashMetrics generates the metric by getting the first block and calculating the days. func GenerateHashMetrics(cfg config.MutualPeersConfig) error { log.Info("Trying to generate the metric for the first block generated...")