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 #3092 from matrix-org/rav/response_cache_metrics
Browse files Browse the repository at this point in the history
Add metrics for ResponseCache
  • Loading branch information
richvdh authored Apr 12, 2018
2 parents 0f13f30 + b338423 commit d5c74b9
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 7 deletions.
3 changes: 2 additions & 1 deletion synapse/appservice/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ def __init__(self, hs):
super(ApplicationServiceApi, self).__init__(hs)
self.clock = hs.get_clock()

self.protocol_meta_cache = ResponseCache(hs, timeout_ms=HOUR_IN_MS)
self.protocol_meta_cache = ResponseCache(hs, "as_protocol_meta",
timeout_ms=HOUR_IN_MS)

@defer.inlineCallbacks
def query_user(self, service, user_id):
Expand Down
2 changes: 1 addition & 1 deletion synapse/federation/federation_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def __init__(self, hs):

# We cache responses to state queries, as they take a while and often
# come in waves.
self._state_resp_cache = ResponseCache(hs, timeout_ms=30000)
self._state_resp_cache = ResponseCache(hs, "state_resp", timeout_ms=30000)

@defer.inlineCallbacks
@log_function
Expand Down
5 changes: 3 additions & 2 deletions synapse/handlers/room_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@
class RoomListHandler(BaseHandler):
def __init__(self, hs):
super(RoomListHandler, self).__init__(hs)
self.response_cache = ResponseCache(hs)
self.remote_response_cache = ResponseCache(hs, timeout_ms=30 * 1000)
self.response_cache = ResponseCache(hs, "room_list")
self.remote_response_cache = ResponseCache(hs, "remote_room_list",
timeout_ms=30 * 1000)

def get_local_public_room_list(self, limit=None, since_token=None,
search_filter=None,
Expand Down
2 changes: 1 addition & 1 deletion synapse/handlers/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def __init__(self, hs):
self.presence_handler = hs.get_presence_handler()
self.event_sources = hs.get_event_sources()
self.clock = hs.get_clock()
self.response_cache = ResponseCache(hs)
self.response_cache = ResponseCache(hs, "sync")
self.state = hs.get_state_handler()

def wait_for_sync_for_user(self, sync_config, since_token=None, timeout=0,
Expand Down
2 changes: 1 addition & 1 deletion synapse/replication/http/send_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def __init__(self, hs):
self.clock = hs.get_clock()

# The responses are tiny, so we may as well cache them for a while
self.response_cache = ResponseCache(hs, timeout_ms=30 * 60 * 1000)
self.response_cache = ResponseCache(hs, "send_event", timeout_ms=30 * 60 * 1000)

def on_PUT(self, request, event_id):
result = self.response_cache.get(event_id)
Expand Down
14 changes: 13 additions & 1 deletion synapse/util/caches/response_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# limitations under the License.

from synapse.util.async import ObservableDeferred
from synapse.util.caches import metrics as cache_metrics


class ResponseCache(object):
Expand All @@ -24,12 +25,21 @@ class ResponseCache(object):
used rather than trying to compute a new response.
"""

def __init__(self, hs, timeout_ms=0):
def __init__(self, hs, name, timeout_ms=0):
self.pending_result_cache = {} # Requests that haven't finished yet.

self.clock = hs.get_clock()
self.timeout_sec = timeout_ms / 1000.

self._metrics = cache_metrics.register_cache(
"response_cache",
size_callback=lambda: self.size(),
cache_name=name,
)

def size(self):
return len(self.pending_result_cache)

def get(self, key):
"""Look up the given key.
Expand All @@ -45,8 +55,10 @@ def get(self, key):
"""
result = self.pending_result_cache.get(key)
if result is not None:
self._metrics.inc_hits()
return result.observe()
else:
self._metrics.inc_misses()
return None

def set(self, key, deferred):
Expand Down

0 comments on commit d5c74b9

Please sign in to comment.