Skip to content
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

Merged
merged 2 commits into from
Sep 21, 2021
Merged

feat(daemon): avoid multiple initialization #1126

merged 2 commits into from
Sep 21, 2021

Conversation

knqyf263
Copy link
Contributor

Description

For example, if LayerByDiffID of daemon.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 locks initialize() before checking i.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 keep err in the image struct like imageOpener.

Related PR

#1121

@codecov-commenter
Copy link

codecov-commenter commented Sep 21, 2021

Codecov Report

Merging #1126 (c3aee49) into main (40ba044) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           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           
Impacted Files Coverage Δ
pkg/v1/daemon/image.go 76.69% <100.00%> (+0.69%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 40ba044...c3aee49. Read the comment docs.

@@ -99,6 +100,9 @@ func Image(ref name.Reference, options ...Option) (v1.Image, error) {
}

func (i *image) initialize() error {
i.mu.Lock()
Copy link
Collaborator

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.

Copy link
Contributor Author

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.

Copy link
Collaborator

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed 3eac11e

Copy link
Collaborator

@imjasonh imjasonh left a 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.

@jonjohnsonjr jonjohnsonjr merged commit 486e71f into google:main Sep 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants