From 03f32d44e5f9b003986278e85ff91907c792a889 Mon Sep 17 00:00:00 2001 From: Deimos Date: Tue, 20 Nov 2018 16:44:22 -0700 Subject: [PATCH] Replace StrictRedis usages with Redis As of version 3.0, the redis-py package no longer has a distinction between its Redis and StrictRedis classes, and both behave the same (StrictRedis is just an alias for Redis). This would have continued working as-is, but we might as well switch it back to the normal name now that StrictRedis doesn't have any benefit. --- tildes/scripts/breached_passwords.py | 4 ++-- tildes/tests/conftest.py | 4 ++-- tildes/tildes/__init__.py | 8 ++++---- tildes/tildes/lib/password.py | 4 ++-- tildes/tildes/lib/ratelimit.py | 8 ++++---- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/tildes/scripts/breached_passwords.py b/tildes/scripts/breached_passwords.py index 234f9cf..64eab8a 100644 --- a/tildes/scripts/breached_passwords.py +++ b/tildes/scripts/breached_passwords.py @@ -30,7 +30,7 @@ import subprocess from typing import Any import click -from redis import ResponseError, StrictRedis +from redis import Redis, ResponseError from tildes.lib.password import ( BREACHED_PASSWORDS_BF_KEY, @@ -38,7 +38,7 @@ from tildes.lib.password import ( ) -REDIS = StrictRedis(unix_socket_path=BREACHED_PASSWORDS_REDIS_SOCKET) +REDIS = Redis(unix_socket_path=BREACHED_PASSWORDS_REDIS_SOCKET) def generate_redis_protocol(*elements: Any) -> str: diff --git a/tildes/tests/conftest.py b/tildes/tests/conftest.py index 1cb8228..1549ae3 100644 --- a/tildes/tests/conftest.py +++ b/tildes/tests/conftest.py @@ -7,7 +7,7 @@ import os from pyramid import testing from pyramid.paster import get_app, get_appsettings from pytest import fixture -from redis import StrictRedis +from redis import Redis from sqlalchemy import create_engine from sqlalchemy.engine.url import make_url from sqlalchemy.orm import sessionmaker @@ -118,7 +118,7 @@ def overall_redis_session(): redis_modules = ["/opt/redis-cell/libredis_cell.so"] with RedisServer() as temp_redis_server: - redis = StrictRedis(**temp_redis_server.dsn()) + redis = Redis(**temp_redis_server.dsn()) for module in redis_modules: redis.execute_command("MODULE LOAD", module) diff --git a/tildes/tildes/__init__.py b/tildes/tildes/__init__.py index cc5bc04..3c5bb8f 100644 --- a/tildes/tildes/__init__.py +++ b/tildes/tildes/__init__.py @@ -10,7 +10,7 @@ from pyramid.config import Configurator from pyramid.httpexceptions import HTTPTooManyRequests from pyramid.registry import Registry from pyramid.request import Request -from redis import StrictRedis +from redis import Redis from webassets import Bundle from tildes.lib.ratelimit import RATE_LIMITED_ACTIONS, RateLimitResult @@ -90,10 +90,10 @@ def http_method_tween_factory(handler: Callable, registry: Registry) -> Callable return method_override_tween -def get_redis_connection(request: Request) -> StrictRedis: - """Return a StrictRedis connection to the Redis server.""" +def get_redis_connection(request: Request) -> Redis: + """Return a connection to the Redis server.""" socket = request.registry.settings["redis.unix_socket_path"] - return StrictRedis(unix_socket_path=socket) + return Redis(unix_socket_path=socket) def is_safe_request_method(request: Request) -> bool: diff --git a/tildes/tildes/lib/password.py b/tildes/tildes/lib/password.py index 9d857f3..5fec403 100644 --- a/tildes/tildes/lib/password.py +++ b/tildes/tildes/lib/password.py @@ -5,7 +5,7 @@ from hashlib import sha1 -from redis import ConnectionError, ResponseError, StrictRedis # noqa +from redis import ConnectionError, Redis, ResponseError # noqa # unix socket path for redis server with the breached passwords bloom filter BREACHED_PASSWORDS_REDIS_SOCKET = "/run/redis_breached_passwords/socket" @@ -16,7 +16,7 @@ BREACHED_PASSWORDS_BF_KEY = "breached_passwords_bloom" def is_breached_password(password: str) -> bool: """Return whether the password is in the breached-passwords list.""" - redis = StrictRedis(unix_socket_path=BREACHED_PASSWORDS_REDIS_SOCKET) + redis = Redis(unix_socket_path=BREACHED_PASSWORDS_REDIS_SOCKET) hashed = sha1(password.encode("utf-8")).hexdigest() diff --git a/tildes/tildes/lib/ratelimit.py b/tildes/tildes/lib/ratelimit.py index d52925b..f5572f6 100644 --- a/tildes/tildes/lib/ratelimit.py +++ b/tildes/tildes/lib/ratelimit.py @@ -8,7 +8,7 @@ from ipaddress import ip_address from typing import Any, List, Optional, Sequence from pyramid.response import Response -from redis import StrictRedis +from redis import Redis from tildes.lib.datetime import utc_now @@ -170,7 +170,7 @@ class RateLimitedAction: max_burst: Optional[int] = None, by_user: bool = True, by_ip: bool = True, - redis: Optional[StrictRedis] = None, + redis: Optional[Redis] = None, ): """Initialize the limits on a particular action. @@ -207,7 +207,7 @@ class RateLimitedAction: self._redis = redis @property - def redis(self) -> StrictRedis: + def redis(self) -> Redis: """Return the redis connection.""" if not self._redis: raise RateLimitError("No redis connection set") @@ -215,7 +215,7 @@ class RateLimitedAction: return self._redis @redis.setter - def redis(self, redis_connection: StrictRedis) -> None: + def redis(self, redis_connection: Redis) -> None: """Set the redis connection.""" self._redis = redis_connection