-
Notifications
You must be signed in to change notification settings - Fork 2k
/
error_group.go
130 lines (116 loc) · 3.9 KB
/
error_group.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
/*
Copyright 2021 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package concurrency
import "context"
// ErrorGroup provides a function for waiting for N goroutines to complete with
// at least Z successes which we wanted to wait for and
// at least X overall successes and no more than Y failures, and cancelling the rest.
//
// It should be used as follows:
//
// errCh := make(chan concurrency.Error)
// errgroupCtx, errgroupCancel := context.WithCancel(ctx)
//
// for _, arg := range args {
// arg := arg
//
// go func() {
// err := doWork(errGroupCtx, arg)
// errCh <- concurrency.Error{
// Err: err,
// MustWaitFor: <boolean>,
// }
// }()
// }
//
// errgroup := concurrency.ErrorGroup{
// NumGoroutines: len(args),
// NumRequiredSuccess: 5, // need at least 5 to respond with nil error before cancelling the rest
// NumAllowedErrors: 1, // if more than 1 responds with non-nil error, cancel the rest
// NumErrorsToWaitFor: 1, // if there is 1 response that we must wait for, before cancelling the rest
// }
// errRec := errgroup.Wait(errgroupCancel, errCh)
//
// if errRec.HasErrors() {
// // ...
// }
//
// The NumErrorsToWaitFor should be equal to the number of
// Errors that are received on the channel which have MustWaitFor set
type ErrorGroup struct {
NumGoroutines int
NumRequiredSuccesses int
NumAllowedErrors int
NumErrorsToWaitFor int
}
// Error is used in ErrGroup.Wait function
// It contains the error that was received along
// with the information of whether the received error
// originated from a tablet that we must wait for
type Error struct {
Err error
MustWaitFor bool
}
// Wait waits for a group of goroutines that are sending errors to the given
// Error channel, and are cancellable by the given cancel function.
//
// Wait will cancel any outstanding goroutines when the following condition is met:
//
// - At least NumErrorsToWaitFor results with MustWaitFor set have been consumed
// on the error channel AND one of the following two -
// (1) More than NumAllowedErrors non-nil results have been consumed on the
// error channel.
//
// (2) At least NumRequiredSuccesses nil results have been consumed on the error
// channel.
//
// After the cancellation condition is triggered, Wait will continue to consume
// results off the Error channel so as to not permanently block any of those
// cancelled goroutines.
//
// When finished consuming results from all goroutines, cancelled or otherwise,
// Wait returns an AllErrorRecorder that contains all errors returned by any of
// those goroutines. It does not close the Error channel.
func (eg ErrorGroup) Wait(cancel context.CancelFunc, errors chan Error) *AllErrorRecorder {
errCounter := 0
successCounter := 0
responseCounter := 0
mustWaitForCounter := 0
rec := &AllErrorRecorder{}
if eg.NumGoroutines < 1 {
return rec
}
for err := range errors {
responseCounter++
if err.MustWaitFor {
mustWaitForCounter++
}
switch err.Err {
case nil:
successCounter++
default:
errCounter++
rec.RecordError(err.Err)
}
// Even though we cancel in the next conditional, we need to keep
// consuming off the channel, or those goroutines will get stuck
// forever.
if responseCounter == eg.NumGoroutines {
break
}
if mustWaitForCounter >= eg.NumErrorsToWaitFor && (errCounter > eg.NumAllowedErrors || successCounter >= eg.NumRequiredSuccesses) {
cancel()
}
}
return rec
}