Skip to content

git/version.go: replace sync.Mutex usage with sync.Once - #3492

Merged
ttaylorr merged 1 commit into
masterfrom
ttaylorr/git-version-use-once
Feb 14, 2019
Merged

ttaylorr merged 1 commit into
masterfrom
ttaylorr/git-version-use-once

Conversation

@ttaylorr

Copy link
Copy Markdown
Contributor

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.

/cc @git-lfs/core
/cc #3490

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.
@ttaylorr ttaylorr added this to the v2.7.0 milestone Jan 23, 2019
@ttaylorr
ttaylorr requested a review from a team February 12, 2019 00:09

@bk2204 bk2204 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

@ttaylorr
ttaylorr merged commit e32fc0f into master Feb 14, 2019
@ttaylorr

Copy link
Copy Markdown
Contributor Author

This looks great. Thanks for writing this up, and my apologies for taking so long to get to review this.

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).

@ttaylorr
ttaylorr deleted the ttaylorr/git-version-use-once branch February 14, 2019 05:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants