Skip to content

Commit 96bef07

Browse files
committed
Fix bad group usages
1 parent ec1df65 commit 96bef07

File tree

4 files changed

+40
-26
lines changed

4 files changed

+40
-26
lines changed

common/bufio/copy.go

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,6 @@ func CopyExtendedWithPool(originSource io.Reader, destination N.ExtendedWriter,
157157
}
158158

159159
func CopyConn(ctx context.Context, source net.Conn, destination net.Conn) error {
160-
return CopyConnContextList([]context.Context{ctx}, source, destination)
161-
}
162-
163-
func CopyConnContextList(contextList []context.Context, source net.Conn, destination net.Conn) error {
164160
var group task.Group
165161
if _, dstDuplex := common.Cast[N.WriteCloser](destination); dstDuplex {
166162
group.Append("upload", func(ctx context.Context) error {
@@ -197,7 +193,19 @@ func CopyConnContextList(contextList []context.Context, source net.Conn, destina
197193
group.Cleanup(func() {
198194
common.Close(source, destination)
199195
})
200-
return group.RunContextList(contextList)
196+
return group.Run(ctx)
197+
}
198+
199+
// Deprecated: not used
200+
func CopyConnContextList(contextList []context.Context, source net.Conn, destination net.Conn) error {
201+
switch len(contextList) {
202+
case 0:
203+
return CopyConn(context.Background(), source, destination)
204+
case 1:
205+
return CopyConn(contextList[0], source, destination)
206+
default:
207+
panic("invalid context list")
208+
}
201209
}
202210

203211
func CopyPacket(destinationConn N.PacketWriter, source N.PacketReader) (n int64, err error) {
@@ -318,10 +326,6 @@ func WritePacketWithPool(originSource N.PacketReader, destinationConn N.PacketWr
318326
}
319327

320328
func CopyPacketConn(ctx context.Context, source N.PacketConn, destination N.PacketConn) error {
321-
return CopyPacketConnContextList([]context.Context{ctx}, source, destination)
322-
}
323-
324-
func CopyPacketConnContextList(contextList []context.Context, source N.PacketConn, destination N.PacketConn) error {
325329
var group task.Group
326330
group.Append("upload", func(ctx context.Context) error {
327331
return common.Error(CopyPacket(destination, source))
@@ -333,5 +337,17 @@ func CopyPacketConnContextList(contextList []context.Context, source N.PacketCon
333337
common.Close(source, destination)
334338
})
335339
group.FastFail()
336-
return group.RunContextList(contextList)
340+
return group.Run(ctx)
341+
}
342+
343+
// Deprecated: not used
344+
func CopyPacketConnContextList(contextList []context.Context, source N.PacketConn, destination N.PacketConn) error {
345+
switch len(contextList) {
346+
case 0:
347+
return CopyPacketConn(context.Background(), source, destination)
348+
case 1:
349+
return CopyPacketConn(contextList[0], source, destination)
350+
default:
351+
panic("invalid context list")
352+
}
337353
}

common/context.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"reflect"
66
)
77

8+
// Deprecated: not used
89
func SelectContext(contextList []context.Context) (int, error) {
910
if len(contextList) == 1 {
1011
<-contextList[0].Done()

common/task/task.go

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,9 @@ func (g *Group) Concurrency(n int) {
5454
}
5555
}
5656

57-
func (g *Group) Run(contextList ...context.Context) error {
58-
return g.RunContextList(contextList)
59-
}
60-
61-
func (g *Group) RunContextList(contextList []context.Context) error {
62-
if len(contextList) == 0 {
63-
contextList = append(contextList, context.Background())
64-
}
65-
57+
func (g *Group) Run(ctx context.Context) error {
6658
taskContext, taskFinish := common.ContextWithCancelCause(context.Background())
67-
taskCancelContext, taskCancel := common.ContextWithCancelCause(contextList[0])
59+
taskCancelContext, taskCancel := common.ContextWithCancelCause(ctx)
6860

6961
var errorAccess sync.Mutex
7062
var returnError error
@@ -112,19 +104,22 @@ func (g *Group) RunContextList(contextList []context.Context) error {
112104
}()
113105
}
114106

115-
selectedContext, upstreamErr := common.SelectContext(append([]context.Context{taskCancelContext}, contextList[1:]...))
116-
taskCancel(upstreamErr)
107+
var upstreamErr bool
108+
select {
109+
case <-taskCancelContext.Done():
110+
case <-ctx.Done():
111+
upstreamErr = true
112+
taskCancel(ctx.Err())
113+
}
117114

118115
if g.cleanup != nil {
119116
g.cleanup()
120117
}
121118

122119
<-taskContext.Done()
123120

124-
if selectedContext != 0 {
125-
returnError = E.Append(returnError, upstreamErr, func(err error) error {
126-
return E.Cause(err, "upstream")
127-
})
121+
if upstreamErr {
122+
return ctx.Err()
128123
}
129124

130125
return returnError

common/task/task_deprecated.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package task
22

33
import "context"
44

5+
// Deprecated: Use Group instead
56
func Run(ctx context.Context, tasks ...func() error) error {
67
var group Group
78
for _, task := range tasks {
@@ -13,6 +14,7 @@ func Run(ctx context.Context, tasks ...func() error) error {
1314
return group.Run(ctx)
1415
}
1516

17+
// Deprecated: Use Group instead
1618
func Any(ctx context.Context, tasks ...func(ctx context.Context) error) error {
1719
var group Group
1820
for _, task := range tasks {

0 commit comments

Comments
 (0)