# HG changeset patch # User RhodeCode Admin # Date 2024-06-13 19:35:28 # Node ID 2c57bb5b57b490016186de1ac7856914a76713aa # Parent 5745b11f4dd3a56519e6982b050f2efe596bd97d core: synced vendor/ext_json with ce for better compatability diff --git a/vcsserver/lib/_vendor/jsonlogger/__init__.py b/vcsserver/lib/_vendor/jsonlogger/__init__.py --- a/vcsserver/lib/_vendor/jsonlogger/__init__.py +++ b/vcsserver/lib/_vendor/jsonlogger/__init__.py @@ -3,7 +3,6 @@ This library is provided to allow standa to output log data as JSON formatted strings ''' import logging -import json import re from datetime import date, datetime, time, tzinfo, timedelta import traceback @@ -13,13 +12,8 @@ from inspect import istraceback from collections import OrderedDict - -def _inject_req_id(record, *args, **kwargs): - return record - - -ExceptionAwareFormatter = logging.Formatter - +from ...logging_formatter import _inject_req_id, ExceptionAwareFormatter +from ...ext_json import sjson as json ZERO = timedelta(0) HOUR = timedelta(hours=1) diff --git a/vcsserver/lib/_vendor/redis_lock/__init__.py b/vcsserver/lib/_vendor/redis_lock/__init__.py --- a/vcsserver/lib/_vendor/redis_lock/__init__.py +++ b/vcsserver/lib/_vendor/redis_lock/__init__.py @@ -111,11 +111,12 @@ class Lock: extend_script = None reset_script = None reset_all_script = None + blocking = None _lock_renewal_interval: float _lock_renewal_thread: Union[threading.Thread, None] - def __init__(self, redis_client, name, expire=None, id=None, auto_renewal=False, strict=True, signal_expire=1000): + def __init__(self, redis_client, name, expire=None, id=None, auto_renewal=False, strict=True, signal_expire=1000, blocking=True): """ :param redis_client: An instance of :class:`~StrictRedis`. @@ -143,6 +144,9 @@ class Lock: If set ``True`` then the ``redis_client`` needs to be an instance of ``redis.StrictRedis``. :param signal_expire: Advanced option to override signal list expiration in milliseconds. Increase it for very slow clients. Default: ``1000``. + :param blocking: + Boolean value specifying whether lock should be blocking or not. + Used in `__enter__` method. """ if strict and not isinstance(redis_client, StrictRedis): raise ValueError("redis_client must be instance of StrictRedis. " @@ -179,6 +183,8 @@ class Lock: else None) self._lock_renewal_thread = None + self.blocking = blocking + self.register_scripts(redis_client) @classmethod @@ -342,9 +348,11 @@ class Lock: loggers["refresh.exit"].debug("Renewal thread for Lock(%r) exited.", self._name) def __enter__(self): - acquired = self.acquire(blocking=True) + acquired = self.acquire(blocking=self.blocking) if not acquired: - raise AssertionError(f"Lock({self._name}) wasn't acquired, but blocking=True was used!") + if self.blocking: + raise AssertionError(f"Lock({self._name}) wasn't acquired, but blocking=True was used!") + raise NotAcquired(f"Lock({self._name}) is not acquired or it already expired.") return self def __exit__(self, exc_type=None, exc_value=None, traceback=None): diff --git a/vcsserver/lib/_vendor/statsd/__init__.py b/vcsserver/lib/_vendor/statsd/__init__.py --- a/vcsserver/lib/_vendor/statsd/__init__.py +++ b/vcsserver/lib/_vendor/statsd/__init__.py @@ -14,7 +14,7 @@ log = logging.getLogger('rhodecode.stats def statsd_config(config, prefix='statsd.'): _config = {} - for key in config.keys(): + for key in list(config.keys()): if key.startswith(prefix): _config[key[len(prefix):]] = config[key] return _config @@ -24,9 +24,10 @@ def client_from_config(configuration, pr from pyramid.settings import asbool _config = statsd_config(configuration, prefix) + statsd_flag = _config.get('enabled') statsd_enabled = asbool(_config.pop('enabled', False)) if not statsd_enabled: - log.debug('statsd client not enabled by statsd.enabled = flag, skipping...') + log.debug('statsd client not enabled by statsd.enabled = %s flag, skipping...', statsd_flag) return host = _config.pop('statsd_host', HOST) diff --git a/vcsserver/lib/ext_json.py b/vcsserver/lib/ext_json.py --- a/vcsserver/lib/ext_json.py +++ b/vcsserver/lib/ext_json.py @@ -1,2 +1,14 @@ -# use orjson by default -import orjson as json +import json as stdlib_json + +try: + # we keep simplejson for having dump functionality still + # orjson doesn't support it + import simplejson as sjson +except ImportError: + sjson = stdlib_json + +try: + import orjson + import orjson as json +except ImportError: + json = stdlib_json diff --git a/vcsserver/lib/logging_formatter.py b/vcsserver/lib/logging_formatter.py --- a/vcsserver/lib/logging_formatter.py +++ b/vcsserver/lib/logging_formatter.py @@ -37,11 +37,19 @@ COLORS = { } +def _inject_req_id(record, *args, **kwargs): + return record + + +class ExceptionAwareFormatter(logging.Formatter): + pass + + class ColorFormatter(logging.Formatter): def format(self, record): """ - Change record's levelname to use with COLORS enum + Changes record's levelname to use with COLORS enum """ def_record = super().format(record) @@ -51,3 +59,5 @@ class ColorFormatter(logging.Formatter): colored_record = ''.join([start, def_record, end]) return colored_record + +