From b951f35572119749eee0160b70b5b76382223103 Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Wed, 28 Nov 2018 20:09:39 +0000 Subject: [PATCH] Reduce size of fed transaction IDs --- synapse/federation/transaction_queue.py | 21 +++++++++++++++++++-- synapse/http/endpoint.py | 7 +++++-- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/synapse/federation/transaction_queue.py b/synapse/federation/transaction_queue.py index ba3b3fd2f9..00bbf01620 100644 --- a/synapse/federation/transaction_queue.py +++ b/synapse/federation/transaction_queue.py @@ -17,6 +17,7 @@ import logging import random import json import opentracing +import string from six import itervalues @@ -135,7 +136,7 @@ class TransactionQueue(object): self.last_device_list_stream_id_by_dest = {} # HACK to get unique tx id - self._next_txn_id = int(self.clock.time_msec()) + self._next_txn_id = 1 self._order = 1 @@ -486,7 +487,7 @@ class TransactionQueue(object): pending_pdus = [] while True: - txn_id = str(self._next_txn_id) + txn_id = _encode_id(self._next_txn_id) self._next_txn_id += 1 for s in pdu_spans.values(): @@ -860,3 +861,19 @@ class TransactionQueue(object): new_destinations = set(pdu.unsigned.get("destinations", [])) new_destinations.discard(destination) yield self._send_pdu(pdu, list(new_destinations), span) + + +def _numberToBase(n, b): + if n == 0: + return [0] + digits = [] + while n: + digits.append(int(n % b)) + n //= b + return digits[::-1] + + +def _encode_id(i): + digits = string.digits + string.ascii_letters + val_slice = _numberToBase(i, len(digits)) + return "".join(digits[x] for x in val_slice) diff --git a/synapse/http/endpoint.py b/synapse/http/endpoint.py index 91025037a3..df528113a4 100644 --- a/synapse/http/endpoint.py +++ b/synapse/http/endpoint.py @@ -194,8 +194,11 @@ class _WrappedConnection(object): # In Twisted >18.4; the TLS connection will be None if it has closed # which will make abortConnection() throw. Check that the TLS connection # is not None before trying to close it. - if self.transport.getHandle() is not None: - self.transport.abortConnection() + try: + if self.transport.getHandle() is not None: + self.transport.abortConnection() + except: + logger.warning("Failed to abort connection") def request(self, request): self.last_request = time.time()