Skip to content

Commit

Permalink
add exponential backoff when doing updates
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-iohk committed Mar 11, 2024
1 parent 734a8e9 commit 1d31f50
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/cassandra.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func (kc *CassandraContext) selectRange(startTime, endTime time.Time) ([]Submiss
return submissions, nil
}

func (kc *CassandraContext) updateSubmissions(submissions []Submission) error {
func (kc *CassandraContext) tryUpdateSubmissions(submissions []Submission) error {
// Define your dummy values here
dummyStateHash := "dummy_state_hash"
dummyParent := "dummy_parent"
Expand All @@ -182,6 +182,16 @@ func (kc *CassandraContext) updateSubmissions(submissions []Submission) error {
return nil
}

func (kc *CassandraContext) updateSubmissions(submissions []Submission) error {
return ExponentialBackoff(func() error {
if err := kc.tryUpdateSubmissions(submissions); err != nil {
kc.Log.Errorf("Error updating submissions (trying again): %v", err)
return err
}
return nil
}, maxRetries, initialBackoff)
}

// func (kc *CassandraContext) updateSubmissionsBatch(submissions []Submission) error {
// batch := kc.Session.NewBatch(gocql.LoggedBatch) // Use gocql.UnloggedBatch for unlogged batches

Expand Down
34 changes: 34 additions & 0 deletions src/operation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
"fmt"
"time"
)

// Operation is a function type that represents an operation that might fail and need a retry.
type Operation func() error

const (
maxRetries = 5
initialBackoff = 300 * time.Millisecond
)

// ExponentialBackoff retries the provided operation with an exponential backoff strategy.
func ExponentialBackoff(operation Operation, maxRetries int, initialBackoff time.Duration) error {
backoff := initialBackoff
var err error
for i := 0; i < maxRetries; i++ {
err = operation()
if err == nil {
return nil // Success
}

if i < maxRetries-1 {
// If not the last retry, wait for a bit
time.Sleep(backoff)
backoff *= 2 // Exponential increase
}
}

return fmt.Errorf("operation failed after %d retries, returned error: %s", maxRetries, err)
}

0 comments on commit 1d31f50

Please sign in to comment.