Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #5730 from matrix-org/erikj/cache_versions
Browse files Browse the repository at this point in the history
Cache get_version_string.
  • Loading branch information
erikjohnston authored Jul 22, 2019
2 parents 8e513e7 + 22e8623 commit 720d304
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog.d/5730.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Cache result of get_version_string to reduce overhead of `/version` federation requests.
23 changes: 21 additions & 2 deletions synapse/util/versionstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,23 @@


def get_version_string(module):
"""Given a module calculate a git-aware version string for it.
If called on a module not in a git checkout will return `__verison__`.
Args:
module (module)
Returns:
str
"""

cached_version = getattr(module, "_synapse_version_string_cache", None)
if cached_version:
return cached_version

version_string = module.__version__

try:
null = open(os.devnull, "w")
cwd = os.path.dirname(os.path.abspath(module.__file__))
Expand Down Expand Up @@ -80,8 +97,10 @@ def get_version_string(module):
s for s in (git_branch, git_tag, git_commit, git_dirty) if s
)

return "%s (%s)" % (module.__version__, git_version)
version_string = "%s (%s)" % (module.__version__, git_version)
except Exception as e:
logger.info("Failed to check for git repository: %s", e)

return module.__version__
module._synapse_version_string_cache = version_string

return version_string

0 comments on commit 720d304

Please sign in to comment.