Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion agentex/src/adapters/streams/adapter_redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,13 @@ async def send_data(self, topic: str, data: dict[str, Any]) -> str:

logger.info(f"Publishing data to stream {topic}, data: {data_json}")

# Add to Redis stream with a reasonable max length
# Add to Redis stream with maxlen to prevent unbounded growth
await self.send_redis_connection_metrics()
message_id = await self.redis.xadd(
name=topic,
fields={"data": data_json},
maxlen=self.environment_variables.REDIS_STREAM_MAXLEN,
approximate=True, # Use ~ for better performance (O(1) vs O(N))
)
await self.send_redis_connection_metrics()
return message_id
Expand Down
7 changes: 7 additions & 0 deletions agentex/src/config/environment_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class EnvVarKeys(str, Enum):
REDIS_MAX_CONNECTIONS = "REDIS_MAX_CONNECTIONS"
REDIS_CONNECTION_TIMEOUT = "REDIS_CONNECTION_TIMEOUT"
REDIS_SOCKET_TIMEOUT = "REDIS_SOCKET_TIMEOUT"
REDIS_STREAM_MAXLEN = "REDIS_STREAM_MAXLEN"
IMAGE_PULL_SECRET_NAME = "IMAGE_PULL_SECRET_NAME"
AGENTEX_AUTH_URL = "AGENTEX_AUTH_URL"
ALLOWED_ORIGINS = "ALLOWED_ORIGINS"
Expand Down Expand Up @@ -88,6 +89,9 @@ class EnvironmentVariables(BaseModel):
REDIS_MAX_CONNECTIONS: int = 50 # Increased for SSE streaming
REDIS_CONNECTION_TIMEOUT: int = 60 # Connection timeout in seconds
REDIS_SOCKET_TIMEOUT: int = 30 # Socket timeout in seconds
REDIS_STREAM_MAXLEN: int = (
10000 # Max entries per Redis stream to prevent unbounded growth
)
IMAGE_PULL_SECRET_NAME: str | None = None
AGENTEX_AUTH_URL: str | None = None
ALLOWED_ORIGINS: str | None = None
Expand Down Expand Up @@ -146,6 +150,9 @@ def refresh(cls, force_refresh: bool = False) -> EnvironmentVariables | None:
REDIS_SOCKET_TIMEOUT=int(
os.environ.get(EnvVarKeys.REDIS_SOCKET_TIMEOUT, "30")
),
REDIS_STREAM_MAXLEN=int(
os.environ.get(EnvVarKeys.REDIS_STREAM_MAXLEN, "10000")
),
IMAGE_PULL_SECRET_NAME=os.environ.get(EnvVarKeys.IMAGE_PULL_SECRET_NAME),
AGENTEX_AUTH_URL=os.environ.get(EnvVarKeys.AGENTEX_AUTH_URL),
ALLOWED_ORIGINS=os.environ.get(EnvVarKeys.ALLOWED_ORIGINS, "*"),
Expand Down
Loading