geminispace.info

Unnamed repository; edit this file 'description' to name the repository.
git clone git://code.clttr.info/geminispace.info.git
Log | Files | Refs | README | LICENSE

commit 759c0493b08f7675a2943be9d93cb8b066bff71c
parent 409d9c14076a70495484ee9df304afa31c8132f8
Author: ugla <ugla@u8.is>
Date:   Sat, 26 Dec 2020 18:30:35 +0100

Defer search requests to threads

Diffstat:
Mserve/views.py | 68++++++++++++++++++++++++++++++++++++++------------------------------
1 file changed, 38 insertions(+), 30 deletions(-)

diff --git a/serve/views.py b/serve/views.py @@ -7,6 +7,8 @@ from urllib.parse import quote, unquote import jinja2 from jetforce import Request, Response, Status, JetforceApplication +from twisted.internet.threads import deferToThread + from . import constants from .models import ( compute_verbose, @@ -202,36 +204,42 @@ def index(request): @app.route("/search(/\d+)?") def search(request): if request.query: - verbose = compute_verbose(request.path) - requested_page = compute_requested_results_page(request.path) - num_results, results = gus.search_index(request.query, requested_page) - if num_results > 0: - num_pages = math.ceil(num_results / 10) - current_page = min(requested_page, num_pages) - if num_results == 0: - current_page = 0 - body = render_template( - "search.gmi", - query=request.query, - quoted_query=quote(request.query), - verbose=verbose, - num_results=num_results, - results=results, - current_page=current_page, - num_pages=num_pages, - index_modification_time=gus.statistics["index_modification_time"], - quote=random.choice(constants.QUOTE_BANK), - ) - else: - search_suggestions = gus.get_search_suggestions(request.query) - body = render_template( - "search_suggestions.gmi", - query=request.query, - search_suggestions=search_suggestions, - index_modification_time=gus.statistics["index_modification_time"], - quote=random.choice(constants.QUOTE_BANK), - ) - return Response(Status.SUCCESS, "text/gemini", body) + def perform_search(): + verbose = compute_verbose(request.path) + requested_page = compute_requested_results_page(request.path) + num_results, results = gus.search_index(request.query, requested_page) + if num_results > 0: + num_pages = math.ceil(num_results / 10) + current_page = min(requested_page, num_pages) + if num_results == 0: + current_page = 0 + body = render_template( + "search.gmi", + query=request.query, + quoted_query=quote(request.query), + verbose=verbose, + num_results=num_results, + results=results, + current_page=current_page, + num_pages=num_pages, + index_modification_time=gus.statistics["index_modification_time"], + quote=random.choice(constants.QUOTE_BANK), + ) + else: + search_suggestions = gus.get_search_suggestions(request.query) + body = render_template( + "search_suggestions.gmi", + query=request.query, + search_suggestions=search_suggestions, + index_modification_time=gus.statistics["index_modification_time"], + quote=random.choice(constants.QUOTE_BANK), + ) + return body + + def deferred_search(): + yield deferToThread(perform_search) + + return Response(Status.SUCCESS, "text/gemini", deferred_search()) else: return Response(Status.INPUT, "Search query")