2020-12-16 12:41:32 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
2021-04-27 13:13:39 +00:00
|
|
|
# lint: pylint
|
|
|
|
|
|
|
|
"""Abstract base classes for engine request processores.
|
|
|
|
|
|
|
|
"""
|
2020-12-16 12:41:32 +00:00
|
|
|
|
2021-04-13 13:21:53 +00:00
|
|
|
import threading
|
2021-01-17 15:14:16 +00:00
|
|
|
from abc import abstractmethod, ABC
|
2021-04-14 15:23:15 +00:00
|
|
|
from timeit import default_timer
|
2021-04-13 13:21:53 +00:00
|
|
|
|
2020-12-16 12:41:32 +00:00
|
|
|
from searx import logger
|
2021-04-13 13:21:53 +00:00
|
|
|
from searx.engines import settings
|
|
|
|
from searx.network import get_time_for_thread, get_network
|
2021-04-14 15:23:15 +00:00
|
|
|
from searx.metrics import histogram_observe, counter_inc, count_exception, count_error
|
2021-05-05 11:08:54 +00:00
|
|
|
from searx.exceptions import SearxEngineAccessDeniedException, SearxEngineResponseException
|
|
|
|
from searx.utils import get_engine_from_settings
|
2020-12-16 12:41:32 +00:00
|
|
|
|
|
|
|
logger = logger.getChild('searx.search.processor')
|
2021-04-13 13:21:53 +00:00
|
|
|
SUSPENDED_STATUS = {}
|
|
|
|
|
2021-04-27 13:13:39 +00:00
|
|
|
# pylint: disable=missing-function-docstring
|
2021-04-13 13:21:53 +00:00
|
|
|
|
|
|
|
class SuspendedStatus:
|
2021-04-27 13:13:39 +00:00
|
|
|
"""Class to handle suspend state."""
|
2021-04-13 13:21:53 +00:00
|
|
|
|
|
|
|
__slots__ = 'suspend_end_time', 'suspend_reason', 'continuous_errors', 'lock'
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.lock = threading.Lock()
|
|
|
|
self.continuous_errors = 0
|
|
|
|
self.suspend_end_time = 0
|
|
|
|
self.suspend_reason = None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_suspended(self):
|
2021-04-14 15:23:15 +00:00
|
|
|
return self.suspend_end_time >= default_timer()
|
2021-04-13 13:21:53 +00:00
|
|
|
|
|
|
|
def suspend(self, suspended_time, suspend_reason):
|
|
|
|
with self.lock:
|
|
|
|
# update continuous_errors / suspend_end_time
|
|
|
|
self.continuous_errors += 1
|
|
|
|
if suspended_time is None:
|
|
|
|
suspended_time = min(settings['search']['max_ban_time_on_fail'],
|
|
|
|
self.continuous_errors * settings['search']['ban_time_on_fail'])
|
2021-04-14 15:23:15 +00:00
|
|
|
self.suspend_end_time = default_timer() + suspended_time
|
2021-04-13 13:21:53 +00:00
|
|
|
self.suspend_reason = suspend_reason
|
|
|
|
logger.debug('Suspend engine for %i seconds', suspended_time)
|
|
|
|
|
|
|
|
def resume(self):
|
|
|
|
with self.lock:
|
|
|
|
# reset the suspend variables
|
|
|
|
self.continuous_errors = 0
|
|
|
|
self.suspend_end_time = 0
|
|
|
|
self.suspend_reason = None
|
2020-12-16 12:41:32 +00:00
|
|
|
|
|
|
|
|
2021-01-17 15:14:16 +00:00
|
|
|
class EngineProcessor(ABC):
|
2021-04-27 13:13:39 +00:00
|
|
|
"""Base classes used for all types of reqest processores."""
|
2020-12-16 12:41:32 +00:00
|
|
|
|
2021-04-13 13:21:53 +00:00
|
|
|
__slots__ = 'engine', 'engine_name', 'lock', 'suspended_status'
|
|
|
|
|
2020-12-16 12:41:32 +00:00
|
|
|
def __init__(self, engine, engine_name):
|
|
|
|
self.engine = engine
|
|
|
|
self.engine_name = engine_name
|
2021-04-13 13:21:53 +00:00
|
|
|
key = get_network(self.engine_name)
|
|
|
|
key = id(key) if key else self.engine_name
|
|
|
|
self.suspended_status = SUSPENDED_STATUS.setdefault(key, SuspendedStatus())
|
|
|
|
|
2021-05-05 11:08:54 +00:00
|
|
|
def initialize(self):
|
|
|
|
try:
|
|
|
|
self.engine.init(get_engine_from_settings(self.engine_name))
|
|
|
|
except SearxEngineResponseException as exc:
|
|
|
|
logger.warn('%s engine: Fail to initialize // %s', self.engine_name, exc)
|
|
|
|
except Exception: # pylint: disable=broad-except
|
|
|
|
logger.exception('%s engine: Fail to initialize', self.engine_name)
|
|
|
|
else:
|
|
|
|
logger.debug('%s engine: Initialized', self.engine_name)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def has_initialize_function(self):
|
|
|
|
return hasattr(self.engine, 'init')
|
|
|
|
|
2021-04-26 09:12:02 +00:00
|
|
|
def handle_exception(self, result_container, exception_or_message, suspend=False):
|
2021-04-13 13:21:53 +00:00
|
|
|
# update result_container
|
2021-04-26 09:12:02 +00:00
|
|
|
if isinstance(exception_or_message, BaseException):
|
|
|
|
exception_class = exception_or_message.__class__
|
|
|
|
module_name = getattr(exception_class, '__module__', 'builtins')
|
|
|
|
module_name = '' if module_name == 'builtins' else module_name + '.'
|
|
|
|
error_message = module_name + exception_class.__qualname__
|
|
|
|
else:
|
|
|
|
error_message = exception_or_message
|
|
|
|
result_container.add_unresponsive_engine(self.engine_name, error_message)
|
2021-04-13 13:21:53 +00:00
|
|
|
# metrics
|
2021-04-14 15:23:15 +00:00
|
|
|
counter_inc('engine', self.engine_name, 'search', 'count', 'error')
|
2021-04-26 09:12:02 +00:00
|
|
|
if isinstance(exception_or_message, BaseException):
|
|
|
|
count_exception(self.engine_name, exception_or_message)
|
2021-04-13 13:21:53 +00:00
|
|
|
else:
|
2021-04-26 09:12:02 +00:00
|
|
|
count_error(self.engine_name, exception_or_message)
|
2021-04-13 13:21:53 +00:00
|
|
|
# suspend the engine ?
|
|
|
|
if suspend:
|
|
|
|
suspended_time = None
|
2021-04-26 09:12:02 +00:00
|
|
|
if isinstance(exception_or_message, SearxEngineAccessDeniedException):
|
|
|
|
suspended_time = exception_or_message.suspended_time
|
|
|
|
self.suspended_status.suspend(suspended_time, error_message) # pylint: disable=no-member
|
2021-04-13 13:21:53 +00:00
|
|
|
|
|
|
|
def _extend_container_basic(self, result_container, start_time, search_results):
|
|
|
|
# update result_container
|
|
|
|
result_container.extend(self.engine_name, search_results)
|
2021-04-14 15:23:15 +00:00
|
|
|
engine_time = default_timer() - start_time
|
2021-04-13 13:21:53 +00:00
|
|
|
page_load_time = get_time_for_thread()
|
|
|
|
result_container.add_timing(self.engine_name, engine_time, page_load_time)
|
|
|
|
# metrics
|
2021-04-14 15:23:15 +00:00
|
|
|
counter_inc('engine', self.engine_name, 'search', 'count', 'successful')
|
|
|
|
histogram_observe(engine_time, 'engine', self.engine_name, 'time', 'total')
|
|
|
|
if page_load_time is not None:
|
|
|
|
histogram_observe(page_load_time, 'engine', self.engine_name, 'time', 'http')
|
2021-04-13 13:21:53 +00:00
|
|
|
|
|
|
|
def extend_container(self, result_container, start_time, search_results):
|
|
|
|
if getattr(threading.current_thread(), '_timeout', False):
|
|
|
|
# the main thread is not waiting anymore
|
2021-04-26 09:12:02 +00:00
|
|
|
self.handle_exception(result_container, 'timeout', None)
|
2021-04-13 13:21:53 +00:00
|
|
|
else:
|
|
|
|
# check if the engine accepted the request
|
|
|
|
if search_results is not None:
|
|
|
|
self._extend_container_basic(result_container, start_time, search_results)
|
|
|
|
self.suspended_status.resume()
|
|
|
|
|
|
|
|
def extend_container_if_suspended(self, result_container):
|
|
|
|
if self.suspended_status.is_suspended:
|
|
|
|
result_container.add_unresponsive_engine(self.engine_name,
|
|
|
|
self.suspended_status.suspend_reason,
|
|
|
|
suspended=True)
|
|
|
|
return True
|
|
|
|
return False
|
2020-12-16 12:41:32 +00:00
|
|
|
|
|
|
|
def get_params(self, search_query, engine_category):
|
|
|
|
# if paging is not supported, skip
|
|
|
|
if search_query.pageno > 1 and not self.engine.paging:
|
|
|
|
return None
|
|
|
|
|
|
|
|
# if time_range is not supported, skip
|
|
|
|
if search_query.time_range and not self.engine.time_range_support:
|
|
|
|
return None
|
|
|
|
|
|
|
|
params = {}
|
|
|
|
params['category'] = engine_category
|
|
|
|
params['pageno'] = search_query.pageno
|
|
|
|
params['safesearch'] = search_query.safesearch
|
|
|
|
params['time_range'] = search_query.time_range
|
2021-03-25 08:37:37 +00:00
|
|
|
params['engine_data'] = search_query.engine_data.get(self.engine_name, {})
|
2020-12-16 12:41:32 +00:00
|
|
|
|
|
|
|
if hasattr(self.engine, 'language') and self.engine.language:
|
|
|
|
params['language'] = self.engine.language
|
|
|
|
else:
|
|
|
|
params['language'] = search_query.lang
|
|
|
|
return params
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def search(self, query, params, result_container, start_time, timeout_limit):
|
|
|
|
pass
|
2020-12-24 08:28:16 +00:00
|
|
|
|
|
|
|
def get_tests(self):
|
|
|
|
tests = getattr(self.engine, 'tests', None)
|
|
|
|
if tests is None:
|
|
|
|
tests = getattr(self.engine, 'additional_tests', {})
|
|
|
|
tests.update(self.get_default_tests())
|
2021-04-27 13:13:39 +00:00
|
|
|
return tests
|
2020-12-24 08:28:16 +00:00
|
|
|
|
2021-04-27 13:13:39 +00:00
|
|
|
def get_default_tests(self): # pylint: disable=no-self-use
|
2020-12-24 08:28:16 +00:00
|
|
|
return {}
|