@@ -35,6 +35,13 @@ import (
3535 "github.com/prometheus/client_golang/exp/internal/github.com/efficientgo/core/backoff"
3636)
3737
38+ // BackoffConfig configures exponential backoff with jitter for retry operations.
39+ type BackoffConfig struct {
40+ Min time.Duration `yaml:"min_period"` // Start backoff at this level
41+ Max time.Duration `yaml:"max_period"` // Increase exponentially to this level
42+ MaxRetries int `yaml:"max_retries"` // Give up after this many; zero means infinite retries
43+ }
44+
3845// API is a client for Prometheus Remote Protocols.
3946// NOTE(bwplotka): Only https://prometheus.io/docs/specs/remote_write_spec_2_0/ is currently implemented,
4047// read protocols to be implemented if there will be a demand.
@@ -56,14 +63,14 @@ type RetryCallback func(err error)
5663type apiOpts struct {
5764 logger * slog.Logger
5865 client * http.Client
59- backoff backoff. Config
66+ backoffConfig BackoffConfig
6067 compression Compression
6168 path string
6269 retryOnRateLimit bool
6370}
6471
6572var defaultAPIOpts = & apiOpts {
66- backoff : backoff. Config {
73+ backoffConfig : BackoffConfig {
6774 Min : 1 * time .Second ,
6875 Max : 10 * time .Second ,
6976 MaxRetries : 10 ,
@@ -107,10 +114,11 @@ func WithAPINoRetryOnRateLimit() APIOption {
107114 }
108115}
109116
110- // WithAPIBackoff returns APIOption that allows overriding backoff configuration.
111- func WithAPIBackoff (backoff backoff.Config ) APIOption {
117+ // WithAPIBackoff returns APIOption that allows configuring backoff.
118+ // By default, exponential backoff with jitter is used (see defaultAPIOpts).
119+ func WithAPIBackoff (cfg BackoffConfig ) APIOption {
112120 return func (o * apiOpts ) error {
113- o .backoff = backoff
121+ o .backoffConfig = cfg
114122 return nil
115123 }
116124}
@@ -259,7 +267,11 @@ func (r *API) Write(ctx context.Context, msgType WriteMessageType, msg any, opts
259267 // across the various attempts.
260268 accumulatedStats := WriteResponseStats {}
261269
262- b := backoff .New (ctx , r .opts .backoff )
270+ b := backoff .New (ctx , backoff.Config {
271+ Min : r .opts .backoffConfig .Min ,
272+ Max : r .opts .backoffConfig .Max ,
273+ MaxRetries : r .opts .backoffConfig .MaxRetries ,
274+ })
263275 for {
264276 rs , err := r .attemptWrite (ctx , r .opts .compression , msgType , payload , b .NumRetries ())
265277 accumulatedStats .Add (rs )
0 commit comments