/
backoff.go
62 lines (58 loc) · 1.74 KB
/
backoff.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
* skogul, backoff sender
*
* Copyright (c) 2019 Telenor Norge AS
* Author(s):
* - Kristian Lyngstøl <kly@kly.no>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
package sender
import (
"github.com/telenornms/skogul"
"sync/atomic"
"time"
)
// Backoff sender will send to Next, but retry up to Retries times, with
// exponential backoff, starting with time.Duration
type Backoff struct {
Next skogul.SenderRef `doc:"The sender to try"`
Base skogul.Duration `doc:"Initial delay after a failure. Will double for each retry"`
Retries uint64 `doc:"Number of retries before giving up"`
holdoff uint64
}
// Send with a delay
func (bo *Backoff) Send(c *skogul.Container) error {
var err error
delay := bo.Base.Duration
t := atomic.LoadUint64(&bo.holdoff)
if t > 0 {
time.Sleep(delay)
}
for i := uint64(1); i <= bo.Retries; i++ {
err = bo.Next.S.Send(c)
if err == nil {
if i > 1 {
atomic.AddUint64(&bo.holdoff, 1-i)
}
return nil
}
atomic.AddUint64(&bo.holdoff, 1)
time.Sleep(delay)
delay = delay * 2
}
return err
}