diff --git a/engine/apps/integrations/mixins/alert_channel_defining_mixin.py b/engine/apps/integrations/mixins/alert_channel_defining_mixin.py index b5128d3a..c621412f 100644 --- a/engine/apps/integrations/mixins/alert_channel_defining_mixin.py +++ b/engine/apps/integrations/mixins/alert_channel_defining_mixin.py @@ -5,8 +5,6 @@ from django.core import serializers from django.core.cache import cache from django.core.exceptions import PermissionDenied from django.db import OperationalError -from django_redis.exceptions import ConnectionInterrupted as RedisConnectionInterrupted # type: ignore -from redis.exceptions import ConnectionError as RedisConnectionError from apps.user_management.exceptions import OrganizationMovedException @@ -35,29 +33,21 @@ class AlertChannelDefiningMixin(object): try: # Trying to define from short-term cache cache_key_short_term = self.CACHE_KEY_SHORT_TERM + "_" + str(kwargs["alert_channel_key"]) - try: - cached_alert_receive_channel_raw = cache.get(cache_key_short_term) - except RedisConnectionError: - logger.error("Skip reading AlertReceiveChannel from cache as Redis is not available") - cached_alert_receive_channel_raw = None - + cached_alert_receive_channel_raw = cache.get(cache_key_short_term) if cached_alert_receive_channel_raw is not None: alert_receive_channel = next(serializers.deserialize("json", cached_alert_receive_channel_raw)).object if alert_receive_channel is None: # Trying to define channel from DB alert_receive_channel = AlertReceiveChannel.objects.get(token=kwargs["alert_channel_key"]) - try: - # Update short term cache - serialized = serializers.serialize("json", [alert_receive_channel]) - cache.set(cache_key_short_term, serialized, self.CACHE_SHORT_TERM_TIMEOUT) + # Update short term cache + serialized = serializers.serialize("json", [alert_receive_channel]) + cache.set(cache_key_short_term, serialized, self.CACHE_SHORT_TERM_TIMEOUT) - # Update cached channels - if cache.get(self.CACHE_DB_FALLBACK_OBSOLETE_KEY) is None: - cache.set(self.CACHE_DB_FALLBACK_OBSOLETE_KEY, True, self.CACHE_DB_FALLBACK_REFRESH_INTERVAL) - self.update_alert_receive_channel_cache() - except (RedisConnectionError, RedisConnectionInterrupted): - logger.error("Skip updating AlertReceiveChannel cache as Redis is not available") + # Update cached channels + if cache.get(self.CACHE_DB_FALLBACK_OBSOLETE_KEY) is None: + cache.set(self.CACHE_DB_FALLBACK_OBSOLETE_KEY, True, self.CACHE_DB_FALLBACK_REFRESH_INTERVAL) + self.update_alert_receive_channel_cache() except AlertReceiveChannel.DoesNotExist: raise PermissionDenied("Integration key was not found. Permission denied.") diff --git a/engine/apps/integrations/mixins/ratelimit_mixin.py b/engine/apps/integrations/mixins/ratelimit_mixin.py index 58bda952..808b4276 100644 --- a/engine/apps/integrations/mixins/ratelimit_mixin.py +++ b/engine/apps/integrations/mixins/ratelimit_mixin.py @@ -2,14 +2,12 @@ import logging from abc import ABC, abstractmethod from functools import wraps -from django.conf import settings from django.core.cache import cache from django.http import HttpRequest, HttpResponse from django.views import View from ratelimit import ALL from ratelimit.exceptions import Ratelimited from ratelimit.utils import is_ratelimited -from redis.exceptions import ConnectionError as RedisConnectionError from apps.integrations.tasks import start_notify_about_integration_ratelimit @@ -56,16 +54,9 @@ def ratelimit(group=None, key=None, rate=None, method=ALL, block=False, reason=N request.limited = getattr(request, "limited", False) was_limited_before = request.limited - # Allow requests when redis cache backend fails and RATELIMIT_FAIL_OPEN setting is true - try: - ratelimited = is_ratelimited( - request=request, group=group, fn=fn, key=key, rate=rate, method=method, increment=True - ) - except RedisConnectionError as e: - if settings.RATELIMIT_FAIL_OPEN: - ratelimited = False - else: - raise e + ratelimited = is_ratelimited( + request=request, group=group, fn=fn, key=key, rate=rate, method=method, increment=True + ) # We need to know if it's the first ratelimited request for notification purposes. request.is_first_rate_limited_request = getattr(request, "is_first_rate_limited_request", False) diff --git a/engine/engine/views.py b/engine/engine/views.py index dca68143..7f3e2f5d 100644 --- a/engine/engine/views.py +++ b/engine/engine/views.py @@ -1,15 +1,10 @@ -import logging - from django.conf import settings from django.core.cache import cache from django.http import HttpResponse, JsonResponse from django.views.generic import View -from redis.exceptions import ConnectionError as RedisConnectionError from apps.integrations.mixins import AlertChannelDefiningMixin -logger = logging.getLogger(__name__) - class HealthCheckView(View): """ @@ -48,12 +43,11 @@ class StartupProbeView(View): dangerously_bypass_middlewares = True def get(self, request): - try: - if cache.get(AlertChannelDefiningMixin.CACHE_KEY_DB_FALLBACK) is None: - AlertChannelDefiningMixin().update_alert_receive_channel_cache() + if cache.get(AlertChannelDefiningMixin.CACHE_KEY_DB_FALLBACK) is None: + AlertChannelDefiningMixin().update_alert_receive_channel_cache() - except RedisConnectionError: - logger.error("Skip updating AlertReceiveChannel cache as Redis is not available") + cache.set("healthcheck", "healthcheck", 30) # Checking cache connectivity + assert cache.get("healthcheck") == "healthcheck" return HttpResponse("Ok") diff --git a/engine/settings/base.py b/engine/settings/base.py index 17cba72b..50eee03d 100644 --- a/engine/settings/base.py +++ b/engine/settings/base.py @@ -838,5 +838,3 @@ ZVONOK_POSTBACK_USER_CHOICE = os.getenv("ZVONOK_POSTBACK_USER_CHOICE", None) ZVONOK_POSTBACK_USER_CHOICE_ACK = os.getenv("ZVONOK_POSTBACK_USER_CHOICE_ACK", None) DETACHED_INTEGRATIONS_SERVER = getenv_boolean("DETACHED_INTEGRATIONS_SERVER", default=False) - -RATELIMIT_FAIL_OPEN = getenv_boolean("RATELIMIT_FAIL_OPEN", default=True)