From 5c3fb82ebb1a6ecfe1bfcdb69f51957f272daee0 Mon Sep 17 00:00:00 2001 From: rimu <3310831+rimu@users.noreply.github.com> Date: Tue, 9 Apr 2024 08:14:25 +1200 Subject: [PATCH] speed up redis-based dup detection by avoiding touching the DB in before_request() #135 --- app/activitypub/routes.py | 1 + pyfedi.py | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/app/activitypub/routes.py b/app/activitypub/routes.py index 310e6107..8a55f262 100644 --- a/app/activitypub/routes.py +++ b/app/activitypub/routes.py @@ -374,6 +374,7 @@ def shared_inbox(): redis_client.set(request_json['id'], 1, ex=90) # Save the activity ID into redis, to avoid duplicate activities that Lemmy sometimes sends activity_log.activity_id = request_json['id'] + g.site = Site.query.get(1) # g.site is not initialized by @app.before_request when request.path == '/inbox' if g.site.log_activitypub_json: activity_log.activity_json = json.dumps(request_json) activity_log.result = 'processing' diff --git a/pyfedi.py b/pyfedi.py index 410baaec..8efd05bb 100644 --- a/pyfedi.py +++ b/pyfedi.py @@ -53,7 +53,8 @@ with app.app_context(): def before_request(): session['nonce'] = gibberish() g.locale = str(get_locale()) - g.site = Site.query.get(1) + if request.path != '/inbox' and not request.path.startswith('/static/'): # do not load g.site on shared inbox, to increase chance of duplicate detection working properly + g.site = Site.query.get(1) if current_user.is_authenticated: current_user.last_seen = datetime.utcnow() current_user.email_unread_sent = False