summary refs log tree commit diff
path: root/synapse/util
diff options
context:
space:
mode:
authorAmber Brown <hawkowl@atleastfornow.net>2018-05-28 18:57:23 +1000
committerAmber Brown <hawkowl@atleastfornow.net>2018-05-28 18:57:23 +1000
commit754826a8305b3f32a45367cb6bd8bb26bd489a6b (patch)
treec5aba5e1e720474b3f94d15bd9202bda0b120e20 /synapse/util
parentpepeightttt (diff)
parentMerge pull request #3288 from matrix-org/rav/no_spam_guests (diff)
downloadsynapse-754826a8305b3f32a45367cb6bd8bb26bd489a6b.tar.xz
Merge remote-tracking branch 'origin/develop' into 3218-official-prom
Diffstat (limited to 'synapse/util')
-rw-r--r--synapse/util/__init__.py18
-rw-r--r--synapse/util/caches/__init__.py7
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 e0c22df249..d968b71e7a 100644
--- a/synapse/util/caches/__init__.py
+++ b/synapse/util/caches/__init__.py
@@ -17,6 +17,9 @@ from prometheus_client.core import Gauge, REGISTRY, GaugeMetricFamily
 
 import os
 
+from six.moves import intern
+import six
+
 CACHE_SIZE_FACTOR = float(os.environ.get("SYNAPSE_CACHE_FACTOR", 0.5))
 
 caches_by_name = {}
@@ -110,7 +113,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