Skip to main content

Backoff Strategies

When a request fails it may be desirable to retry the request in the future. For this the SDKs employ a couple of strategies to give the best chance of a successful request.

Constant Backoff

A constant backoff applies a fixed period of time between each failed request.

Constant Backoff
package main

import (
"time"

"github.com/StatusCakeDev/statuscake-go"
"github.com/StatusCakeDev/statuscake-go/backoff"
"github.com/StatusCakeDev/statuscake-go/credentials"
)

func main() {
bearer := credentials.NewBearerWithStaticToken("my-api-token")
client := statuscake.NewClient(
statuscake.WithRequestCredentials(bearer),
statuscake.WithBackoff(backoff.Constant{
BaseDelay: time.Duration(5) * time.Second,
}),
)
}

Exponential Backoff

An exponential backoff applies an exponential function on the time between each failed request.

Exponential Backoff
package main

import (
"time"

"github.com/StatusCakeDev/statuscake-go"
"github.com/StatusCakeDev/statuscake-go/backoff"
"github.com/StatusCakeDev/statuscake-go/credentials"
)

func main() {
bearer := credentials.NewBearerWithStaticToken("my-api-token")
client := statuscake.NewClient(
statuscake.WithRequestCredentials(bearer),
statuscake.WithBackoff(backoff.Exponential{
BaseDelay: time.Duration(1) * time.Second,
Multiplier: 2.0,
Jitter: 0.2,
MaxDelay: time.Duration(30) * time.Second,
}),
)
}

Linear Backoff

Linear backoff applies a linear functions on the time between each failed request.

Linear Backoff
package main

import (
"time"

"github.com/StatusCakeDev/statuscake-go"
"github.com/StatusCakeDev/statuscake-go/backoff"
"github.com/StatusCakeDev/statuscake-go/credentials"
)

func main() {
bearer := credentials.NewBearerWithStaticToken("my-api-token")
client := statuscake.NewClient(
statuscake.WithRequestCredentials(bearer),
statuscake.WithBackoff(backoff.Linear{
BaseDelay: time.Duration(1) * time.Second,
Jitter: 0.2,
MaxDelay: time.Duration(30) * time.Second,
}),
)
}