mirror of
https://github.com/langgenius/dify.git
synced 2026-02-03 09:31:13 +08:00
Some checks are pending
autofix.ci / autofix (push) Waiting to run
Build and Push API & Web / build (api, DIFY_API_IMAGE_NAME, linux/amd64, build-api-amd64) (push) Waiting to run
Build and Push API & Web / build (api, DIFY_API_IMAGE_NAME, linux/arm64, build-api-arm64) (push) Waiting to run
Build and Push API & Web / build (web, DIFY_WEB_IMAGE_NAME, linux/amd64, build-web-amd64) (push) Waiting to run
Build and Push API & Web / build (web, DIFY_WEB_IMAGE_NAME, linux/arm64, build-web-arm64) (push) Waiting to run
Build and Push API & Web / create-manifest (api, DIFY_API_IMAGE_NAME, merge-api-images) (push) Blocked by required conditions
Build and Push API & Web / create-manifest (web, DIFY_WEB_IMAGE_NAME, merge-web-images) (push) Blocked by required conditions
Main CI Pipeline / Check Changed Files (push) Waiting to run
Main CI Pipeline / API Tests (push) Blocked by required conditions
Main CI Pipeline / Web Tests (push) Blocked by required conditions
Main CI Pipeline / Style Check (push) Waiting to run
Main CI Pipeline / VDB Tests (push) Blocked by required conditions
Main CI Pipeline / DB Migration Test (push) Blocked by required conditions
Co-authored-by: Dev <dev@Devs-MacBook-Pro-4.local> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Asuka Minato <i@asukaminato.eu.org> Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com>
70 lines
1.9 KiB
Python
70 lines
1.9 KiB
Python
from __future__ import annotations
|
|
|
|
from libs.broadcast_channel.channel import Producer, Subscriber, Subscription
|
|
from redis import Redis
|
|
|
|
from ._subscription import RedisSubscriptionBase
|
|
|
|
|
|
class BroadcastChannel:
|
|
"""
|
|
Redis Pub/Sub based broadcast channel implementation (regular, non-sharded).
|
|
|
|
Provides "at most once" delivery semantics for messages published to channels
|
|
using Redis PUBLISH/SUBSCRIBE commands for real-time message delivery.
|
|
|
|
The `redis_client` used to construct BroadcastChannel should have `decode_responses` set to `False`.
|
|
"""
|
|
|
|
def __init__(
|
|
self,
|
|
redis_client: Redis,
|
|
):
|
|
self._client = redis_client
|
|
|
|
def topic(self, topic: str) -> Topic:
|
|
return Topic(self._client, topic)
|
|
|
|
|
|
class Topic:
|
|
def __init__(self, redis_client: Redis, topic: str):
|
|
self._client = redis_client
|
|
self._topic = topic
|
|
|
|
def as_producer(self) -> Producer:
|
|
return self
|
|
|
|
def publish(self, payload: bytes) -> None:
|
|
self._client.publish(self._topic, payload)
|
|
|
|
def as_subscriber(self) -> Subscriber:
|
|
return self
|
|
|
|
def subscribe(self) -> Subscription:
|
|
return _RedisSubscription(
|
|
pubsub=self._client.pubsub(),
|
|
topic=self._topic,
|
|
)
|
|
|
|
|
|
class _RedisSubscription(RedisSubscriptionBase):
|
|
"""Regular Redis pub/sub subscription implementation."""
|
|
|
|
def _get_subscription_type(self) -> str:
|
|
return "regular"
|
|
|
|
def _subscribe(self) -> None:
|
|
assert self._pubsub is not None
|
|
self._pubsub.subscribe(self._topic)
|
|
|
|
def _unsubscribe(self) -> None:
|
|
assert self._pubsub is not None
|
|
self._pubsub.unsubscribe(self._topic)
|
|
|
|
def _get_message(self) -> dict | None:
|
|
assert self._pubsub is not None
|
|
return self._pubsub.get_message(ignore_subscribe_messages=True, timeout=0.1)
|
|
|
|
def _get_message_type(self) -> str:
|
|
return "message"
|