diff options
author | Erik Johnston <erik@matrix.org> | 2018-05-25 10:53:43 +0100 |
---|---|---|
committer | Erik Johnston <erik@matrix.org> | 2018-05-25 10:53:43 +0100 |
commit | bcc9e7f7775e7b605bc688eff70b91e4a5577d3a (patch) | |
tree | b12587e6acc779b87df8352426194bfd4e1af66e /synapse/util | |
parent | Merge branch 'develop' of github.com:matrix-org/synapse into erikj/room_chunks (diff) | |
parent | Merge pull request #3283 from NotAFile/py3-state (diff) | |
download | synapse-bcc9e7f7775e7b605bc688eff70b91e4a5577d3a.tar.xz |
Merge branch 'develop' of github.com:matrix-org/synapse into erikj/room_chunks
Diffstat (limited to 'synapse/util')
-rw-r--r-- | synapse/util/__init__.py | 18 | ||||
-rw-r--r-- | synapse/util/caches/__init__.py | 7 |
2 files changed, 24 insertions, 1 deletions
diff --git a/synapse/util/__init__.py b/synapse/util/__init__.py index 814a7bf71b..fc11e26623 100644 --- a/synapse/util/__init__.py +++ b/synapse/util/__init__.py @@ -20,6 +20,8 @@ from twisted.internet import defer, reactor, task import time import logging +from itertools import islice + logger = logging.getLogger(__name__) @@ -79,3 +81,19 @@ class Clock(object): except Exception: if not ignore_errs: raise + + +def batch_iter(iterable, size): + """batch an iterable up into tuples with a maximum size + + Args: + iterable (iterable): the iterable to slice + size (int): the maximum batch size + + Returns: + an iterator over the chunks + """ + # make sure we can deal with iterables like lists too + sourceiter = iter(iterable) + # call islice until it returns an empty tuple + return iter(lambda: tuple(islice(sourceiter, size)), ()) diff --git a/synapse/util/caches/__init__.py b/synapse/util/caches/__init__.py index 4adae96681..329ccbb866 100644 --- a/synapse/util/caches/__init__.py +++ b/synapse/util/caches/__init__.py @@ -16,6 +16,9 @@ import synapse.metrics import os +from six.moves import intern +import six + CACHE_SIZE_FACTOR = float(os.environ.get("SYNAPSE_CACHE_FACTOR", 0.5)) metrics = synapse.metrics.get_metrics_for("synapse.util.caches") @@ -66,7 +69,9 @@ def intern_string(string): return None try: - string = string.encode("ascii") + if six.PY2: + string = string.encode("ascii") + return intern(string) except UnicodeEncodeError: return string |