(cls, decode_responses=True, max_retries=5, retry_interval=1)
| 118 | |
| 119 | @classmethod |
| 120 | def _init_client(cls, decode_responses=True, max_retries=5, retry_interval=1): |
| 121 | retry_count = 0 |
| 122 | while retry_count < max_retries: |
| 123 | try: |
| 124 | # Get connection parameters from settings or environment |
| 125 | redis_host = os.environ.get("REDIS_HOST", getattr(settings, 'REDIS_HOST', 'localhost')) |
| 126 | redis_port = int(os.environ.get("REDIS_PORT", getattr(settings, 'REDIS_PORT', 6379))) |
| 127 | redis_db = int(os.environ.get("REDIS_DB", getattr(settings, 'REDIS_DB', 0))) |
| 128 | redis_password = os.environ.get("REDIS_PASSWORD", getattr(settings, 'REDIS_PASSWORD', '')) |
| 129 | redis_user = os.environ.get("REDIS_USER", getattr(settings, 'REDIS_USER', '')) |
| 130 | |
| 131 | # Use standardized settings |
| 132 | socket_timeout = getattr(settings, 'REDIS_SOCKET_TIMEOUT', 5) |
| 133 | socket_connect_timeout = getattr(settings, 'REDIS_SOCKET_CONNECT_TIMEOUT', 5) |
| 134 | health_check_interval = getattr(settings, 'REDIS_HEALTH_CHECK_INTERVAL', 30) |
| 135 | socket_keepalive = getattr(settings, 'REDIS_SOCKET_KEEPALIVE', True) |
| 136 | retry_on_timeout = getattr(settings, 'REDIS_RETRY_ON_TIMEOUT', True) |
| 137 | |
| 138 | # TLS params from settings (empty dict when TLS is disabled) |
| 139 | ssl_params = getattr(settings, 'REDIS_SSL_PARAMS', {}) |
| 140 | |
| 141 | # Create Redis client with better defaults |
| 142 | client = redis.Redis( |
| 143 | host=redis_host, |
| 144 | port=redis_port, |
| 145 | db=redis_db, |
| 146 | password=redis_password if redis_password else None, |
| 147 | username=redis_user if redis_user else None, |
| 148 | socket_timeout=socket_timeout, |
| 149 | socket_connect_timeout=socket_connect_timeout, |
| 150 | socket_keepalive=socket_keepalive, |
| 151 | health_check_interval=health_check_interval, |
| 152 | retry_on_timeout=retry_on_timeout, |
| 153 | decode_responses=decode_responses, |
| 154 | **ssl_params |
| 155 | ) |
| 156 | |
| 157 | # Validate connection with ping |
| 158 | client.ping() |
| 159 | |
| 160 | # Disable persistence on first connection - improves performance |
| 161 | # Only try to disable if not in a read-only environment |
| 162 | try: |
| 163 | client.config_set('save', '') # Disable RDB snapshots |
| 164 | client.config_set('appendonly', 'no') # Disable AOF logging |
| 165 | |
| 166 | # Disable protected mode when in debug mode |
| 167 | if os.environ.get('DISPATCHARR_DEBUG', '').lower() == 'true': |
| 168 | client.config_set('protected-mode', 'no') # Disable protected mode in debug |
| 169 | logger.warning("Redis protected mode disabled for debug environment") |
| 170 | |
| 171 | logger.trace("Redis persistence disabled for better performance") |
| 172 | except redis.exceptions.ResponseError as e: |
| 173 | # Improve error handling for Redis configuration errors |
| 174 | if "OOM" in str(e): |
| 175 | logger.error(f"Redis OOM during configuration: {e}") |
| 176 | # Try to increase maxmemory as an emergency measure |
| 177 | try: |
no test coverage detected