Skip to content

Commit

Permalink
bugfix: add lock to image bolb list (#2238)
Browse files Browse the repository at this point in the history
Signed-off-by: kakzhou719 <kakazhou719@163.com>
  • Loading branch information
kakaZhou719 authored Jun 5, 2023
1 parent 211005f commit 9b08d0c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
1 change: 0 additions & 1 deletion pkg/image/save/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"context"

"github.com/docker/docker/pkg/progress"

v1 "github.com/sealerio/sealer/types/api/v1"
)

Expand Down
33 changes: 26 additions & 7 deletions pkg/image/save/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"io"
"strings"
"sync"

"github.com/distribution/distribution/v3"
"github.com/distribution/distribution/v3/configuration"
Expand All @@ -32,13 +33,12 @@ import (
"github.com/docker/docker/pkg/progress"
"github.com/docker/docker/pkg/streamformatter"
"github.com/opencontainers/go-digest"
"github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"

"github.com/sealerio/sealer/common"
"github.com/sealerio/sealer/pkg/client/docker/auth"
"github.com/sealerio/sealer/pkg/image/save/distributionpkg/proxy"
v1 "github.com/sealerio/sealer/types/api/v1"
"github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
)

const (
Expand Down Expand Up @@ -321,9 +321,16 @@ func (is *DefaultImageSaver) saveManifestAndGetDigest(nameds []Named, repo distr
if err != nil {
return nil, fmt.Errorf("failed to get manifest service: %v", err)
}

var (
// lock protects imageDigests
lock sync.Mutex
imageDigests = make([]digest.Digest, 0)
numCh = make(chan struct{}, maxPullGoroutineNum)
)

eg, _ := errgroup.WithContext(context.Background())
numCh := make(chan struct{}, maxPullGoroutineNum)
imageDigests := make([]digest.Digest, 0)

for _, named := range nameds {
tmpnamed := named
numCh <- struct{}{}
Expand All @@ -340,6 +347,9 @@ func (is *DefaultImageSaver) saveManifestAndGetDigest(nameds []Named, repo distr
if err != nil {
return fmt.Errorf("failed to get digest: %v", err)
}

lock.Lock()
defer lock.Unlock()
imageDigests = append(imageDigests, imageDigest)
return nil
})
Expand Down Expand Up @@ -388,9 +398,15 @@ func (is *DefaultImageSaver) saveBlobs(imageDigests []digest.Digest, repo distri
if err != nil {
return fmt.Errorf("failed to get blob service: %v", err)
}

var (
// lock protects blobLists
lock sync.Mutex
blobLists = make([]digest.Digest, 0)
numCh = make(chan struct{}, maxPullGoroutineNum)
)

eg, _ := errgroup.WithContext(context.Background())
numCh := make(chan struct{}, maxPullGoroutineNum)
blobLists := make([]digest.Digest, 0)

//get blob list
//each blob identified by a digest
Expand All @@ -411,6 +427,9 @@ func (is *DefaultImageSaver) saveBlobs(imageDigests []digest.Digest, repo distri
if err != nil {
return fmt.Errorf("failed to get blob list: %v", err)
}

lock.Lock()
defer lock.Unlock()
blobLists = append(blobLists, blobList...)
return nil
})
Expand Down

0 comments on commit 9b08d0c

Please sign in to comment.