Skip to content

Commit

Permalink
refactor(torch): split in priv funcs
Browse files Browse the repository at this point in the history
Signed-off-by: Jose Ramon Mañes <[email protected]>
  • Loading branch information
tty47 committed Nov 13, 2023
1 parent ee8d71c commit 78bbf75
Showing 1 changed file with 46 additions and 32 deletions.
78 changes: 46 additions & 32 deletions pkg/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...")
Expand Down

0 comments on commit 78bbf75

Please sign in to comment.