From 5ea773c50568088b30e15728b65480d0335fe14e Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Mon, 22 Jul 2019 13:15:08 +0100 Subject: [PATCH 1/3] Cache get_version_string. The version of a module isn't going to change over the lifetime of the process (assuming no funky hot reloading is going on, which it isn't), so let's just cache the result to avoid spawning lots of git subprocesses. Fixes #5672. --- synapse/util/versionstring.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/synapse/util/versionstring.py b/synapse/util/versionstring.py index a4d9a462f..fa404b9d7 100644 --- a/synapse/util/versionstring.py +++ b/synapse/util/versionstring.py @@ -22,6 +22,23 @@ logger = logging.getLogger(__name__) 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__)) @@ -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 From 2017369f7d134fb40f52564123819d6c77f4f9b0 Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Mon, 22 Jul 2019 13:18:25 +0100 Subject: [PATCH 2/3] Newsfile --- changelog.d/5730.misc | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/5730.misc diff --git a/changelog.d/5730.misc b/changelog.d/5730.misc new file mode 100644 index 000000000..88767fe2f --- /dev/null +++ b/changelog.d/5730.misc @@ -0,0 +1 @@ +Cache result of get_version_string to reduce overhead off /versions client and federation requests. From 22e862304a9c3faac86d4373a50a3b7efd6758b1 Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Mon, 22 Jul 2019 14:09:56 +0100 Subject: [PATCH 3/3] Update changelog.d/5730.misc Co-Authored-By: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> --- changelog.d/5730.misc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog.d/5730.misc b/changelog.d/5730.misc index 88767fe2f..a99677f5e 100644 --- a/changelog.d/5730.misc +++ b/changelog.d/5730.misc @@ -1 +1 @@ -Cache result of get_version_string to reduce overhead off /versions client and federation requests. +Cache result of get_version_string to reduce overhead of `/version` federation requests.