From 692cf8919569f9333aab63aa3b95d583d468af70 Mon Sep 17 00:00:00 2001 From: kaniini Date: Sun, 18 Nov 2018 14:20:17 +0000 Subject: [PATCH] actor: use LFU cache to break cycles --- relay/actor.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/relay/actor.py b/relay/actor.py index f1a4c6d..28d83ea 100644 --- a/relay/actor.py +++ b/relay/actor.py @@ -164,19 +164,27 @@ def distill_object_id(activity): async def handle_relay(actor, data, request): + global CACHE + object_id = distill_object_id(data) + if object_id in CACHE: + logging.debug('>> already relayed %r as %r', object_id, CACHE[object_id]) + return + # don't relay mastodon announces -- causes LRP fake direction issues if data['type'] == 'Announce' and len(data.get('cc', [])) > 0: return + activity_id = "https://{}/activities/{}".format(request.host, uuid.uuid4()) + message = { "@context": "https://www.w3.org/ns/activitystreams", "type": "Announce", "to": ["https://{}/actor/followers".format(request.host)], "actor": "https://{}/actor".format(request.host), "object": object_id, - "id": "https://{}/activities/{}".format(request.host, uuid.uuid4()) + "id": activity_id } logging.debug('>> relay: %r', message) @@ -186,6 +194,8 @@ async def handle_relay(actor, data, request): futures = [push_message_to_actor({'inbox': inbox}, message, 'https://{}/actor#main-key'.format(request.host)) for inbox in inboxes] asyncio.ensure_future(asyncio.gather(*futures)) + CACHE[object_id] = activity_id + async def handle_follow(actor, data, request): global DATABASE