Skip to content

Commit c70ea02

Browse files
committed
Replace two more backoff implementations with common one
1 parent f83e3ba commit c70ea02

File tree

2 files changed

+14
-18
lines changed

2 files changed

+14
-18
lines changed

pkg/alertmanager/multitenant.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@ import (
2828
"github.com/weaveworks/cortex/pkg/util"
2929
)
3030

31-
const (
31+
var backoffConfig = util.BackoffConfig{
3232
// Backoff for loading initial configuration set.
33-
minBackoff = 100 * time.Millisecond
34-
maxBackoff = 2 * time.Second
33+
MinBackoff: 100 * time.Millisecond,
34+
MaxBackoff: 2 * time.Second,
35+
}
3536

37+
const (
3638
// If a config sets the Slack URL to this, it will be rewritten to
3739
// a URL derived from Config.AutoSlackRoot
3840
autoSlackURL = "internal://monitor"
@@ -331,19 +333,15 @@ func (am *MultitenantAlertmanager) Stop() {
331333
// Load the full set of configurations from the server, retrying with backoff
332334
// until we can get them.
333335
func (am *MultitenantAlertmanager) loadAllConfigs() map[string]configs.View {
334-
backoff := minBackoff
336+
backoff := util.NewBackoff(backoffConfig, nil)
335337
for {
336338
cfgs, err := am.poll()
337339
if err == nil {
338340
level.Debug(util.Logger).Log("msg", "MultitenantAlertmanager: initial configuration load", "num_configs", len(cfgs))
339341
return cfgs
340342
}
341343
level.Warn(util.Logger).Log("msg", "MultitenantAlertmanager: error fetching all configurations, backing off", "err", err)
342-
time.Sleep(backoff)
343-
backoff *= 2
344-
if backoff > maxBackoff {
345-
backoff = maxBackoff
346-
}
344+
backoff.Wait()
347345
}
348346
}
349347

pkg/ruler/scheduler.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ import (
1818
"github.com/weaveworks/cortex/pkg/util"
1919
)
2020

21-
const (
21+
var backoffConfig = util.BackoffConfig{
2222
// Backoff for loading initial configuration set.
23-
minBackoff = 100 * time.Millisecond
24-
maxBackoff = 2 * time.Second
23+
MinBackoff: 100 * time.Millisecond,
24+
MaxBackoff: 2 * time.Second,
25+
}
2526

27+
const (
2628
timeLogFormat = "2006-01-02T15:04:05"
2729
)
2830

@@ -136,19 +138,15 @@ func (s *scheduler) Stop() {
136138
// Load the full set of configurations from the server, retrying with backoff
137139
// until we can get them.
138140
func (s *scheduler) loadAllConfigs() map[string]configs.View {
139-
backoff := minBackoff
141+
backoff := util.NewBackoff(backoffConfig, nil)
140142
for {
141143
cfgs, err := s.poll()
142144
if err == nil {
143145
level.Debug(util.Logger).Log("msg", "scheduler: initial configuration load", "num_configs", len(cfgs))
144146
return cfgs
145147
}
146148
level.Warn(util.Logger).Log("msg", "scheduler: error fetching all configurations, backing off", "err", err)
147-
time.Sleep(backoff)
148-
backoff *= 2
149-
if backoff > maxBackoff {
150-
backoff = maxBackoff
151-
}
149+
backoff.Wait()
152150
}
153151
}
154152

0 commit comments

Comments
 (0)