-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexchange.go
More file actions
297 lines (267 loc) · 7.26 KB
/
Copy pathexchange.go
File metadata and controls
297 lines (267 loc) · 7.26 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package warc
import (
"context"
"errors"
"sync"
http "github.com/saveweb/fhttp"
)
type exchangeContextKey struct{}
type writerOwnerContextKey struct{}
var ErrExchangeAlreadyDecided = errors.New("warc: exchange already has a different decision")
type exchangeDecision uint8
const (
exchangeUndecided exchangeDecision = iota
exchangeCommit
exchangeDiscard
)
// AttemptResult describes the archival outcome of one actual transport attempt.
// A retried logical request can therefore contain more than one result.
type AttemptResult struct {
Protocol string
Outcome http.CaptureOutcome
Records FeedbackEvent
Err error
cleanupErr error
}
// ExchangeResult is complete only after every transport attempt has finished
// and every retained WARC batch has been accepted by a writer.
type ExchangeResult struct {
Attempts []AttemptResult
Records FeedbackEvent
Err error
}
// Exchange separates receiving response headers from the decision to retain
// the capture. Callers consume or close Response.Body, then finish with either
// Commit or Discard.
type Exchange struct {
Response *http.Response
state *exchangeState
}
// Commit retains every transport attempt belonging to the logical request and
// waits until its records have been accepted by a WARC writer. Repeating Commit
// is harmless and returns the same result. Its result includes any error
// already returned by Start for this exchange.
func (e *Exchange) Commit(ctx context.Context) (ExchangeResult, error) {
if err := e.decide(exchangeCommit); err != nil {
return ExchangeResult{}, err
}
return e.wait(ctx)
}
// Discard closes any response body and releases every transport attempt
// without writing HTTP records. Repeating Discard is harmless.
func (e *Exchange) Discard(ctx context.Context) error {
if e == nil || e.state == nil {
return errors.New("warc: nil exchange")
}
if err := e.state.discard(); err != nil {
return err
}
_, err := e.wait(ctx)
return err
}
func (e *Exchange) decide(decision exchangeDecision) error {
if e == nil || e.state == nil {
return errors.New("warc: nil exchange")
}
return e.state.decide(decision)
}
func (e *Exchange) wait(ctx context.Context) (ExchangeResult, error) {
if e == nil || e.state == nil {
return ExchangeResult{}, errors.New("warc: nil exchange")
}
select {
case <-e.state.done:
result := e.state.result()
return result, result.Err
case <-ctx.Done():
return ExchangeResult{}, context.Cause(ctx)
}
}
type exchangeState struct {
client *CustomHTTPClient
feedback chan FeedbackEvent
mu sync.Mutex
active int
networkDone bool
networkErr error
attempts []AttemptResult
decision exchangeDecision
decisionDone chan struct{}
response *http.Response
responseClosed bool
responseCloseErr error
done chan struct{}
decisionOnce sync.Once
completeOnce sync.Once
}
func newExchangeState(client *CustomHTTPClient, feedback chan FeedbackEvent, decision exchangeDecision) *exchangeState {
state := &exchangeState{
client: client,
feedback: feedback,
decisionDone: make(chan struct{}),
done: make(chan struct{}),
}
if decision != exchangeUndecided {
state.decision = decision
close(state.decisionDone)
}
return state
}
func exchangeStateFromContext(ctx context.Context) *exchangeState {
state, _ := ctx.Value(exchangeContextKey{}).(*exchangeState)
return state
}
func (s *exchangeState) beginAttempt() {
s.mu.Lock()
s.active++
s.mu.Unlock()
s.client.WaitGroup.Add(1)
}
func (s *exchangeState) finishAttempt(result AttemptResult) {
s.mu.Lock()
s.attempts = append(s.attempts, result)
s.active--
s.completeLocked()
s.mu.Unlock()
s.client.WaitGroup.Done()
}
func (s *exchangeState) finishNetwork(err error) {
s.mu.Lock()
s.networkDone = true
s.networkErr = err
s.completeLocked()
s.mu.Unlock()
}
func (s *exchangeState) decide(decision exchangeDecision) error {
s.mu.Lock()
defer s.mu.Unlock()
if s.decision != exchangeUndecided {
if s.decision == decision {
return nil
}
return ErrExchangeAlreadyDecided
}
s.decision = decision
s.decisionOnce.Do(func() { close(s.decisionDone) })
s.completeLocked()
return nil
}
func (s *exchangeState) discard() error {
s.mu.Lock()
if s.decision == exchangeCommit {
s.mu.Unlock()
return ErrExchangeAlreadyDecided
}
if s.decision == exchangeUndecided {
s.decision = exchangeDiscard
s.decisionOnce.Do(func() { close(s.decisionDone) })
s.completeLocked()
}
response := s.responseToCloseLocked()
s.mu.Unlock()
s.closeResponse(response)
return nil
}
func (s *exchangeState) closeForShutdown() (networkPending bool) {
s.mu.Lock()
if s.decision == exchangeUndecided {
s.decision = exchangeDiscard
s.decisionOnce.Do(func() { close(s.decisionDone) })
s.completeLocked()
}
response := s.responseToCloseLocked()
networkPending = !s.networkDone
s.mu.Unlock()
s.closeResponse(response)
return networkPending
}
func (s *exchangeState) waitForDecision() exchangeDecision {
<-s.decisionDone
s.mu.Lock()
defer s.mu.Unlock()
return s.decision
}
func (s *exchangeState) attachResponse(response *http.Response) {
s.mu.Lock()
s.response = response
var closeResponse *http.Response
if s.decision == exchangeDiscard {
closeResponse = s.responseToCloseLocked()
}
s.mu.Unlock()
s.closeResponse(closeResponse)
}
func (s *exchangeState) responseToCloseLocked() *http.Response {
if s.response == nil || s.response.Body == nil || s.responseClosed {
return nil
}
s.responseClosed = true
return s.response
}
func (s *exchangeState) closeResponse(response *http.Response) {
if response == nil {
return
}
err := response.Body.Close()
s.mu.Lock()
s.responseCloseErr = errors.Join(s.responseCloseErr, err)
s.mu.Unlock()
}
func (s *exchangeState) completeLocked() {
// Network completion alone is insufficient: retries can leave capture
// serialization and writer acknowledgement running asynchronously.
if s.decision == exchangeUndecided || !s.networkDone || s.active != 0 {
return
}
s.completeOnce.Do(func() {
if s.feedback != nil {
events := append(FeedbackEvent(nil), s.resultLocked().Records...)
if len(events) > 0 {
s.feedback <- events
}
close(s.feedback)
}
close(s.done)
s.client.unregisterExchange(s)
})
}
func (s *exchangeState) result() ExchangeResult {
s.mu.Lock()
defer s.mu.Unlock()
return s.resultLocked()
}
func (s *exchangeState) resultLocked() ExchangeResult {
result := ExchangeResult{
Attempts: append([]AttemptResult(nil), s.attempts...),
}
if s.decision == exchangeCommit {
result.Err = joinDistinctError(result.Err, s.networkErr)
} else {
result.Err = joinDistinctError(result.Err, s.responseCloseErr)
}
for _, attempt := range s.attempts {
result.Records = append(result.Records, attempt.Records...)
if s.decision == exchangeCommit {
result.Err = joinDistinctError(result.Err, attempt.Err)
result.Err = joinDistinctError(result.Err, attempt.cleanupErr)
} else {
result.Err = joinDistinctError(result.Err, attempt.cleanupErr)
}
}
return result
}
func joinDistinctError(current, next error) error {
if next == nil {
return current
}
if current == nil {
return next
}
if errors.Is(current, next) {
return current
}
if errors.Is(next, current) {
return next
}
return errors.Join(current, next)
}