|
| 1 | +"""State manager for managing client states.""" |
| 2 | + |
| 3 | +import contextlib |
| 4 | +import dataclasses |
| 5 | +from abc import ABC, abstractmethod |
| 6 | +from collections.abc import AsyncIterator |
| 7 | + |
| 8 | +from reflex import constants |
| 9 | +from reflex.config import get_config |
| 10 | +from reflex.state import BaseState |
| 11 | +from reflex.utils import console, prerequisites |
| 12 | +from reflex.utils.exceptions import InvalidStateManagerModeError |
| 13 | + |
| 14 | + |
| 15 | +@dataclasses.dataclass |
| 16 | +class StateManager(ABC): |
| 17 | + """A class to manage many client states.""" |
| 18 | + |
| 19 | + # The state class to use. |
| 20 | + state: type[BaseState] |
| 21 | + |
| 22 | + @classmethod |
| 23 | + def create(cls, state: type[BaseState]): |
| 24 | + """Create a new state manager. |
| 25 | +
|
| 26 | + Args: |
| 27 | + state: The state class to use. |
| 28 | +
|
| 29 | + Raises: |
| 30 | + InvalidStateManagerModeError: If the state manager mode is invalid. |
| 31 | +
|
| 32 | + Returns: |
| 33 | + The state manager (either disk, memory or redis). |
| 34 | + """ |
| 35 | + config = get_config() |
| 36 | + if prerequisites.parse_redis_url() is not None: |
| 37 | + config.state_manager_mode = constants.StateManagerMode.REDIS |
| 38 | + if config.state_manager_mode == constants.StateManagerMode.MEMORY: |
| 39 | + from reflex.istate.manager.memory import StateManagerMemory |
| 40 | + |
| 41 | + return StateManagerMemory(state=state) |
| 42 | + if config.state_manager_mode == constants.StateManagerMode.DISK: |
| 43 | + from reflex.istate.manager.disk import StateManagerDisk |
| 44 | + |
| 45 | + return StateManagerDisk(state=state) |
| 46 | + if config.state_manager_mode == constants.StateManagerMode.REDIS: |
| 47 | + redis = prerequisites.get_redis() |
| 48 | + if redis is not None: |
| 49 | + from reflex.istate.manager.redis import StateManagerRedis |
| 50 | + |
| 51 | + # make sure expiration values are obtained only from the config object on creation |
| 52 | + return StateManagerRedis( |
| 53 | + state=state, |
| 54 | + redis=redis, |
| 55 | + token_expiration=config.redis_token_expiration, |
| 56 | + lock_expiration=config.redis_lock_expiration, |
| 57 | + lock_warning_threshold=config.redis_lock_warning_threshold, |
| 58 | + ) |
| 59 | + msg = f"Expected one of: DISK, MEMORY, REDIS, got {config.state_manager_mode}" |
| 60 | + raise InvalidStateManagerModeError(msg) |
| 61 | + |
| 62 | + @abstractmethod |
| 63 | + async def get_state(self, token: str) -> BaseState: |
| 64 | + """Get the state for a token. |
| 65 | +
|
| 66 | + Args: |
| 67 | + token: The token to get the state for. |
| 68 | +
|
| 69 | + Returns: |
| 70 | + The state for the token. |
| 71 | + """ |
| 72 | + |
| 73 | + @abstractmethod |
| 74 | + async def set_state(self, token: str, state: BaseState): |
| 75 | + """Set the state for a token. |
| 76 | +
|
| 77 | + Args: |
| 78 | + token: The token to set the state for. |
| 79 | + state: The state to set. |
| 80 | + """ |
| 81 | + |
| 82 | + @abstractmethod |
| 83 | + @contextlib.asynccontextmanager |
| 84 | + async def modify_state(self, token: str) -> AsyncIterator[BaseState]: |
| 85 | + """Modify the state for a token while holding exclusive lock. |
| 86 | +
|
| 87 | + Args: |
| 88 | + token: The token to modify the state for. |
| 89 | +
|
| 90 | + Yields: |
| 91 | + The state for the token. |
| 92 | + """ |
| 93 | + yield self.state() |
| 94 | + |
| 95 | + |
| 96 | +def _default_token_expiration() -> int: |
| 97 | + """Get the default token expiration time. |
| 98 | +
|
| 99 | + Returns: |
| 100 | + The default token expiration time. |
| 101 | + """ |
| 102 | + return get_config().redis_token_expiration |
| 103 | + |
| 104 | + |
| 105 | +def reset_disk_state_manager(): |
| 106 | + """Reset the disk state manager.""" |
| 107 | + console.debug("Resetting disk state manager.") |
| 108 | + states_directory = prerequisites.get_states_dir() |
| 109 | + if states_directory.exists(): |
| 110 | + for path in states_directory.iterdir(): |
| 111 | + path.unlink() |
| 112 | + |
| 113 | + |
| 114 | +def get_state_manager() -> StateManager: |
| 115 | + """Get the state manager for the app that is currently running. |
| 116 | +
|
| 117 | + Returns: |
| 118 | + The state manager. |
| 119 | + """ |
| 120 | + return prerequisites.get_and_validate_app().app.state_manager |
0 commit comments