From e4be6a98e8263983c96b696bc63227cf259049f5 Mon Sep 17 00:00:00 2001 From: Hugh Rundle Date: Fri, 7 Jan 2022 13:13:56 +1100 Subject: [PATCH 1/3] do not cache registration form Fixes #1777 Caching the Landing view also caches the registration form, including the CSRF value. This moves the caching into the recently reviewed books landing template which is presumably what we're trying to cache here, instead of caching the whole view. NOTE: this fixes the problem with registration, I haven't done enough testing to be sure it actually still caches the recent reviews data. --- bookwyrm/templates/landing/landing.html | 4 +++- bookwyrm/views/landing/landing.py | 1 - 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/bookwyrm/templates/landing/landing.html b/bookwyrm/templates/landing/landing.html index d13cd582a..759e8c619 100644 --- a/bookwyrm/templates/landing/landing.html +++ b/bookwyrm/templates/landing/landing.html @@ -1,11 +1,13 @@ {% extends 'landing/layout.html' %} {% load i18n %} +{% load cache %} {% block panel %}

{% trans "Recent Books" %}

+{% cache 60 * 60 %}
@@ -46,5 +48,5 @@
- +{% endcache %} {% endblock %} diff --git a/bookwyrm/views/landing/landing.py b/bookwyrm/views/landing/landing.py index 74b5ee513..5839731d5 100644 --- a/bookwyrm/views/landing/landing.py +++ b/bookwyrm/views/landing/landing.py @@ -33,7 +33,6 @@ class Home(View): class Landing(View): """preview of recently reviewed books""" - @method_decorator(cache_page(60 * 60), name="dispatch") def get(self, request): """tiled book activity page""" data = { From a1e3ef1c79c0d9ac5165d7562a9203eb97e8ccfe Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Thu, 6 Jan 2022 18:47:14 -0800 Subject: [PATCH 2/3] Fixes pylint complaint --- bookwyrm/views/landing/landing.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/bookwyrm/views/landing/landing.py b/bookwyrm/views/landing/landing.py index 5839731d5..c8bba0664 100644 --- a/bookwyrm/views/landing/landing.py +++ b/bookwyrm/views/landing/landing.py @@ -1,8 +1,6 @@ """ non-interactive pages """ from django.template.response import TemplateResponse from django.views import View -from django.utils.decorators import method_decorator -from django.views.decorators.cache import cache_page from bookwyrm import forms from bookwyrm.views import helpers From b18c69e186b4fea32bbdec6a05d8ab5c210c7d12 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Fri, 7 Jan 2022 07:42:05 -0800 Subject: [PATCH 3/3] Make search timeouts configurable --- .env.dev.example | 7 +++++++ .env.prod.example | 7 +++++++ bookwyrm/connectors/abstract_connector.py | 5 +++-- bookwyrm/connectors/connector_manager.py | 4 ++-- bookwyrm/settings.py | 6 ++++++ 5 files changed, 25 insertions(+), 4 deletions(-) diff --git a/.env.dev.example b/.env.dev.example index b65c1b023..9aac77a54 100644 --- a/.env.dev.example +++ b/.env.dev.example @@ -16,6 +16,7 @@ DEFAULT_LANGUAGE="English" MEDIA_ROOT=images/ +# Database configuration PGPORT=5432 POSTGRES_PASSWORD=securedbypassword123 POSTGRES_USER=fedireads @@ -32,10 +33,12 @@ REDIS_ACTIVITY_PASSWORD=redispassword345 REDIS_BROKER_PORT=6379 REDIS_BROKER_PASSWORD=redispassword123 +# Monitoring for celery FLOWER_PORT=8888 FLOWER_USER=mouse FLOWER_PASSWORD=changeme +# Email config EMAIL_HOST=smtp.mailgun.org EMAIL_PORT=587 EMAIL_HOST_USER=mail@your.domain.here @@ -43,6 +46,10 @@ EMAIL_HOST_PASSWORD=emailpassword123 EMAIL_USE_TLS=true EMAIL_USE_SSL=false +# Query timeouts +SEARCH_TIMEOUT=15 +QUERY_TIMEOUT=5 + # Thumbnails Generation ENABLE_THUMBNAIL_GENERATION=false diff --git a/.env.prod.example b/.env.prod.example index 2e0ced5e8..603fb2f20 100644 --- a/.env.prod.example +++ b/.env.prod.example @@ -16,6 +16,7 @@ DEFAULT_LANGUAGE="English" MEDIA_ROOT=images/ +# Database configuration PGPORT=5432 POSTGRES_PASSWORD=securedbypassword123 POSTGRES_USER=fedireads @@ -32,10 +33,12 @@ REDIS_ACTIVITY_PASSWORD=redispassword345 REDIS_BROKER_PORT=6379 REDIS_BROKER_PASSWORD=redispassword123 +# Monitoring for celery FLOWER_PORT=8888 FLOWER_USER=mouse FLOWER_PASSWORD=changeme +# Email config EMAIL_HOST=smtp.mailgun.org EMAIL_PORT=587 EMAIL_HOST_USER=mail@your.domain.here @@ -43,6 +46,10 @@ EMAIL_HOST_PASSWORD=emailpassword123 EMAIL_USE_TLS=true EMAIL_USE_SSL=false +# Query timeouts +SEARCH_TIMEOUT=15 +QUERY_TIMEOUT=5 + # Thumbnails Generation ENABLE_THUMBNAIL_GENERATION=false diff --git a/bookwyrm/connectors/abstract_connector.py b/bookwyrm/connectors/abstract_connector.py index 20a175264..5ed57df1f 100644 --- a/bookwyrm/connectors/abstract_connector.py +++ b/bookwyrm/connectors/abstract_connector.py @@ -35,7 +35,7 @@ class AbstractMinimalConnector(ABC): for field in self_fields: setattr(self, field, getattr(info, field)) - def search(self, query, min_confidence=None, timeout=5): + def search(self, query, min_confidence=None, timeout=settings.QUERY_TIMEOUT): """free text search""" params = {} if min_confidence: @@ -52,12 +52,13 @@ class AbstractMinimalConnector(ABC): results.append(self.format_search_result(doc)) return results - def isbn_search(self, query): + def isbn_search(self, query, timeout=settings.QUERY_TIMEOUT): """isbn search""" params = {} data = self.get_search_data( f"{self.isbn_search_url}{query}", params=params, + timeout=timeout, ) results = [] diff --git a/bookwyrm/connectors/connector_manager.py b/bookwyrm/connectors/connector_manager.py index 45530cd60..3bdd5cb41 100644 --- a/bookwyrm/connectors/connector_manager.py +++ b/bookwyrm/connectors/connector_manager.py @@ -11,6 +11,7 @@ from django.db.models import signals from requests import HTTPError from bookwyrm import book_search, models +from bookwyrm.settings import SEARCH_TIMEOUT from bookwyrm.tasks import app logger = logging.getLogger(__name__) @@ -30,7 +31,6 @@ def search(query, min_confidence=0.1, return_first=False): isbn = re.sub(r"[\W_]", "", query) maybe_isbn = len(isbn) in [10, 13] # ISBN10 or ISBN13 - timeout = 15 start_time = datetime.now() for connector in get_connectors(): result_set = None @@ -62,7 +62,7 @@ def search(query, min_confidence=0.1, return_first=False): "results": result_set, } ) - if (datetime.now() - start_time).seconds >= timeout: + if (datetime.now() - start_time).seconds >= SEARCH_TIMEOUT: break if return_first: diff --git a/bookwyrm/settings.py b/bookwyrm/settings.py index 5b69e3782..cacccf783 100644 --- a/bookwyrm/settings.py +++ b/bookwyrm/settings.py @@ -119,6 +119,12 @@ STREAMS = [ {"key": "books", "name": _("Books Timeline"), "shortname": _("Books")}, ] +# Search configuration +# total time in seconds that the instance will spend searching connectors +SEARCH_TIMEOUT = int(env("SEARCH_TIMEOUT", 15)) +# timeout for a query to an individual connector +QUERY_TIMEOUT = int(env("QUERY_TIMEOUT", 5)) + # Redis cache backend if env("USE_DUMMY_CACHE", False): CACHES = {