-
Notifications
You must be signed in to change notification settings - Fork 0
/
async-group.go
80 lines (67 loc) · 1.59 KB
/
async-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
// Copyright (C) 2023 Takayuki Sato. All Rights Reserved.
// This program is free software under MIT License.
// See the file LICENSE in this distribution for more details.
package sabi
import (
"sync"
"github.com/sttk/sabi/errs"
)
// AsyncGroup is the interface to execute added functions asynchronously.
// The method: Add is to add target functions.
// This interface is used as an argument of DaxSrc#Setup, DaxConn#Commit, and DaxConn#Rollback.
type AsyncGroup interface {
Add(fn func() errs.Err)
}
type errEntry[N comparable] struct {
name N
err errs.Err
next *errEntry[N]
}
type asyncGroupAsync[N comparable] struct {
wg sync.WaitGroup
errHead *errEntry[N]
errLast *errEntry[N]
mutex sync.Mutex
name N
}
func (ag *asyncGroupAsync[N]) Add(fn func() errs.Err) {
ag.wg.Add(1)
go func(name N) {
defer ag.wg.Done()
err := fn()
if err.IsNotOk() {
ag.mutex.Lock()
defer ag.mutex.Unlock()
ag.addErr(name, err)
}
}(ag.name)
}
func (ag *asyncGroupAsync[N]) wait() {
ag.wg.Wait()
}
func (ag *asyncGroupAsync[N]) addErr(name N, err errs.Err) {
ent := &errEntry[N]{name: name, err: err}
if ag.errLast == nil {
ag.errHead = ent
ag.errLast = ent
} else {
ag.errLast.next = ent
ag.errLast = ent
}
}
func (ag *asyncGroupAsync[N]) hasErr() bool {
return (ag.errHead != nil)
}
func (ag *asyncGroupAsync[N]) makeErrs() map[N]errs.Err {
m := make(map[N]errs.Err)
for ent := ag.errHead; ent != nil; ent = ent.next {
m[ent.name] = ent.err
}
return m
}
type asyncGroupSync struct {
err errs.Err
}
func (ag *asyncGroupSync) Add(fn func() errs.Err) {
ag.err = fn()
}