diff options
author | Erik Johnston <erik@matrix.org> | 2016-06-03 12:00:33 +0100 |
---|---|---|
committer | Erik Johnston <erik@matrix.org> | 2016-06-03 12:00:33 +0100 |
commit | 3b096c5f5cc55f43cb96bf951dcddf7c0064fd22 (patch) | |
tree | 5b5fc4ed70b8455fdc760ccb887b4f4cd4efd41f /synapse/util | |
parent | Merge pull request #830 from matrix-org/erikj/metrics_perf (diff) | |
parent | Pull out update_results_dict (diff) | |
download | synapse-3b096c5f5cc55f43cb96bf951dcddf7c0064fd22.tar.xz |
Merge branch 'erikj/cache_perf' of github.com:matrix-org/synapse into develop
Diffstat (limited to 'synapse/util')
-rw-r--r-- | synapse/util/async.py | 9 | ||||
-rw-r--r-- | synapse/util/caches/descriptors.py | 35 |
2 files changed, 34 insertions, 10 deletions
diff --git a/synapse/util/async.py b/synapse/util/async.py index 0d6f48e2d8..40be7fe7e3 100644 --- a/synapse/util/async.py +++ b/synapse/util/async.py @@ -102,6 +102,15 @@ class ObservableDeferred(object): def observers(self): return self._observers + def has_called(self): + return self._result is not None + + def has_succeeded(self): + return self._result is not None and self._result[0] is True + + def get_result(self): + return self._result[1] + def __getattr__(self, name): return getattr(self._deferred, name) diff --git a/synapse/util/caches/descriptors.py b/synapse/util/caches/descriptors.py index 5d25c9e762..f31dfb22b7 100644 --- a/synapse/util/caches/descriptors.py +++ b/synapse/util/caches/descriptors.py @@ -33,6 +33,7 @@ import functools import inspect import threading + logger = logging.getLogger(__name__) @@ -302,16 +303,21 @@ class CacheListDescriptor(object): # cached is a dict arg -> deferred, where deferred results in a # 2-tuple (`arg`, `result`) - cached = {} + results = {} + cached_defers = {} missing = [] for arg in list_args: key = list(keyargs) key[self.list_pos] = arg try: - res = cache.get(tuple(key)).observe() - res.addCallback(lambda r, arg: (arg, r), arg) - cached[arg] = res + res = cache.get(tuple(key)) + if not res.has_succeeded(): + res = res.observe() + res.addCallback(lambda r, arg: (arg, r), arg) + cached_defers[arg] = res + else: + results[arg] = res.get_result() except KeyError: missing.append(arg) @@ -349,12 +355,21 @@ class CacheListDescriptor(object): res = observer.observe() res.addCallback(lambda r, arg: (arg, r), arg) - cached[arg] = res - - return preserve_context_over_deferred(defer.gatherResults( - cached.values(), - consumeErrors=True, - ).addErrback(unwrapFirstError).addCallback(lambda res: dict(res))) + cached_defers[arg] = res + + if cached_defers: + def update_results_dict(res): + results.update(res) + return results + + return preserve_context_over_deferred(defer.gatherResults( + cached_defers.values(), + consumeErrors=True, + ).addCallback(update_results_dict).addErrback( + unwrapFirstError + )) + else: + return results obj.__dict__[self.orig.__name__] = wrapped |