Skip to content

Commit ffa27a0

Browse files
committed
feat: add empty struct alias.
1 parent 08fea4b commit ffa27a0

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

async.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ type executeResult struct {
1616
Index int
1717
}
1818

19+
// empty is a smallest cost struct.
20+
type empty struct{}
21+
1922
// getContext returns the specified non-nil context from the parameter, or creates and returns a
2023
// new empty context.
2124
func getContext(ctx context.Context) context.Context {

parallel.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ func parallel(parent context.Context, concurrency int, funcs ...AsyncFn) (int, e
4646
defer canFunc()
4747

4848
ch := make(chan executeResult) // channel for result
49-
var conch chan struct{} // channel for concurrency limit
49+
var conch chan empty // channel for concurrency limit
5050

5151
// no concurrency limitation if the value of the number is 0
5252
if concurrency > 0 {
53-
conch = make(chan struct{}, concurrency)
53+
conch = make(chan empty, concurrency)
5454
}
5555

5656
for i := 0; i < len(funcs); i++ {
@@ -78,11 +78,11 @@ func runTaskInParallel(
7878
ctx context.Context,
7979
n int,
8080
fn AsyncFn,
81-
conch chan struct{},
81+
conch chan empty,
8282
ch chan executeResult,
8383
) {
8484
if conch != nil {
85-
conch <- struct{}{}
85+
conch <- empty{}
8686
}
8787

8888
childCtx, childCanFunc := context.WithCancel(ctx)
@@ -154,18 +154,18 @@ func parallelComplete(parent context.Context, concurrency int, funcs ...AsyncFn)
154154
wg := sync.WaitGroup{}
155155
wg.Add(len(funcs))
156156

157-
var conch chan struct{} // channel for concurrency limit
157+
var conch chan empty // channel for concurrency limit
158158
// no concurrency limitation if the value of the number is 0
159159
if concurrency > 0 {
160-
conch = make(chan struct{}, concurrency)
160+
conch = make(chan empty, concurrency)
161161
}
162162

163163
for i := 0; i < len(funcs); i++ {
164164
go func(n int) {
165165
defer wg.Done()
166166

167167
if conch != nil {
168-
conch <- struct{}{}
168+
conch <- empty{}
169169
}
170170

171171
fn := funcs[n]

0 commit comments

Comments
 (0)