-
Notifications
You must be signed in to change notification settings - Fork 0
/
concurrentfunc.go
68 lines (57 loc) · 1.36 KB
/
concurrentfunc.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
package concurrentfunc
import (
"context"
"sync"
"time"
)
// this library can be use if we have move than 1 function to run at the same time
// it's has timeout
// see the md file to see full example how to properly used it
type ConcurrentFunc func(context.Context) (any, error)
type Entity struct {
Entity any
Err error
}
func Exec(ctx context.Context, timeout time.Duration, funcs ...ConcurrentFunc) ([]any, []error, error) {
newCtx, cancelNewCtx := context.WithTimeout(ctx, timeout)
defer cancelNewCtx()
res := make([]any, len(funcs))
err := make([]error, len(funcs))
var rootCause error
doneChannel := make(chan struct{})
wg := new(sync.WaitGroup)
wg.Add(len(funcs))
mutex := new(sync.Mutex)
for idx, func_ := range funcs {
func_ := func_
idx := idx
go func() {
defer wg.Done()
var tempEntity Entity
tempEntity.Entity, tempEntity.Err = func_(newCtx)
// cancel the context to cancel the other function
// and put
if tempEntity.Err != nil {
mutex.Lock()
if rootCause == nil {
rootCause = tempEntity.Err
}
mutex.Unlock()
cancelNewCtx()
}
res[idx] = tempEntity.Entity
err[idx] = tempEntity.Err
}()
}
go func() {
defer close(doneChannel)
wg.Wait()
}()
select {
case <-time.After(timeout):
cancelNewCtx()
return nil, nil, newCtx.Err()
case <-doneChannel:
}
return res, err, rootCause
}