This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Get pdu limiting #79
Merged
Merged
Get pdu limiting #79
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2674aeb
Factor out ExpiringCache from StateHandler
erikjohnston baa5b9a
Cache results of get_pdu.
erikjohnston 0194e71
Merge branch 'develop' of github.com:matrix-org/synapse into get_pdu_…
erikjohnston 8b919c0
Start the get_pdu cache
erikjohnston e7e2041
ExpiringCache: purge every 1/2 interval
erikjohnston 964bb43
Fix typo in function name
erikjohnston ec84705
Rename _fail_fetch_pdu_cache to _get_pdu_cache
erikjohnston File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright 2015 OpenMarket Ltd | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import logging | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class ExpiringCache(object): | ||
def __init__(self, cache_name, clock, max_len=0, expiry_ms=0, | ||
reset_expiry_on_get=False): | ||
""" | ||
Args: | ||
cache_name (str): Name of this cache, used for logging. | ||
clock (Clock) | ||
max_len (int): Max size of dict. If the dict grows larger than this | ||
then the oldest items get automatically evicted. Default is 0, | ||
which indicates there is no max limit. | ||
expiry_ms (int): How long before an item is evicted from the cache | ||
in milliseconds. Default is 0, indicating items never get | ||
evicted based on time. | ||
reset_expiry_on_get (bool): If true, will reset the expiry time for | ||
an item on access. Defaults to False. | ||
|
||
""" | ||
self._cache_name = cache_name | ||
|
||
self._clock = clock | ||
|
||
self._max_len = max_len | ||
self._expiry_ms = expiry_ms | ||
|
||
self._reset_expiry_on_get = reset_expiry_on_get | ||
|
||
self._cache = {} | ||
|
||
def start(self): | ||
if not self._expiry_ms: | ||
# Don't bother starting the loop if things never expire | ||
return | ||
|
||
def f(): | ||
self._prune_cache() | ||
|
||
self._clock.looping_call(f, self._expiry_ms/2) | ||
|
||
def __setitem__(self, key, value): | ||
now = self._clock.time_msec() | ||
self._cache[key] = _CacheEntry(now, value) | ||
|
||
# Evict if there are now too many items | ||
if self._max_len and len(self._cache.keys()) > self._max_len: | ||
sorted_entries = sorted( | ||
self._cache.items(), | ||
key=lambda k, v: v.time, | ||
) | ||
|
||
for k, _ in sorted_entries[self._max_len:]: | ||
self._cache.pop(k) | ||
|
||
def __getitem__(self, key): | ||
entry = self._cache[key] | ||
|
||
if self._reset_expiry_on_get: | ||
entry.time = self._clock.time_msec() | ||
|
||
return entry.value | ||
|
||
def get(self, key, default=None): | ||
try: | ||
return self[key] | ||
except KeyError: | ||
return default | ||
|
||
def _prune_cache(self): | ||
if not self._expiry_ms: | ||
# zero expiry time means don't expire. This should never get called | ||
# since we have this check in start too. | ||
return | ||
begin_length = len(self._cache) | ||
|
||
now = self._clock.time_msec() | ||
|
||
keys_to_delete = set() | ||
|
||
for key, cache_entry in self._cache.items(): | ||
if now - cache_entry.time > self._expiry_ms: | ||
keys_to_delete.add(key) | ||
|
||
for k in keys_to_delete: | ||
self._cache.pop(k) | ||
|
||
logger.debug( | ||
"[%s] _prune_cache before: %d, after len: %d", | ||
self._cache_name, begin_length, len(self._cache.keys()) | ||
) | ||
|
||
|
||
class _CacheEntry(object): | ||
def __init__(self, time, value): | ||
self.time = time | ||
self.value = value |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might nice to call this something other than "fail_cache" if it caches more than just failures.