Limit the number of prev_events of new events

This commit is contained in:
Erik Johnston 2016-11-08 11:02:29 +00:00
parent d24197bead
commit eeda4e618c
1 changed files with 13 additions and 0 deletions

View File

@ -34,6 +34,7 @@ from ._base import BaseHandler
from canonicaljson import encode_canonical_json from canonicaljson import encode_canonical_json
import logging import logging
import random
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -415,6 +416,18 @@ class MessageHandler(BaseHandler):
builder.room_id, builder.room_id,
) )
# We want to limit the max number of prev events we point to in our
# new event
if len(latest_ret) > 10:
# Sort by reverse depth, so we point to the most recent.
latest_ret.sort(key=lambda a: -a[2])
new_latest_ret = latest_ret[:5]
# We also randomly point to some of the older events, to make
# sure that we don't completely ignore the older events.
new_latest_ret.extend(random.sample(latest_ret, 5))
latest_ret = new_latest_ret
if latest_ret: if latest_ret:
depth = max([d for _, _, d in latest_ret]) + 1 depth = max([d for _, _, d in latest_ret]) + 1
else: else: