feat(config): integrate etcd for service discovery and config management

- 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
This commit is contained in:
zhenyi
2026-06-11 22:50:38 +08:00
parent e72866db8d
commit c794b818ff
24 changed files with 984 additions and 952 deletions
+40 -28
View File
@@ -1,8 +1,8 @@
use std::time::{SystemTime, UNIX_EPOCH};
use async_trait::async_trait;
use fred::prelude::*;
use crate::socket::message_bus::redis::RedisCommandClient;
use crate::socket::message_bus::redis::RedisMessageBus;
use crate::socket::session_store::{SessionError, SessionInfo, SessionStoreTrait};
@@ -17,14 +17,14 @@ const DEFAULT_TTL_SECS: u64 = 60;
const KEY_PREFIX: &str = "socket.io:session";
pub struct RedisSessionStore {
client: Client,
client: RedisCommandClient,
ttl_secs: u64,
}
impl RedisSessionStore {
pub fn new(bus: &RedisMessageBus, ttl_secs: Option<u64>) -> Self {
Self {
client: bus.client().clone(),
client: bus.client(),
ttl_secs: ttl_secs.unwrap_or(DEFAULT_TTL_SECS),
}
}
@@ -45,22 +45,29 @@ impl SessionStoreTrait for RedisSessionStore {
let key = self.key(sid);
let now = now_millis();
// Batch all fields in a single HSET call for efficiency
let fields: Vec<(&str, String)> = vec![
("sid", sid.to_string()),
("transport", transport.to_string()),
("state", "connecting".to_string()),
("server_id", server_id.to_string()),
("created_at", now.to_string()),
("last_ping", now.to_string()),
];
// Batch all fields in a single HMSET-style call
self.client
.hset::<(), _, _>(&key, fields)
.query::<()>(
redis::cmd("HSET")
.arg(&key)
.arg("sid")
.arg(sid)
.arg("transport")
.arg(transport)
.arg("state")
.arg("connecting")
.arg("server_id")
.arg(server_id)
.arg("created_at")
.arg(now.to_string())
.arg("last_ping")
.arg(now.to_string()),
)
.await
.map_err(|e| SessionError::Redis(e.to_string()))?;
self.client
.expire::<(), _>(&key, self.ttl_secs as i64, None)
.query::<()>(redis::cmd("EXPIRE").arg(&key).arg(self.ttl_secs as i64))
.await
.map_err(|e| SessionError::Redis(e.to_string()))?;
@@ -70,11 +77,9 @@ impl SessionStoreTrait for RedisSessionStore {
async fn get(&self, sid: &str) -> Result<Option<SessionInfo>, SessionError> {
let key = self.key(sid);
// Use hgetall directly — if the key doesn't exist Redis returns an empty map.
// This avoids the TOCTOU race between EXISTS and HGETALL.
let values: std::collections::HashMap<String, String> = self
.client
.hgetall::<std::collections::HashMap<String, String>, _>(&key)
.query(redis::cmd("HGETALL").arg(&key))
.await
.map_err(|e| SessionError::Redis(e.to_string()))?;
@@ -103,14 +108,13 @@ impl SessionStoreTrait for RedisSessionStore {
async fn set_state(&self, sid: &str, state: &str) -> Result<(), SessionError> {
let key = self.key(sid);
// Use HSET (not HSETNX) to overwrite existing fields
self.client
.hset::<(), _, _>(&key, ("state", state))
.query::<()>(redis::cmd("HSET").arg(&key).arg("state").arg(state))
.await
.map_err(|e| SessionError::Redis(e.to_string()))?;
self.client
.expire::<(), _>(&key, self.ttl_secs as i64, None)
.query::<()>(redis::cmd("EXPIRE").arg(&key).arg(self.ttl_secs as i64))
.await
.map_err(|e| SessionError::Redis(e.to_string()))?;
@@ -120,14 +124,18 @@ impl SessionStoreTrait for RedisSessionStore {
async fn set_transport(&self, sid: &str, transport: &str) -> Result<(), SessionError> {
let key = self.key(sid);
// Use HSET (not HSETNX) to overwrite existing fields
self.client
.hset::<(), _, _>(&key, ("transport", transport))
.query::<()>(
redis::cmd("HSET")
.arg(&key)
.arg("transport")
.arg(transport),
)
.await
.map_err(|e| SessionError::Redis(e.to_string()))?;
self.client
.expire::<(), _>(&key, self.ttl_secs as i64, None)
.query::<()>(redis::cmd("EXPIRE").arg(&key).arg(self.ttl_secs as i64))
.await
.map_err(|e| SessionError::Redis(e.to_string()))?;
@@ -138,14 +146,18 @@ impl SessionStoreTrait for RedisSessionStore {
let key = self.key(sid);
let now = now_millis();
// Use HSET (not HSETNX) to overwrite existing fields
self.client
.hset::<(), _, _>(&key, ("last_ping", now.to_string()))
.query::<()>(
redis::cmd("HSET")
.arg(&key)
.arg("last_ping")
.arg(now.to_string()),
)
.await
.map_err(|e| SessionError::Redis(e.to_string()))?;
self.client
.expire::<(), _>(&key, self.ttl_secs as i64, None)
.query::<()>(redis::cmd("EXPIRE").arg(&key).arg(self.ttl_secs as i64))
.await
.map_err(|e| SessionError::Redis(e.to_string()))?;
@@ -156,7 +168,7 @@ impl SessionStoreTrait for RedisSessionStore {
let key = self.key(sid);
self.client
.del::<(), _>(&key)
.query::<()>(redis::cmd("DEL").arg(&key))
.await
.map_err(|e| SessionError::Redis(e.to_string()))?;
@@ -168,7 +180,7 @@ impl SessionStoreTrait for RedisSessionStore {
let exists: bool = self
.client
.exists::<bool, _>(&key)
.query(redis::cmd("EXISTS").arg(&key))
.await
.map_err(|e| SessionError::Redis(e.to_string()))?;