ext_redis.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import redis
  2. from redis.connection import Connection, SSLConnection
  3. from redis.sentinel import Sentinel
  4. from configs import dify_config
  5. class RedisClientWrapper(redis.Redis):
  6. """
  7. A wrapper class for the Redis client that addresses the issue where the global
  8. `redis_client` variable cannot be updated when a new Redis instance is returned
  9. by Sentinel.
  10. This class allows for deferred initialization of the Redis client, enabling the
  11. client to be re-initialized with a new instance when necessary. This is particularly
  12. useful in scenarios where the Redis instance may change dynamically, such as during
  13. a failover in a Sentinel-managed Redis setup.
  14. Attributes:
  15. _client (redis.Redis): The actual Redis client instance. It remains None until
  16. initialized with the `initialize` method.
  17. Methods:
  18. initialize(client): Initializes the Redis client if it hasn't been initialized already.
  19. __getattr__(item): Delegates attribute access to the Redis client, raising an error
  20. if the client is not initialized.
  21. """
  22. def __init__(self):
  23. self._client = None
  24. def initialize(self, client):
  25. if self._client is None:
  26. self._client = client
  27. def __getattr__(self, item):
  28. if self._client is None:
  29. raise RuntimeError("Redis client is not initialized. Call init_app first.")
  30. return getattr(self._client, item)
  31. redis_client = RedisClientWrapper()
  32. def init_app(app):
  33. global redis_client
  34. connection_class = Connection
  35. if dify_config.REDIS_USE_SSL:
  36. connection_class = SSLConnection
  37. redis_params = {
  38. "username": dify_config.REDIS_USERNAME,
  39. "password": dify_config.REDIS_PASSWORD,
  40. "db": dify_config.REDIS_DB,
  41. "encoding": "utf-8",
  42. "encoding_errors": "strict",
  43. "decode_responses": False,
  44. }
  45. if dify_config.REDIS_USE_SENTINEL:
  46. sentinel_hosts = [
  47. (node.split(":")[0], int(node.split(":")[1])) for node in dify_config.REDIS_SENTINELS.split(",")
  48. ]
  49. sentinel = Sentinel(
  50. sentinel_hosts,
  51. sentinel_kwargs={
  52. "socket_timeout": dify_config.REDIS_SENTINEL_SOCKET_TIMEOUT,
  53. "username": dify_config.REDIS_SENTINEL_USERNAME,
  54. "password": dify_config.REDIS_SENTINEL_PASSWORD,
  55. },
  56. )
  57. master = sentinel.master_for(dify_config.REDIS_SENTINEL_SERVICE_NAME, **redis_params)
  58. redis_client.initialize(master)
  59. else:
  60. redis_params.update(
  61. {
  62. "host": dify_config.REDIS_HOST,
  63. "port": dify_config.REDIS_PORT,
  64. "connection_class": connection_class,
  65. }
  66. )
  67. pool = redis.ConnectionPool(**redis_params)
  68. redis_client.initialize(redis.Redis(connection_pool=pool))
  69. app.extensions["redis"] = redis_client