Skip to content

Commit 4c8706d

Browse files
authored
fix: cancel context when using errgroup for error return earlier (#213)
Signed-off-by: chlins <[email protected]>
1 parent 14532fe commit 4c8706d

File tree

5 files changed

+35
-5
lines changed

5 files changed

+35
-5
lines changed

pkg/backend/extract.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,16 @@ func (b *backend) Extract(ctx context.Context, target string, cfg *config.Extrac
6262

6363
// exportModelArtifact exports the target model artifact to the output directory, which will open the artifact and extract to restore the original repo structure.
6464
func exportModelArtifact(ctx context.Context, store storage.Storage, manifest ocispec.Manifest, repo string, cfg *config.Extract) error {
65-
g := &errgroup.Group{}
65+
g, ctx := errgroup.WithContext(ctx)
6666
g.SetLimit(cfg.Concurrency)
6767

6868
for _, layer := range manifest.Layers {
6969
g.Go(func() error {
70+
select {
71+
case <-ctx.Done():
72+
return ctx.Err()
73+
default:
74+
}
7075
// pull the blob from the storage.
7176
reader, err := store.PullBlob(ctx, repo, layer.Digest.String())
7277
if err != nil {

pkg/backend/fetch.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,17 @@ func (b *backend) Fetch(ctx context.Context, target string, cfg *config.Fetch) e
8282
pb.Start()
8383
defer pb.Stop()
8484

85-
g := &errgroup.Group{}
85+
g, ctx := errgroup.WithContext(ctx)
8686
g.SetLimit(cfg.Concurrency)
8787

8888
for _, layer := range layers {
8989
g.Go(func() error {
90+
select {
91+
case <-ctx.Done():
92+
return ctx.Err()
93+
default:
94+
}
95+
9096
return pullAndExtractFromRemote(ctx, pb, internalpb.NormalizePrompt("Fetching blob"), client, cfg.Output, layer)
9197
})
9298
}

pkg/backend/pull.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func (b *backend) Pull(ctx context.Context, target string, cfg *config.Pull) err
8282

8383
// copy the layers.
8484
dst := b.store
85-
g := &errgroup.Group{}
85+
g, ctx := errgroup.WithContext(ctx)
8686
g.SetLimit(cfg.Concurrency)
8787

8888
var fn func(desc ocispec.Descriptor) error
@@ -98,6 +98,12 @@ func (b *backend) Pull(ctx context.Context, target string, cfg *config.Pull) err
9898

9999
for _, layer := range manifest.Layers {
100100
g.Go(func() error {
101+
select {
102+
case <-ctx.Done():
103+
return ctx.Err()
104+
default:
105+
}
106+
101107
return retry.Do(func() error {
102108
// call the before hook.
103109
cfg.Hooks.BeforePullLayer(layer, manifest)

pkg/backend/pull_by_d7y.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,17 @@ func (b *backend) pullByDragonfly(ctx context.Context, target string, cfg *confi
9595
defer pb.Stop()
9696

9797
// Process layers concurrently.
98-
g := &errgroup.Group{}
98+
g, ctx := errgroup.WithContext(ctx)
9999
g.SetLimit(cfg.Concurrency)
100100

101101
for _, layer := range manifest.Layers {
102102
g.Go(func() error {
103+
select {
104+
case <-ctx.Done():
105+
return ctx.Err()
106+
default:
107+
}
108+
103109
return processLayer(ctx, pb, dfdaemon.NewDfdaemonDownloadClient(conn), ref, manifest, layer, authToken, cfg)
104110
})
105111
}

pkg/backend/push.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,17 @@ func (b *backend) Push(ctx context.Context, target string, cfg *config.Push) err
7373
// note: the order is important, manifest should be pushed at last.
7474

7575
// copy the layers.
76-
g := &errgroup.Group{}
76+
g, ctx := errgroup.WithContext(ctx)
7777
g.SetLimit(cfg.Concurrency)
78+
7879
for _, layer := range manifest.Layers {
7980
g.Go(func() error {
81+
select {
82+
case <-ctx.Done():
83+
return ctx.Err()
84+
default:
85+
}
86+
8087
return retry.Do(func() error {
8188
return pushIfNotExist(ctx, pb, internalpb.NormalizePrompt("Copying blob"), src, dst, layer, repo, tag)
8289
}, append(defaultRetryOpts, retry.Context(ctx))...)

0 commit comments

Comments
 (0)