Browse Source

Add a lower bound for TTL on well known results.

It costs both us and the remote server for us to fetch the well known
for every single request we send, so we add a minimum cache period. This
is set to 5m so that we still honour the basic premise of "refetch
frequently".
Erik Johnston 4 years ago
parent
commit
af9f1c0764

+ 4 - 0
synapse/http/federation/matrix_federation_agent.py

@@ -47,6 +47,9 @@ WELL_KNOWN_INVALID_CACHE_PERIOD = 1 * 3600
 # cap for .well-known cache period
 WELL_KNOWN_MAX_CACHE_PERIOD = 48 * 3600
 
+# lower bound for .well-known cache period
+WELL_KNOWN_MIN_CACHE_PERIOD = 5 * 60
+
 logger = logging.getLogger(__name__)
 well_known_cache = TTLCache("well-known")
 
@@ -356,6 +359,7 @@ class MatrixFederationAgent(object):
             cache_period += random.uniform(0, WELL_KNOWN_DEFAULT_CACHE_PERIOD_JITTER)
         else:
             cache_period = min(cache_period, WELL_KNOWN_MAX_CACHE_PERIOD)
+            cache_period = max(cache_period, WELL_KNOWN_MIN_CACHE_PERIOD)
 
         return (result, cache_period)
 

+ 2 - 2
tests/http/federation/test_matrix_federation_agent.py

@@ -953,7 +953,7 @@ class MatrixFederationAgentTests(TestCase):
         well_known_server = self._handle_well_known_connection(
             client_factory,
             expected_sni=b"testserv",
-            response_headers={b"Cache-Control": b"max-age=10"},
+            response_headers={b"Cache-Control": b"max-age=1000"},
             content=b'{ "m.server": "target-server" }',
         )
 
@@ -969,7 +969,7 @@ class MatrixFederationAgentTests(TestCase):
         self.assertEqual(r, b"target-server")
 
         # expire the cache
-        self.reactor.pump((10.0,))
+        self.reactor.pump((1000.0,))
 
         # now it should connect again
         fetch_d = self.do_get_well_known(b"testserv")