From ef4d73324374f5dd75f83c92c5732fa188828ff7 Mon Sep 17 00:00:00 2001 From: yuu Date: Tue, 28 May 2024 16:51:15 +0700 Subject: [PATCH 1/2] chore: retry processor --- backend/monitor_processor.go | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/backend/monitor_processor.go b/backend/monitor_processor.go index d27e48c..ea28378 100644 --- a/backend/monitor_processor.go +++ b/backend/monitor_processor.go @@ -2,6 +2,7 @@ package main import ( "context" + "time" "github.com/rs/zerolog/log" ) @@ -23,16 +24,30 @@ func (m *Processor) ProcessResponse(response Response) { uniqueId = uniqueId[:255] } - // TODO: Retry write if it fails - // Write the response to the historical writer - err := m.historicalWriter.Write(context.Background(), MonitorHistorical{ - MonitorID: uniqueId, - Status: status, - Latency: response.RequestDuration, - Timestamp: response.Timestamp, - }) - if err != nil { - log.Error().Err(err).Msg("failed to write historical data") + maxAttemps := 3 + attempts := 0 + for { + attempts++ + err := m.historicalWriter.Write(context.Background(), MonitorHistorical{ + MonitorID: uniqueId, + Status: status, + Latency: response.RequestDuration, + Timestamp: response.Timestamp, + }) + if err != nil { + if attempts >= maxAttemps { + log.Error().Err(err).Msgf("failed to write historical data. Attempt %d failed\n", attempts) + return + } + + delay := time.Duration(attempts*2) * time.Second + log.Error().Err(err).Msgf("failed to write historical data. Attempt %d failed. Retrying in %v...\n", attempts, delay) + + time.Sleep(delay) + continue + } + + break } // TODO: If the current status is different from the last status, send an alert notification From 32ce9dd929827b13aab3d369d2991e89a5313ce6 Mon Sep 17 00:00:00 2001 From: yuu Date: Fri, 13 Sep 2024 18:46:37 +0700 Subject: [PATCH 2/2] fix: retry processor --- backend/monitor_processor.go | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/backend/monitor_processor.go b/backend/monitor_processor.go index ea28378..5884a3d 100644 --- a/backend/monitor_processor.go +++ b/backend/monitor_processor.go @@ -2,6 +2,7 @@ package main import ( "context" + "math" "time" "github.com/rs/zerolog/log" @@ -24,10 +25,9 @@ func (m *Processor) ProcessResponse(response Response) { uniqueId = uniqueId[:255] } - maxAttemps := 3 - attempts := 0 - for { - attempts++ + attemptRemaining := 3 + attemptedEntries := 0 + for attemptRemaining > 0 { err := m.historicalWriter.Write(context.Background(), MonitorHistorical{ MonitorID: uniqueId, Status: status, @@ -35,15 +35,18 @@ func (m *Processor) ProcessResponse(response Response) { Timestamp: response.Timestamp, }) if err != nil { - if attempts >= maxAttemps { - log.Error().Err(err).Msgf("failed to write historical data. Attempt %d failed\n", attempts) + attemptedEntries++ + if attemptRemaining == 0 { + log.Error().Err(err).Msgf("failed to write historical data. Attempt %d failed\n", attemptedEntries) return } - delay := time.Duration(attempts*2) * time.Second - log.Error().Err(err).Msgf("failed to write historical data. Attempt %d failed. Retrying in %v...\n", attempts, delay) + delay := time.Second * time.Duration(math.Pow(2, math.Abs(float64(attemptedEntries)))) + log.Error().Err(err).Msgf("failed to write historical data. Attempt %d failed. Retrying in %v...\n", attemptedEntries, delay) time.Sleep(delay) + + attemptRemaining -= 1 continue }