-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.go
More file actions
219 lines (208 loc) · 5.43 KB
/
run.go
File metadata and controls
219 lines (208 loc) · 5.43 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
package jack
import (
"context"
"runtime/debug"
"sync"
)
// Group coordinates concurrent execution of functions with error and panic handling.
// It supports context cancellation and worker limits, collecting errors in a channel.
// Thread-safe via wait group, mutex, and channel operations.
type Group struct {
wg sync.WaitGroup // Tracks running goroutines
errCh chan error // Receives first error from goroutines
errOnce sync.Once // Ensures errCh is closed once
cancelOnce sync.Once // Ensures context cancellation is called once
sem chan struct{} // Limits concurrent workers
ctx context.Context // Group context for cancellation
cancel func() // Cancels the group context
}
// NewGroup creates a new Group for running functions concurrently.
// It initializes an error channel with a buffer of 1.
// Thread-safe via initialization.
// Example:
//
// group := NewGroup() // Creates a new group
func NewGroup() *Group {
return &Group{
errCh: make(chan error, 1),
}
}
// WithContext associates a context with the Group, enabling cancellation.
// It cancels any previous context and creates a new cancellable context.
// Thread-safe via context operations.
// Example:
//
// group.WithContext(ctx) // Sets group context
func (g *Group) WithContext(ctx context.Context) *Group {
if g.cancel != nil {
g.cancel()
}
g.ctx, g.cancel = context.WithCancel(ctx)
return g
}
// WithLimit sets the maximum number of concurrent workers in the Group.
// Non-positive values remove the limit.
// Thread-safe via semaphore initialization.
// Example:
//
// group.WithLimit(5) // Limits to 5 concurrent workers
func (g *Group) WithLimit(n int) *Group {
if n <= 0 {
g.sem = nil
return g
}
if cap(g.sem) != n {
g.sem = make(chan struct{}, n)
}
return g
}
// doWork executes a job with panic recovery and error handling.
// It sends the first error to the error channel and triggers context cancellation.
// Thread-safe via channel and once operations.
func (g *Group) doWork(job func() error) {
if g.sem != nil {
g.sem <- struct{}{}
defer func() { <-g.sem }()
}
var errToSend error
defer func() {
if errToSend != nil {
select {
case g.errCh <- errToSend:
default:
}
if g.cancel != nil {
g.cancelOnce.Do(g.cancel)
}
}
}()
func() {
defer func() {
if v := recover(); v != nil {
errToSend = &CaughtPanic{
Value: v,
Stack: debug.Stack(),
}
}
}()
errToSend = job()
}()
}
// Go runs a function in a new goroutine with error and panic handling.
// The first non-nil error or panic is sent to the Errors channel.
// If a context is set, it is cancelled on the first error.
// Thread-safe via wait group and doWork.
// Example:
//
// group.Go(func() error { return nil }) // Runs a function
func (g *Group) Go(f func() error) {
g.wg.Add(1)
go func() {
defer g.wg.Done()
g.doWork(f)
}()
}
// GoCtx runs a context-aware function in a new goroutine.
// It requires a context set via WithContext and passes it to the function.
// The first error, panic, or context cancellation is sent to the Errors channel,
// and the context is cancelled.
// Thread-safe via wait group and doWork.
// Example:
//
// group.WithContext(ctx).GoCtx(func(ctx context.Context) error { return nil }) // Runs a context-aware function
func (g *Group) GoCtx(f func(context.Context) error) {
if g.ctx == nil {
panic("jack.Group: must use WithContext before GoCtx")
}
g.wg.Add(1)
go func() {
defer g.wg.Done()
job := func() error {
if err := g.ctx.Err(); err != nil {
return err
}
return f(g.ctx)
}
if g.sem != nil {
g.sem <- struct{}{}
defer func() { <-g.sem }()
if err := g.ctx.Err(); err != nil {
select {
case g.errCh <- err:
default:
}
if g.cancel != nil {
g.cancelOnce.Do(g.cancel)
}
return
}
} else {
if err := g.ctx.Err(); err != nil {
select {
case g.errCh <- err:
default:
}
if g.cancel != nil {
g.cancelOnce.Do(g.cancel)
}
return
}
}
g.doWork(job)
}()
}
// Errors returns a channel that receives the first error from Go or GoCtx goroutines.
// The channel has a buffer of 1 and is closed by Wait.
// Thread-safe via channel operations.
// Example:
//
// for err := range group.Errors() { ... } // Reads errors
func (g *Group) Errors() <-chan error {
return g.errCh
}
// Wait blocks until all Go and GoCtx goroutines complete.
// It closes the Errors channel and cancels the Group’s context, if set.
// Thread-safe via wait group and once.
// Example:
//
// group.Wait() // Waits for all goroutines
func (g *Group) Wait() {
g.wg.Wait()
g.errOnce.Do(func() {
close(g.errCh)
if g.cancel != nil {
g.cancel()
}
})
}
// Go runs a function in a standalone goroutine with error and panic handling.
// It returns a buffered channel that receives the function’s error or a CaughtPanic.
// The channel is closed after the error or completion.
// Thread-safe via goroutine and channel operations.
// Example:
//
// errCh := Go(func() error { return nil }) // Runs a function and returns error channel
func Go(f func() error) <-chan error {
errCh := make(chan error, 1)
go func() {
var errToSend error
defer func() {
if errToSend != nil {
errCh <- errToSend
}
close(errCh)
}()
func() {
defer func() {
if v := recover(); v != nil {
errToSend = &CaughtPanic{
Value: v,
Stack: debug.Stack(),
}
}
}()
errToSend = f()
}()
}()
return errCh
}