e72866db8d
- Add etcd-client dependency for distributed configuration storage - Implement EtcdConfig with priority: etcd > environment variables > defaults - Add ServiceRegistry for service registration with lease keep-alive - Integrate etcd-based service discovery for appks gRPC connections - Add service watcher for real-time service instance updates - Migrate Redis configuration from single URL to cluster node list - Update Dockerfile with default IMKS_HOST and IMKS_PORT environment variables - Add etcd bootstrap configuration through environment variables - Implement Redis cluster URL building with optional authentication
103 lines
3.1 KiB
Rust
103 lines
3.1 KiB
Rust
use async_trait::async_trait;
|
|
use fred::clients::{Client, SubscriberClient};
|
|
use fred::interfaces::{ClientLike, EventInterface, PubsubInterface};
|
|
use fred::prelude::*;
|
|
use tokio::sync::mpsc;
|
|
|
|
use crate::socket::message_bus::{MessageBus, MessageBusError};
|
|
|
|
pub struct RedisMessageBus {
|
|
client: Client,
|
|
subscriber: SubscriberClient,
|
|
}
|
|
|
|
impl RedisMessageBus {
|
|
/// Connect to a Redis cluster.
|
|
///
|
|
/// `cluster_url` should be in `redis-cluster://` format, e.g.:
|
|
/// `redis-cluster://host1:6379,host2:6379,host3:6379`
|
|
pub async fn new(cluster_url: &str) -> Result<Self, MessageBusError> {
|
|
let config =
|
|
Config::from_url(cluster_url).map_err(|e| MessageBusError::Redis(e.to_string()))?;
|
|
|
|
let client = Client::new(config.clone(), None, None, None);
|
|
let subscriber = SubscriberClient::new(config, None, None, None);
|
|
|
|
let _ = client.connect().await;
|
|
let _ = subscriber.connect().await;
|
|
|
|
client
|
|
.wait_for_connect()
|
|
.await
|
|
.map_err(|e| MessageBusError::Redis(e.to_string()))?;
|
|
subscriber
|
|
.wait_for_connect()
|
|
.await
|
|
.map_err(|e| MessageBusError::Redis(e.to_string()))?;
|
|
|
|
tracing::info!(cluster_url, "Redis cluster connected");
|
|
Ok(Self { client, subscriber })
|
|
}
|
|
|
|
pub fn client(&self) -> &Client {
|
|
&self.client
|
|
}
|
|
}
|
|
|
|
#[async_trait]
|
|
impl MessageBus for RedisMessageBus {
|
|
async fn publish(&self, channel: &str, message: &[u8]) -> Result<(), MessageBusError> {
|
|
self.client
|
|
.publish::<(), _, Vec<u8>>(channel, message.to_vec())
|
|
.await
|
|
.map_err(|e| MessageBusError::Redis(e.to_string()))?;
|
|
Ok(())
|
|
}
|
|
|
|
async fn subscribe(&self, channel: &str) -> Result<mpsc::Receiver<Vec<u8>>, MessageBusError> {
|
|
let (tx, rx) = mpsc::channel::<Vec<u8>>(256);
|
|
|
|
self.subscriber
|
|
.subscribe(channel.to_string())
|
|
.await
|
|
.map_err(|e| MessageBusError::Redis(e.to_string()))?;
|
|
|
|
let subscriber = self.subscriber.clone();
|
|
let channel_owned = channel.to_string();
|
|
let mut message_rx = subscriber.message_rx();
|
|
|
|
tokio::spawn(async move {
|
|
while let Ok(message) = message_rx.recv().await {
|
|
if message.channel == channel_owned {
|
|
let data: Vec<u8> = FromValue::from_value(message.value).unwrap_or_default();
|
|
if tx.send(data).await.is_err() {
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
Ok(rx)
|
|
}
|
|
|
|
async fn unsubscribe(&self, channel: &str) -> Result<(), MessageBusError> {
|
|
self.subscriber
|
|
.unsubscribe(channel.to_string())
|
|
.await
|
|
.map_err(|e| MessageBusError::Redis(e.to_string()))?;
|
|
Ok(())
|
|
}
|
|
|
|
async fn close(&self) -> Result<(), MessageBusError> {
|
|
self.client
|
|
.quit()
|
|
.await
|
|
.map_err(|e| MessageBusError::Redis(e.to_string()))?;
|
|
self.subscriber
|
|
.quit()
|
|
.await
|
|
.map_err(|e| MessageBusError::Redis(e.to_string()))?;
|
|
Ok(())
|
|
}
|
|
}
|