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

mendersoftware / mender-connect / 1210990983

29 Feb 2024 10:56AM UTC coverage: 64.671% (-9.7%) from 74.341%
1210990983

push

gitlab-ci

web-flow
Merge pull request #118 from danielskinstad/backoff

feat: added back-off in connectionmanager

38 of 60 new or added lines in 3 files covered. (63.33%)

5 existing lines in 2 files now uncovered.

551 of 852 relevant lines covered (64.67%)

25.45 hits per line

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

77.5
/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
        exceededMax bool
30
        // The maximum interval before it starts increasing linearly
31
        maxInterval time.Duration
32
        // Retry time does not increase beyond maxBackoff
33
        maxBackoff time.Duration
34
        // Smallest backoff sleep time
35
        smallestUnit time.Duration
36
        interval     time.Duration
37
}
38

39
// Simple algorithm: Start with one minute, and try three times, then double
40
// interval (maxInterval is maximum) and try again. Repeat until we tried
41
// three times with maxInterval.
42
func (a *expBackoff) GetExponentialBackoffTime() time.Duration {
22✔
43
        var nextInterval time.Duration
22✔
44
        const perIntervalAttempts = 3
22✔
45

22✔
46
        interval := 1 * a.smallestUnit
22✔
47
        nextInterval = interval << (a.attempts / perIntervalAttempts)
22✔
48
        // Generates a random interval between 0 and 2 minutes
22✔
49
        randomInterval := time.Duration(rand.Float64()*float64(time.Minute)) * 2
22✔
50
        if nextInterval > a.maxBackoff {
32✔
51
                a.attempts--
10✔
52
                return a.maxBackoff + randomInterval
10✔
53
        }
10✔
54

55
        if nextInterval > a.maxInterval && !a.exceededMax {
13✔
56
                a.exceededMax = true
1✔
57
                a.attempts = 0
1✔
58
        }
1✔
59

60
        if a.exceededMax {
13✔
61
                return a.maxInterval + (time.Duration(a.attempts) * time.Minute) + randomInterval
1✔
62
        }
1✔
63
        return nextInterval
11✔
64
}
65

66
func (a *expBackoff) WaitForBackoff(ctx context.Context, proto ws.ProtoType) error {
2✔
67
        a.attempts++
2✔
68
        a.interval = a.GetExponentialBackoffTime()
2✔
69

2✔
70
        if a.attempts <= 1 {
4✔
71
                return nil
2✔
72
        }
2✔
73

NEW
74
        log.Infof("connectionmanager backoff: retrying in %s", a.interval)
×
NEW
75

×
NEW
76
        select {
×
NEW
77
        case <-time.After(a.interval):
×
NEW
78
        case <-cancelReconnectChan[proto]:
×
NEW
79
                return context.Canceled
×
NEW
80
        case <-ctx.Done():
×
NEW
81
                return ctx.Err()
×
82
        }
NEW
83
        return nil
×
84
}
85

86
func (a *expBackoff) resetBackoff() {
3✔
87
        a.attempts = 0
3✔
88
        a.exceededMax = false
3✔
89
}
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