feat: ✨ connection id
This commit is contained in:
parent
a51a0acc5b
commit
ae000732a2
27
Cargo.lock
generated
27
Cargo.lock
generated
@ -1644,9 +1644,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "prost"
|
name = "prost"
|
||||||
version = "0.12.4"
|
version = "0.12.6"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "d0f5d036824e4761737860779c906171497f6d55681139d8312388f8fe398922"
|
checksum = "deb1435c188b76130da55f17a466d252ff7b1418b2ad3e037d127b94e3411f29"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bytes",
|
"bytes",
|
||||||
"prost-derive",
|
"prost-derive",
|
||||||
@ -1675,9 +1675,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "prost-derive"
|
name = "prost-derive"
|
||||||
version = "0.12.4"
|
version = "0.12.6"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "19de2de2a00075bf566bee3bd4db014b11587e84184d3f7a791bc17f1a8e9e48"
|
checksum = "81bddcdb20abf9501610992b6759a4c888aef7d1a7247ef75e2404275ac24af1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"itertools",
|
"itertools",
|
||||||
@ -1762,9 +1762,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "redis"
|
name = "redis"
|
||||||
version = "0.25.3"
|
version = "0.25.4"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "6472825949c09872e8f2c50bde59fcefc17748b6be5c90fd67cd8b4daca73bfd"
|
checksum = "e0d7a6955c7511f60f3ba9e86c6d02b3c3f144f8c24b288d1f4e18074ab8bbec"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"async-trait",
|
"async-trait",
|
||||||
"bytes",
|
"bytes",
|
||||||
@ -1949,6 +1949,7 @@ dependencies = [
|
|||||||
"tokio-util",
|
"tokio-util",
|
||||||
"tonic",
|
"tonic",
|
||||||
"tonic-build",
|
"tonic-build",
|
||||||
|
"uuid",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -2696,9 +2697,9 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tokio"
|
name = "tokio"
|
||||||
version = "1.37.0"
|
version = "1.38.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "1adbebffeca75fcfd058afa480fb6c0b81e165a0323f9c9d39c9697e37c46787"
|
checksum = "ba4f4a02a7a80d6f274636f0aa95c7e383b912d41fe721a31f29e29698585a4a"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"backtrace",
|
"backtrace",
|
||||||
"bytes",
|
"bytes",
|
||||||
@ -2723,9 +2724,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tokio-macros"
|
name = "tokio-macros"
|
||||||
version = "2.2.0"
|
version = "2.3.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b"
|
checksum = "5f5ae998a069d4b5aba8ee9dad856af7d520c3699e6159b185c2acd48155d39a"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
@ -2745,9 +2746,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tokio-tungstenite"
|
name = "tokio-tungstenite"
|
||||||
version = "0.21.0"
|
version = "0.22.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "c83b561d025642014097b66e6c1bb422783339e0909e4429cde4749d1990bc38"
|
checksum = "d46baf930138837d65e25e3b33be49c9228579a6135dbf756b5cb9e4283e7cef"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"futures-util",
|
"futures-util",
|
||||||
"log",
|
"log",
|
||||||
@ -3013,6 +3014,8 @@ version = "1.8.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "a183cf7feeba97b4dd1c0d46788634f6221d87fa961b305bed08c851829efcc0"
|
checksum = "a183cf7feeba97b4dd1c0d46788634f6221d87fa961b305bed08c851829efcc0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"getrandom",
|
||||||
|
"rand",
|
||||||
"serde",
|
"serde",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|||||||
21
Cargo.toml
21
Cargo.toml
@ -15,17 +15,17 @@ path = "lib/lib.rs"
|
|||||||
# utils
|
# utils
|
||||||
eyre = { version = "0.6.12", default-features = false }
|
eyre = { version = "0.6.12", default-features = false }
|
||||||
dotenvy = "0.15.7"
|
dotenvy = "0.15.7"
|
||||||
chrono = "0.4.37"
|
chrono = "0.4.38"
|
||||||
getset = "0.1.2"
|
getset = "0.1.2"
|
||||||
|
|
||||||
# async
|
# async
|
||||||
tokio = { version = "1.37.0", features = ["macros", "rt-multi-thread"] }
|
tokio = { version = "1.38.0", features = ["macros", "rt-multi-thread"] }
|
||||||
async-trait = "0.1.79"
|
async-trait = "0.1.80"
|
||||||
|
|
||||||
# grpc & websocket
|
# grpc & websocket
|
||||||
tokio-tungstenite = { version = "0.21.0" }
|
tokio-tungstenite = { version = "0.22.0" }
|
||||||
tonic = "0.11.0"
|
tonic = "0.11.0"
|
||||||
prost = "0.12.3" # protocol buffers
|
prost = "0.12.6" # protocol buffers
|
||||||
futures = "0.3.30"
|
futures = "0.3.30"
|
||||||
tokio-util = "0.7.11"
|
tokio-util = "0.7.11"
|
||||||
|
|
||||||
@ -39,22 +39,19 @@ sea-orm-migration = { version = "0.12.15", features = [
|
|||||||
"runtime-tokio-native-tls",
|
"runtime-tokio-native-tls",
|
||||||
"sqlx-sqlite",
|
"sqlx-sqlite",
|
||||||
] }
|
] }
|
||||||
redis = { version = "0.25.3", features = ["tokio-comp"] }
|
redis = { version = "0.25.4", features = ["tokio-comp"] }
|
||||||
|
|
||||||
# other
|
# other
|
||||||
serde = { version = "1.0.203", features = ["derive"] }
|
serde = { version = "1.0.203", features = ["derive"] }
|
||||||
serde_json = "1.0.117"
|
serde_json = "1.0.117"
|
||||||
|
uuid = { version = "1.8.0", features = ["v4", "fast-rng"] }
|
||||||
[dependencies.lool]
|
lool = { version = "^0.3.2", registry = "lugit", features = [
|
||||||
version = "^0.3.2" # crates: disable-check
|
|
||||||
registry = "lugit"
|
|
||||||
features = [
|
|
||||||
"cli.stylize",
|
"cli.stylize",
|
||||||
"logger",
|
"logger",
|
||||||
"sched.tokio",
|
"sched.tokio",
|
||||||
"sched.rule-recurrence",
|
"sched.rule-recurrence",
|
||||||
"macros",
|
"macros",
|
||||||
]
|
] }
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
tonic-build = "0.11.0"
|
tonic-build = "0.11.0"
|
||||||
|
|||||||
@ -22,9 +22,11 @@ impl EventDispatcher for Dispatcher {
|
|||||||
event: String,
|
event: String,
|
||||||
data: event::Data,
|
data: event::Data,
|
||||||
outgoing: Arc<Mutex<Outgoing>>,
|
outgoing: Arc<Mutex<Outgoing>>,
|
||||||
|
conn_id: String,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
info!("Event: {}", event);
|
info!("Event: {}", event);
|
||||||
info!("Data: {:?}", data);
|
info!("Data: {:?}", data);
|
||||||
|
info!("Connection ID: {}", conn_id);
|
||||||
|
|
||||||
let mut sx = bus::redis::subscriber::<Quote, _>(&"redis://127.0.0.1/").await?;
|
let mut sx = bus::redis::subscriber::<Quote, _>(&"redis://127.0.0.1/").await?;
|
||||||
let mut quote_feed = sx.stream().await?;
|
let mut quote_feed = sx.stream().await?;
|
||||||
|
|||||||
@ -32,6 +32,7 @@ pub trait EventDispatcher: Send {
|
|||||||
event: String,
|
event: String,
|
||||||
data: event::Data,
|
data: event::Data,
|
||||||
outgoing: Arc<Mutex<Outgoing>>,
|
outgoing: Arc<Mutex<Outgoing>>,
|
||||||
|
conn_id: String,
|
||||||
) -> Result<()>;
|
) -> Result<()>;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -101,11 +102,12 @@ where
|
|||||||
|
|
||||||
if let Ok(ws_stream) = ws_stream {
|
if let Ok(ws_stream) = ws_stream {
|
||||||
stats.inc_current_clients();
|
stats.inc_current_clients();
|
||||||
|
|
||||||
let stats = stats.clone();
|
let stats = stats.clone();
|
||||||
|
let conn_id = uuid::Uuid::new_v4();
|
||||||
|
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
match Server::handle_connection(ws_stream, dispatcher).await {
|
match Server::handle_connection(ws_stream, dispatcher, conn_id).await {
|
||||||
Ok(_) => info!("Connection closed"),
|
Ok(_) => info!("Connection {} closed", conn_id),
|
||||||
Err(e) => error!("Error handling connection: {:?}", e),
|
Err(e) => error!("Error handling connection: {:?}", e),
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -123,12 +125,14 @@ where
|
|||||||
async fn handle_connection(
|
async fn handle_connection(
|
||||||
stream: WebSocketStream<TcpStream>,
|
stream: WebSocketStream<TcpStream>,
|
||||||
event_dispatcher: ED,
|
event_dispatcher: ED,
|
||||||
|
conn_id: uuid::Uuid,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let (outgoing, mut incoming) = stream.split();
|
let (outgoing, mut incoming) = stream.split();
|
||||||
let synced_outgoing = Arc::new(Mutex::new(outgoing));
|
let synced_outgoing = Arc::new(Mutex::new(outgoing));
|
||||||
|
|
||||||
while let Some(msg) = incoming.next().await {
|
while let Some(msg) = incoming.next().await {
|
||||||
Server::handle_message(msg?, &event_dispatcher, synced_outgoing.clone()).await?;
|
Server::handle_message(msg?, &event_dispatcher, synced_outgoing.clone(), conn_id)
|
||||||
|
.await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
@ -139,11 +143,14 @@ where
|
|||||||
msg: Message,
|
msg: Message,
|
||||||
event_dispatcher: &ED,
|
event_dispatcher: &ED,
|
||||||
outgoing: Arc<Mutex<Outgoing>>,
|
outgoing: Arc<Mutex<Outgoing>>,
|
||||||
|
conn_id: uuid::Uuid,
|
||||||
) -> Result<HandlingResult> {
|
) -> Result<HandlingResult> {
|
||||||
if msg.is_text() || msg.is_binary() {
|
if msg.is_text() || msg.is_binary() {
|
||||||
if let Ok(event) = serde_json::from_str::<event::WsEvent>(&msg.to_string()) {
|
if let Ok(event) = serde_json::from_str::<event::WsEvent>(&msg.to_string()) {
|
||||||
let outgoing = Arc::clone(&outgoing);
|
let outgoing = Arc::clone(&outgoing);
|
||||||
let result = event_dispatcher.dispatch(event.event, event.data, outgoing).await;
|
let result = event_dispatcher
|
||||||
|
.dispatch(event.event, event.data, outgoing, conn_id.into())
|
||||||
|
.await;
|
||||||
|
|
||||||
match result {
|
match result {
|
||||||
Ok(_) => {}
|
Ok(_) => {}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user