-
Notifications
You must be signed in to change notification settings - Fork 36
/
poller.go
225 lines (188 loc) · 5.92 KB
/
poller.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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package pollers
import (
"context"
"fmt"
"sync"
"time"
"github.com/hashicorp/go-azure-sdk/sdk/client"
)
const DefaultNumberOfDroppedConnectionsToAllow = 3
type Poller struct {
// initialDelayDuration specifies the duration of the initial delay when polling
// this is also used for retries should a `latestResponse` not be available, for
// example when a connection is dropped.
initialDelayDuration time.Duration
// latestError contains the error returned from the latest poll.
latestError error
// latestResponse contains the polling status from the latest response.
latestResponse *PollResult
// maxNumberOfDroppedConnections specifies the maximum number of sequential dropped connections before an error is raised.
maxNumberOfDroppedConnections int
// poller is a reference to the PollerType, for example a LongRunningOperationPoller
// which should be polled to determine the latest state.
poller PollerType
}
func NewPoller(pollerType PollerType, initialDelayDuration time.Duration, maxNumberOfDroppedConnections int) Poller {
return Poller{
initialDelayDuration: initialDelayDuration,
maxNumberOfDroppedConnections: maxNumberOfDroppedConnections,
poller: pollerType,
}
}
// LatestResponse returns the latest HTTP Response returned when polling
func (p *Poller) LatestResponse() *client.Response {
if p.latestError != nil {
if v, ok := p.latestError.(PollingCancelledError); ok {
return v.HttpResponse
}
if _, ok := p.latestError.(PollingDroppedConnectionError); ok {
return nil
}
if v, ok := p.latestError.(PollingFailedError); ok {
return v.HttpResponse
}
if p.latestError == context.DeadlineExceeded {
return nil
}
}
if p.latestResponse == nil {
return nil
}
return p.latestResponse.HttpResponse
}
// LatestStatus returns the latest status returned when polling
func (p *Poller) LatestStatus() PollingStatus {
if p.latestError != nil {
if _, ok := p.latestError.(PollingCancelledError); ok {
return PollingStatusCancelled
}
if _, ok := p.latestError.(PollingDroppedConnectionError); ok {
// we could look to expose a status for this, but we likely wouldn't handle this any differently
// to it being unknown, so I (@tombuildsstuff) think this is reasonable for now?
return PollingStatusUnknown
}
if _, ok := p.latestError.(PollingFailedError); ok {
return PollingStatusFailed
}
if p.latestError == context.DeadlineExceeded {
return PollingStatusUnknown
}
}
if p.latestResponse == nil {
return PollingStatusUnknown
}
return p.latestResponse.Status
}
// PollUntilDone polls until the poller determines that the operation has been completed
func (p *Poller) PollUntilDone(ctx context.Context) error {
if p.poller == nil {
return fmt.Errorf("internal-error: `poller` was nil`")
}
if _, ok := ctx.Deadline(); !ok {
return fmt.Errorf("internal-error: `ctx` should have a deadline")
}
var wait sync.WaitGroup
wait.Add(1)
go func() {
connectionDropCounter := 0
retryDuration := p.initialDelayDuration
for {
// determine the next retry duration / how long to poll for
if p.latestResponse != nil {
retryDuration = p.latestResponse.PollInterval
}
endTime := time.Now().Add(retryDuration)
select {
case <-time.After(time.Until(endTime)):
{
break
}
}
p.latestResponse, p.latestError = p.poller.Poll(ctx)
// first check the connection drop status
connectionHasBeenDropped := false
if p.latestResponse == nil && p.latestError == nil {
// connection drops can either have no response/error (where we have no context)
connectionHasBeenDropped = true
} else if _, ok := p.latestError.(PollingDroppedConnectionError); ok {
// or have an error with more details (e.g. server not found, connection reset etc.)
connectionHasBeenDropped = true
}
if connectionHasBeenDropped {
connectionDropCounter++
if connectionDropCounter < p.maxNumberOfDroppedConnections {
continue
}
if p.latestResponse == nil && p.latestError == nil {
// the connection was dropped, but we have no context
p.latestError = PollingDroppedConnectionError{}
break
}
} else {
connectionDropCounter = 0
}
if p.latestError != nil {
break
}
if response := p.latestResponse; response != nil {
retryDuration = response.PollInterval
done := false
switch response.Status {
// Cancelled, Dropped Connections and Failed should be raised as errors containing additional info if available
case PollingStatusCancelled:
p.latestError = fmt.Errorf("internal-error: a polling status of `Cancelled` should be surfaced as a PollingCancelledError")
done = true
break
case PollingStatusFailed:
p.latestError = fmt.Errorf("internal-error: a polling status of `Failed` should be surfaced as a PollingFailedError")
done = true
break
case PollingStatusInProgress:
continue
case PollingStatusSucceeded:
done = true
break
default:
p.latestError = fmt.Errorf("internal-error: unimplemented polling status %q", string(response.Status))
done = true
break
}
if done {
break
}
}
}
wait.Done()
}()
waitDone := make(chan struct{}, 1)
go func() {
wait.Wait()
waitDone <- struct{}{}
}()
select {
case <-waitDone:
break
case <-ctx.Done():
{
p.latestResponse = nil
p.latestError = ctx.Err()
return p.latestError
}
}
if p.latestError != nil {
p.latestResponse = nil
}
return p.latestError
}
// FinalResult attempts to unmarshal the final result into the provided model
// model should be a pointer to the type you wish to unmarshal into
func (p *Poller) FinalResult(model interface{}) error {
if latestResponse := p.LatestResponse(); latestResponse != nil {
if err := latestResponse.Unmarshal(model); err != nil {
return fmt.Errorf("unmarshalling latest response: %+v", err)
}
}
return nil
}