Repository navigation
git/version.go: replace sync.Mutex usage with sync.Once - #3492
Merged
Merged
Conversation
We use a sync.Mutex to synchronize access to the string pointer
`gitVersion`, which indicates the version of Git used by the system
running Git LFS.
Our basic usage of the sync.Mutex is not incorrect, but we can improve
the readability by instead using a sync.Once. A sync.Once determines
very quickly (and in an atomic, goroutine-safe fashion) whether or not
_any_ function has been run, and if it hasn't, run it.
By doing this, we can--at the first request--produce a value for the
result of running 'git version', and then return it later to the caller.
This has the following benefits:
- If 'git version' has already been run, we do not need to hold the
lock for the entire duration of the function.
- If 'git version' has not already been run, we only run it once,
retaining the existing behavior.
Only one change, which is the introduction of the `gitVersionErr`
variable. This is a consequence of executing the 'git version' call in a
closure: since we're in a new stack frame, we can't return from our
parent.
Instead, we retain the value for all time, and return _it_, along with
whatever value we got from running 'git version' in the first place.
bk2204
approved these changes
Feb 12, 2019
bk2204
left a comment
Member
There was a problem hiding this comment.
This looks great. Thanks for writing this up, and my apologies for taking so long to get to review this. I had looked at it but neglected to press the button approving it for some reason.
Contributor
Author
Thanks for reviewing it, and no worries. I had forgotten about it myself until I was browsing through open pull requests (as I tend to do every ~week or so just to make sure things aren't getting stale). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
We use a sync.Mutex to synchronize access to the string pointer
gitVersion, which indicates the version of Git used by the systemrunning Git LFS.
Our basic usage of the sync.Mutex is not incorrect, but we can improve
the readability by instead using a sync.Once. A sync.Once determines
very quickly (and in an atomic, goroutine-safe fashion) whether or not
any function has been run, and if it hasn't, run it.
By doing this, we can--at the first request--produce a value for the
result of running 'git version', and then return it later to the caller.
This has the following benefits:
If 'git version' has already been run, we do not need to hold the
lock for the entire duration of the function.
If 'git version' has not already been run, we only run it once,
retaining the existing behavior.
Only one change, which is the introduction of the
gitVersionErrvariable. This is a consequence of executing the 'git version' call in a
closure: since we're in a new stack frame, we can't return from our
parent.
Instead, we retain the value for all time, and return it, along with
whatever value we got from running 'git version' in the first place.
/cc @git-lfs/core
/cc #3490