Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use exponential backoff for retries #1433

Merged
merged 1 commit into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions dkron/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package dkron

import (
"fmt"
"math"
"strconv"
"time"

proto "github.com/distribworks/dkron/v3/plugin/types"
"google.golang.org/protobuf/types/known/timestamppb"
)

const defaultRetryInterval = 500 * time.Millisecond

// Execution type holds all of the details of a specific Execution.
type Execution struct {
// Id is the Key for this execution
Expand Down Expand Up @@ -91,3 +94,13 @@ func (e *Execution) Key() string {
func (e *Execution) GetGroup() string {
return strconv.FormatInt(e.Group, 10)
}

func (e *Execution) CalculateExponentialBackoff() time.Duration {
now := time.Now()
if now.Before(e.StartedAt) {
return 0
}
diff := now.Sub(e.StartedAt)
backoff := math.Log2(float64(diff/defaultRetryInterval)) + float64(e.Attempt)
return time.Duration(backoff) * defaultRetryInterval
}
5 changes: 5 additions & 0 deletions dkron/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,14 +217,19 @@ func (grpcs *GRPCServer) ExecutionDone(ctx context.Context, execDoneReq *proto.E
// Keep all execution properties intact except the last output
execution.Output = ""

eb := execution.CalculateExponentialBackoff()
grpcs.logger.WithFields(logrus.Fields{
"attempt": execution.Attempt,
"execution": execution,
"backoff": eb,
}).Debug("grpc: Retrying execution")

time.Sleep(eb)

if _, err := grpcs.agent.Run(job.Name, execution); err != nil {
return nil, err
}

return &proto.ExecutionDoneResponse{
From: grpcs.agent.config.NodeName,
Payload: []byte("retry"),
Expand Down
Loading