forked from MirrorHub/synapse
ResponseCache: fix handling of completed results
Turns out that ObservableDeferred.observe doesn't return a deferred if the result is already completed. Fix handling and improve documentation.
This commit is contained in:
parent
b78395b7fe
commit
60f6014bb7
1 changed files with 19 additions and 13 deletions
|
@ -14,6 +14,8 @@
|
|||
# limitations under the License.
|
||||
import logging
|
||||
|
||||
from twisted.internet import defer
|
||||
|
||||
from synapse.util.async import ObservableDeferred
|
||||
from synapse.util.caches import metrics as cache_metrics
|
||||
from synapse.util.logcontext import make_deferred_yieldable, run_in_background
|
||||
|
@ -48,15 +50,21 @@ class ResponseCache(object):
|
|||
def get(self, key):
|
||||
"""Look up the given key.
|
||||
|
||||
Returns a deferred which doesn't follow the synapse logcontext rules,
|
||||
so you'll probably want to make_deferred_yieldable it.
|
||||
Can return either a new Deferred (which also doesn't follow the synapse
|
||||
logcontext rules), or, if the request has completed, the actual
|
||||
result. You will probably want to make_deferred_yieldable the result.
|
||||
|
||||
If there is no entry for the key, returns None. It is worth noting that
|
||||
this means there is no way to distinguish a completed result of None
|
||||
from an absent cache entry.
|
||||
|
||||
Args:
|
||||
key (hashable):
|
||||
|
||||
Returns:
|
||||
twisted.internet.defer.Deferred|None: None if there is no entry
|
||||
for this key; otherwise a deferred result.
|
||||
twisted.internet.defer.Deferred|None|E: None if there is no entry
|
||||
for this key; otherwise either a deferred result or the result
|
||||
itself.
|
||||
"""
|
||||
result = self.pending_result_cache.get(key)
|
||||
if result is not None:
|
||||
|
@ -73,19 +81,17 @@ class ResponseCache(object):
|
|||
you should wrap normal synapse deferreds with
|
||||
logcontext.run_in_background).
|
||||
|
||||
Returns a new Deferred which also doesn't follow the synapse logcontext
|
||||
rules, so you will want to make_deferred_yieldable it
|
||||
|
||||
(TODO: before using this more widely, it might make sense to refactor
|
||||
it and get() so that they do the necessary wrapping rather than having
|
||||
to do it everywhere ResponseCache is used.)
|
||||
Can return either a new Deferred (which also doesn't follow the synapse
|
||||
logcontext rules), or, if *deferred* was already complete, the actual
|
||||
result. You will probably want to make_deferred_yieldable the result.
|
||||
|
||||
Args:
|
||||
key (hashable):
|
||||
deferred (twisted.internet.defer.Deferred):
|
||||
deferred (twisted.internet.defer.Deferred[T):
|
||||
|
||||
Returns:
|
||||
twisted.internet.defer.Deferred
|
||||
twisted.internet.defer.Deferred[T]|T: a new deferred, or the actual
|
||||
result.
|
||||
"""
|
||||
result = ObservableDeferred(deferred, consumeErrors=True)
|
||||
self.pending_result_cache[key] = result
|
||||
|
@ -144,7 +150,7 @@ class ResponseCache(object):
|
|||
self._name, key)
|
||||
d = run_in_background(callback, *args, **kwargs)
|
||||
result = self.set(key, d)
|
||||
elif result.called:
|
||||
elif not isinstance(result, defer.Deferred) or result.called:
|
||||
logger.info("[%s]: using completed cached result for [%s]",
|
||||
self._name, key)
|
||||
else:
|
||||
|
|
Loading…
Reference in a new issue