• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / mender-connect / 1902674345

02 Jul 2025 02:54PM UTC coverage: 70.127% (+0.3%) from 69.848%
1902674345

Pull #157

gitlab-ci

danielskinstad
feat: lower backoff intervals

The previous backoff had a starting interval at 1
minute and a max backoff at 60 minutes. When it reached 60 minutes it
would linearly increase to 120 minutes minutes where it would keep
retrying.

The backoff will now start at 1 second and exponentially increase after
3 retries until it reaches 30 minutes where it will keep retrying.
For each interval a jitter between 0 and 5 seconds will be added.

Ticket: ME-546
Changelog: Commit

Signed-off-by: Daniel Skinstad Drabitzius <daniel.drabitzius@northern.tech>
Pull Request #157: WIP: feat: lower backoff intervals

6 of 7 new or added lines in 1 file covered. (85.71%)

1 existing line in 1 file now uncovered.

2486 of 3545 relevant lines covered (70.13%)

6.1 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

72.73
/connectionmanager/exponentialbackoff.go
1
// Copyright 2024 Northern.tech AS
2
//
3
//        Licensed under the Apache License, Version 2.0 (the "License");
4
//        you may not use this file except in compliance with the License.
5
//        You may obtain a copy of the License at
6
//
7
//            http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//        Unless required by applicable law or agreed to in writing, software
10
//        distributed under the License is distributed on an "AS IS" BASIS,
11
//        WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//        See the License for the specific language governing permissions and
13
//        limitations under the License.
14

15
package connectionmanager
16

17
import (
18
        "math/rand"
19
        "time"
20

21
        "github.com/mendersoftware/go-lib-micro/ws"
22
        log "github.com/sirupsen/logrus"
23

24
        "context"
25
)
26

27
type expBackoff struct {
28
        attempts int
29
        // Retry time does not increase beyond maxBackoff
30
        maxBackoff time.Duration
31
        // Smallest backoff sleep time
32
        smallestUnit time.Duration
33
        interval     time.Duration
34

35
        randomInterval time.Duration
36
}
37

38
// Simple algorithm: Start with `smallestUnit`, and try three times, then double
39
// `interval` until `maxBackoff` is reached and keep retrying with the maxBackoff
40
// interval
41
func (a *expBackoff) GetExponentialBackoffTime() time.Duration {
21✔
42
        var nextInterval time.Duration
21✔
43
        const perIntervalAttempts = 3
21✔
44

21✔
45
        interval := 1 * a.smallestUnit
21✔
46
        nextInterval = interval << (a.attempts / perIntervalAttempts)
21✔
47

21✔
48
        if nextInterval > a.maxBackoff {
31✔
49
                return a.maxBackoff + a.randomInterval
10✔
50
        }
10✔
51
        return nextInterval + a.randomInterval
11✔
52
}
53

54
func (a *expBackoff) WaitForBackoff(ctx context.Context, proto ws.ProtoType) error {
2✔
55
        a.attempts++
2✔
56

2✔
57
        // Random interval between 0 and 5 a.smallestUnit (e.g. seconds)
2✔
58
        a.randomInterval = time.Duration(rand.Float64()*float64(a.smallestUnit)) * 5
2✔
59
        a.interval = a.GetExponentialBackoffTime()
2✔
60

2✔
61
        if a.attempts <= 1 {
4✔
62
                return nil
2✔
63
        }
2✔
64

NEW
65
        log.Infof("connectionmanager backoff: retrying in %s", a.interval.Round(time.Second))
×
66

×
67
        select {
×
68
        case <-time.After(a.interval):
×
69
        case <-cancelReconnectChan[proto]:
×
70
                return context.Canceled
×
71
        case <-ctx.Done():
×
72
                return ctx.Err()
×
73
        }
74
        return nil
×
75
}
76

77
func (a *expBackoff) resetBackoff() {
3✔
78
        a.attempts = 0
3✔
79
}
3✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc