-
Notifications
You must be signed in to change notification settings - Fork 528
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(daemon): avoid multiple initialization #1126
Conversation
Codecov Report
@@ Coverage Diff @@
## main #1126 +/- ##
=======================================
Coverage 75.06% 75.07%
=======================================
Files 108 108
Lines 7800 7803 +3
=======================================
+ Hits 5855 5858 +3
Misses 1380 1380
Partials 565 565
Continue to review full report at Codecov.
|
pkg/v1/daemon/image.go
Outdated
@@ -99,6 +100,9 @@ func Image(ref name.Reference, options ...Option) (v1.Image, error) { | |||
} | |||
|
|||
func (i *image) initialize() error { | |||
i.mu.Lock() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of a mutex, could we use a sync.Once
? This would call tarball.Image
only the first time initialize
is called, taking into account multiple concurrent calls.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. As described in the description, we need to keep the err. Is it ok with you?
Note that we need to keep err in the image struct like imageOpener.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I missed that part of the original comment. That seems better to me than a mutex that will usually guard a no-op. Storing the error seems fine too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed 3eac11e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice. Thanks for doing this!
edit: I'll wait for @jonjohnsonjr to approve+merge in case he has other comments.
Description
For example, if
LayerByDiffID
ofdaemon.Image
is called twice in parallel,initialize
might be called twice at the same time. Then,tarball.Image
might be called twice. It is not efficient, so this PR locksinitialize()
before checkingi.tarballImage
for nil so that it will not be initialized twice.Please let me know if you prefer
sync.Once
. Note that we need to keeperr
in theimage
struct likeimageOpener
.go-containerregistry/pkg/v1/daemon/image.go
Line 46 in c3aee49
Related PR
#1121