~untrusted-ci-dev-bot/ubuntu-push/ubuntu-push-ubuntu-xenial-1169

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
 Copyright 2013-2014 Canonical Ltd.

 This program is free software: you can redistribute it and/or modify it
 under the terms of the GNU General Public License version 3, as published
 by the Free Software Foundation.

 This program is distributed in the hope that it will be useful, but
 WITHOUT ANY WARRANTY; without even the implied warranties of
 MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 PURPOSE.  See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along
 with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

// Package util contains the redialer.
package util

import (
	"sync"
	"time"
)

// A Dialer is an object that knows how to establish a connection, and
// where you'd usually want some kind of backoff if that connection
// fails.
type Dialer interface {
	Dial() error
}

// A Jitterer is a Dialer that wants to vary the backoff a little (to avoid a
// thundering herd, for example).
type Jitterer interface {
	Dialer
	Jitter(time.Duration) time.Duration
}

// The timeouts used during backoff.
var timeouts []time.Duration
var trwlock sync.RWMutex

// Retrieve the list of timeouts used for exponential backoff.
func Timeouts() []time.Duration {
	trwlock.RLock()
	defer trwlock.RUnlock()
	return timeouts
}

// For testing: change the default timeouts with the provided ones,
// returning the defaults (the idea being you reset them on test
// teardown).
func SwapTimeouts(newTimeouts []time.Duration) (oldTimeouts []time.Duration) {
	trwlock.Lock()
	defer trwlock.Unlock()
	oldTimeouts, timeouts = timeouts, newTimeouts
	return
}

// An AutoRedialer's Redial() method retries its dialer's Dial() method until
// it stops returning an error. It does exponential backoff (optionally
// jittered).
type AutoRedialer interface {
	Redial() uint32 // Redial keeps on calling Dial until it stops returning an error.
	Stop()          // Stop shuts down the given AutoRedialer, if it is still retrying.
}

type redialerState uint32

const (
	Unconfigured redialerState = iota
	Redialing
	Stopped
)

func (s *redialerState) String() string {
	return [3]string{"Unconfigured", "Redialing", "Stopped"}[uint32(*s)]
}

type autoRedialer struct {
	stateLock     sync.RWMutex
	stateValue    redialerState
	stopping      chan struct{}
	reallyStopped chan struct{}
	dial          func() error
	jitter        func(time.Duration) time.Duration
}

func (ar *autoRedialer) state() redialerState {
	ar.stateLock.RLock()
	defer ar.stateLock.RUnlock()
	return ar.stateValue
}

func (ar *autoRedialer) setState(s redialerState) {
	ar.stateLock.Lock()
	defer ar.stateLock.Unlock()
	ar.stateValue = s
}

func (ar *autoRedialer) setStateIfEqual(oldState, newState redialerState) bool {
	ar.stateLock.Lock()
	defer ar.stateLock.Unlock()
	if ar.stateValue != oldState {
		return false
	}
	ar.stateValue = newState
	return true
}

func (ar *autoRedialer) setStateStopped() {
	ar.stateLock.Lock()
	defer ar.stateLock.Unlock()
	switch ar.stateValue {
	case Stopped:
		return
	case Unconfigured:
		close(ar.reallyStopped)
	}
	ar.stateValue = Stopped
	close(ar.stopping)
}

func (ar *autoRedialer) Stop() {
	if ar != nil {
		ar.setStateStopped()
		<-ar.reallyStopped
	}
}

// Redial keeps on calling Dial until it stops returning an error.  It does
// exponential backoff, adding back the output of Jitter at each step.
func (ar *autoRedialer) Redial() uint32 {
	if ar == nil {
		// at least it's better than a segfault...
		panic("you can't Redial a nil AutoRedialer")
	}
	if !ar.setStateIfEqual(Unconfigured, Redialing) {
		// XXX log this
		return 0
	}
	defer close(ar.reallyStopped)

	var timeout time.Duration
	var dialAttempts uint32 = 0 // unsigned so it can wrap safely ...
	timeouts := Timeouts()
	var numTimeouts uint32 = uint32(len(timeouts))
	for {
		if ar.state() != Redialing {
			return dialAttempts
		}
		if ar.dial() == nil {
			return dialAttempts + 1
		}
		if dialAttempts < numTimeouts {
			timeout = timeouts[dialAttempts]
		} else {
			timeout = timeouts[numTimeouts-1]
		}
		if ar.jitter != nil {
			timeout += ar.jitter(timeout)
		}
		dialAttempts++
		select {
		case <-ar.stopping:
		case <-time.After(timeout):
		}
	}
}

// Returns a stoppable AutoRedialer using the provided Dialer. If the Dialer
// is also a Jitterer, the backoff will be jittered.
func NewAutoRedialer(dialer Dialer) AutoRedialer {
	ar := &autoRedialer{
		stateValue:    Unconfigured,
		dial:          dialer.Dial,
		reallyStopped: make(chan struct{}),
		stopping:      make(chan struct{}),
	}
	jitterer, ok := dialer.(Jitterer)
	if ok {
		ar.jitter = jitterer.Jitter
	}
	return ar
}

func init() {
	ps := []int{1, 2, 5, 11, 19, 37, 67, 113, 191} // 3 pₙ₊₁ ≥ 5 pₙ
	timeouts := make([]time.Duration, len(ps))
	for i, n := range ps {
		timeouts[i] = time.Duration(n) * time.Second
	}
	SwapTimeouts(timeouts)
}