Add ctx to task.Any

This commit is contained in:
世界 2022-07-08 12:16:39 +08:00
parent 6a0987c52a
commit 04e100e91a
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
2 changed files with 6 additions and 6 deletions

View file

@ -283,18 +283,18 @@ func CopyPacketWithPoolTimeout(dest N.PacketWriter, src N.TimeoutPacketReader, t
func CopyPacketConn(ctx context.Context, conn N.PacketConn, dest N.PacketConn) error { func CopyPacketConn(ctx context.Context, conn N.PacketConn, dest N.PacketConn) error {
defer common.Close(conn, dest) defer common.Close(conn, dest)
return task.Any(ctx, func() error { return task.Any(ctx, func(ctx context.Context) error {
return common.Error(CopyPacket(dest, conn)) return common.Error(CopyPacket(dest, conn))
}, func() error { }, func(ctx context.Context) error {
return common.Error(CopyPacket(conn, dest)) return common.Error(CopyPacket(conn, dest))
}) })
} }
func CopyPacketConnTimeout(ctx context.Context, conn N.PacketConn, dest N.PacketConn, timeout time.Duration) error { func CopyPacketConnTimeout(ctx context.Context, conn N.PacketConn, dest N.PacketConn, timeout time.Duration) error {
defer common.Close(conn, dest) defer common.Close(conn, dest)
return task.Any(ctx, func() error { return task.Any(ctx, func(ctx context.Context) error {
return common.Error(CopyPacketTimeout(dest, conn, timeout)) return common.Error(CopyPacketTimeout(dest, conn, timeout))
}, func() error { }, func(ctx context.Context) error {
return common.Error(CopyPacketTimeout(conn, dest, timeout)) return common.Error(CopyPacketTimeout(conn, dest, timeout))
}) })
} }

View file

@ -34,13 +34,13 @@ func Run(ctx context.Context, tasks ...func() error) error {
} }
//goland:noinspection GoVetLostCancel //goland:noinspection GoVetLostCancel
func Any(ctx context.Context, tasks ...func() error) error { func Any(ctx context.Context, tasks ...func(ctx context.Context) error) error {
runtimeCtx, cancel := context.WithCancel(ctx) runtimeCtx, cancel := context.WithCancel(ctx)
var retErr error var retErr error
for _, task := range tasks { for _, task := range tasks {
currentTask := task currentTask := task
go func() { go func() {
if err := currentTask(); err != nil { if err := currentTask(runtimeCtx); err != nil {
retErr = err retErr = err
} }
cancel() cancel()