split core into a separate crate (#17)

This commit is contained in:
Nikita Vilunov 2023-09-30 23:12:11 +00:00
parent 1b3551f108
commit a1db17c779
15 changed files with 89 additions and 83 deletions

14
Cargo.lock generated
View File

@ -854,6 +854,7 @@ dependencies = [
"futures-util", "futures-util",
"http-body-util", "http-body-util",
"hyper 1.0.0-rc.3", "hyper 1.0.0-rc.3",
"lavina-core",
"mgmt-api", "mgmt-api",
"nonempty", "nonempty",
"prometheus", "prometheus",
@ -865,7 +866,6 @@ dependencies = [
"rustls-pemfile", "rustls-pemfile",
"serde", "serde",
"serde_json", "serde_json",
"sqlx",
"tokio", "tokio",
"tokio-rustls", "tokio-rustls",
"tracing", "tracing",
@ -873,6 +873,18 @@ dependencies = [
"uuid", "uuid",
] ]
[[package]]
name = "lavina-core"
version = "0.0.1-dev"
dependencies = [
"anyhow",
"prometheus",
"serde",
"sqlx",
"tokio",
"tracing",
]
[[package]] [[package]]
name = "lazy_static" name = "lazy_static"
version = "1.4.0" version = "1.4.0"

View File

@ -1,6 +1,7 @@
[workspace] [workspace]
members = [ members = [
".", ".",
"crates/lavina-core",
"crates/proto-irc", "crates/proto-irc",
"crates/proto-xmpp", "crates/proto-xmpp",
"crates/mgmt-api", "crates/mgmt-api",
@ -22,6 +23,8 @@ regex = "1.7.1"
derive_more = "0.99.17" derive_more = "0.99.17"
clap = { version = "4.4.4", features = ["derive"] } clap = { version = "4.4.4", features = ["derive"] }
serde = { version = "1.0.152", features = ["rc", "serde_derive"] } serde = { version = "1.0.152", features = ["rc", "serde_derive"] }
tracing = "0.1.37" # logging & tracing api
prometheus = { version = "0.13.3", default-features = false }
[package] [package]
name = "lavina" name = "lavina"
@ -37,17 +40,17 @@ http-body-util = "0.1.0-rc.3"
serde.workspace = true serde.workspace = true
serde_json = "1.0.93" serde_json = "1.0.93"
tokio.workspace = true tokio.workspace = true
tracing = "0.1.37" # logging & tracing api tracing.workspace = true
tracing-subscriber = "0.3.16" tracing-subscriber = "0.3.16"
futures-util.workspace = true futures-util.workspace = true
prometheus = { version = "0.13.3", default-features = false } prometheus.workspace = true
nonempty.workspace = true nonempty.workspace = true
tokio-rustls = "0.24.1" tokio-rustls = "0.24.1"
rustls-pemfile = "1.0.2" rustls-pemfile = "1.0.2"
quick-xml.workspace = true quick-xml.workspace = true
derive_more.workspace = true derive_more.workspace = true
uuid = { version = "1.3.0", features = ["v4"] } uuid = { version = "1.3.0", features = ["v4"] }
sqlx = { version = "0.7.0-alpha.2", features = ["sqlite", "migrate"] } lavina-core = { path = "crates/lavina-core" }
proto-irc = { path = "crates/proto-irc" } proto-irc = { path = "crates/proto-irc" }
proto-xmpp = { path = "crates/proto-xmpp" } proto-xmpp = { path = "crates/proto-xmpp" }
mgmt-api = { path = "crates/mgmt-api" } mgmt-api = { path = "crates/mgmt-api" }

View File

@ -0,0 +1,12 @@
[package]
name = "lavina-core"
edition = "2021"
version.workspace = true
[dependencies]
anyhow.workspace = true
sqlx = { version = "0.7.0-alpha.2", features = ["sqlite", "migrate"] }
serde.workspace = true
tokio.workspace = true
tracing.workspace = true
prometheus.workspace = true

View File

@ -2,3 +2,6 @@
pub mod player; pub mod player;
pub mod repo; pub mod repo;
pub mod room; pub mod room;
mod prelude;
mod table;

View File

@ -19,11 +19,9 @@ use tokio::{
task::JoinHandle, task::JoinHandle,
}; };
use crate::{ use crate::prelude::*;
core::room::{RoomHandle, RoomId, RoomInfo, RoomRegistry}, use crate::room::{RoomHandle, RoomId, RoomInfo, RoomRegistry};
prelude::*, use crate::table::{AnonTable, Key as AnonKey};
util::table::{AnonTable, Key as AnonKey},
};
/// Opaque player identifier. Cannot contain spaces, must be shorter than 32. /// Opaque player identifier. Cannot contain spaces, must be shorter than 32.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize)] #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize)]
@ -63,9 +61,7 @@ impl PlayerConnection {
} }
pub async fn join_room(&mut self, room_id: RoomId) -> Result<JoinResult> { pub async fn join_room(&mut self, room_id: RoomId) -> Result<JoinResult> {
self.player_handle self.player_handle.join_room(room_id, self.connection_id.clone()).await
.join_room(room_id, self.connection_id.clone())
.await
} }
pub async fn change_topic(&mut self, room_id: RoomId, new_topic: Str) -> Result<()> { pub async fn change_topic(&mut self, room_id: RoomId, new_topic: Str) -> Result<()> {
@ -100,9 +96,7 @@ impl PlayerConnection {
pub async fn get_rooms(&self) -> Result<Vec<RoomInfo>> { pub async fn get_rooms(&self) -> Result<Vec<RoomInfo>> {
let (promise, deferred) = oneshot(); let (promise, deferred) = oneshot();
self.player_handle self.player_handle.send(PlayerCommand::GetRooms(promise)).await;
.send(PlayerCommand::GetRooms(promise))
.await;
Ok(deferred.await?) Ok(deferred.await?)
} }
} }
@ -126,27 +120,14 @@ impl PlayerHandle {
} }
} }
pub async fn send_message( pub async fn send_message(&self, room_id: RoomId, connection_id: ConnectionId, body: Str) -> Result<()> {
&self,
room_id: RoomId,
connection_id: ConnectionId,
body: Str,
) -> Result<()> {
let (promise, deferred) = oneshot(); let (promise, deferred) = oneshot();
let cmd = Cmd::SendMessage { let cmd = Cmd::SendMessage { room_id, body, promise };
room_id,
body,
promise,
};
let _ = self.tx.send(PlayerCommand::Cmd(cmd, connection_id)).await; let _ = self.tx.send(PlayerCommand::Cmd(cmd, connection_id)).await;
Ok(deferred.await?) Ok(deferred.await?)
} }
pub async fn join_room( pub async fn join_room(&self, room_id: RoomId, connection_id: ConnectionId) -> Result<JoinResult> {
&self,
room_id: RoomId,
connection_id: ConnectionId,
) -> Result<JoinResult> {
let (promise, deferred) = oneshot(); let (promise, deferred) = oneshot();
let cmd = Cmd::JoinRoom { room_id, promise }; let cmd = Cmd::JoinRoom { room_id, promise };
let _ = self.tx.send(PlayerCommand::Cmd(cmd, connection_id)).await; let _ = self.tx.send(PlayerCommand::Cmd(cmd, connection_id)).await;
@ -230,12 +211,8 @@ pub enum Updates {
#[derive(Clone)] #[derive(Clone)]
pub struct PlayerRegistry(Arc<RwLock<PlayerRegistryInner>>); pub struct PlayerRegistry(Arc<RwLock<PlayerRegistryInner>>);
impl PlayerRegistry { impl PlayerRegistry {
pub fn empty( pub fn empty(room_registry: RoomRegistry, metrics: &mut MetricsRegistry) -> Result<PlayerRegistry> {
room_registry: RoomRegistry, let metric_active_players = IntGauge::new("chat_players_active", "Number of alive player actors")?;
metrics: &mut MetricsRegistry,
) -> Result<PlayerRegistry> {
let metric_active_players =
IntGauge::new("chat_players_active", "Number of alive player actors")?;
metrics.register(Box::new(metric_active_players.clone()))?; metrics.register(Box::new(metric_active_players.clone()))?;
let inner = PlayerRegistryInner { let inner = PlayerRegistryInner {
room_registry, room_registry,
@ -375,8 +352,7 @@ impl Player {
return; return;
} }
}; };
room.subscribe(self.player_id.clone(), self.handle.clone()) room.subscribe(self.player_id.clone(), self.handle.clone()).await;
.await;
self.my_rooms.insert(room_id.clone(), room.clone()); self.my_rooms.insert(room_id.clone(), room.clone());
let room_info = room.get_room_info().await; let room_info = room.get_room_info().await;
let _ = promise.send(JoinResult::Success(room_info)); let _ = promise.send(JoinResult::Success(room_info));
@ -399,15 +375,10 @@ impl Player {
}; };
self.broadcast_update(update, connection_id).await; self.broadcast_update(update, connection_id).await;
} }
Cmd::SendMessage { Cmd::SendMessage { room_id, body, promise } => {
room_id,
body,
promise,
} => {
let room = self.rooms.get_room(&room_id).await; let room = self.rooms.get_room(&room_id).await;
if let Some(room) = room { if let Some(room) = room {
room.send_message(self.player_id.clone(), body.clone()) room.send_message(self.player_id.clone(), body.clone()).await;
.await;
} else { } else {
tracing::info!("no room found"); tracing::info!("no room found");
} }
@ -426,8 +397,7 @@ impl Player {
} => { } => {
let room = self.rooms.get_room(&room_id).await; let room = self.rooms.get_room(&room_id).await;
if let Some(mut room) = room { if let Some(mut room) = room {
room.set_topic(self.player_id.clone(), new_topic.clone()) room.set_topic(self.player_id.clone(), new_topic.clone()).await;
.await;
} else { } else {
tracing::info!("no room found"); tracing::info!("no room found");
} }

View File

@ -0,0 +1,17 @@
pub use std::future::Future;
pub use tokio::pin;
pub use tokio::select;
pub use tokio::sync::oneshot::{channel as oneshot, Receiver as Deferred, Sender as Promise};
pub use tokio::task::JoinHandle;
pub mod log {
pub use tracing::{debug, error, info, warn};
}
pub type Result<T> = std::result::Result<T, anyhow::Error>;
pub type Str = std::sync::Arc<str>;
pub fn fail(msg: &str) -> anyhow::Error {
anyhow::Error::msg(msg.to_owned())
}

View File

@ -93,10 +93,10 @@ impl Storage {
Ok(()) Ok(())
} }
pub async fn close(mut self) -> Result<()> { pub async fn close(self) -> Result<()> {
let res = match Arc::try_unwrap(self.conn) { let res = match Arc::try_unwrap(self.conn) {
Ok(e) => e, Ok(e) => e,
Err(e) => return Err(fail("failed to acquire DB ownership on shutdown")), Err(_) => return Err(fail("failed to acquire DB ownership on shutdown")),
}; };
let res = res.into_inner(); let res = res.into_inner();
res.close().await?; res.close().await?;

View File

@ -1,17 +1,13 @@
//! Domain of rooms — chats with multiple participants. //! Domain of rooms — chats with multiple participants.
use std::{ use std::{collections::HashMap, hash::Hash, sync::Arc};
collections::HashMap,
hash::Hash,
sync::Arc,
};
use prometheus::{IntGauge, Registry as MetricRegistry}; use prometheus::{IntGauge, Registry as MetricRegistry};
use serde::Serialize; use serde::Serialize;
use tokio::sync::RwLock as AsyncRwLock; use tokio::sync::RwLock as AsyncRwLock;
use crate::core::player::{PlayerHandle, PlayerId, Updates}; use crate::player::{PlayerHandle, PlayerId, Updates};
use crate::core::repo::Storage;
use crate::prelude::*; use crate::prelude::*;
use crate::repo::Storage;
/// Opaque room id /// Opaque room id
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize)] #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize)]
@ -20,9 +16,7 @@ impl RoomId {
pub fn from(str: impl Into<Str>) -> Result<RoomId> { pub fn from(str: impl Into<Str>) -> Result<RoomId> {
let bytes = str.into(); let bytes = str.into();
if bytes.len() > 32 { if bytes.len() > 32 {
return Err(anyhow::Error::msg( return Err(anyhow::Error::msg("Room name cannot be longer than 32 symbols"));
"Room name cannot be longer than 32 symbols",
));
} }
if bytes.contains(' ') { if bytes.contains(' ') {
return Err(anyhow::Error::msg("Room name cannot contain spaces")); return Err(anyhow::Error::msg("Room name cannot contain spaces"));
@ -42,8 +36,7 @@ impl RoomId {
pub struct RoomRegistry(Arc<AsyncRwLock<RoomRegistryInner>>); pub struct RoomRegistry(Arc<AsyncRwLock<RoomRegistryInner>>);
impl RoomRegistry { impl RoomRegistry {
pub fn new(metrics: &mut MetricRegistry, storage: Storage) -> Result<RoomRegistry> { pub fn new(metrics: &mut MetricRegistry, storage: Storage) -> Result<RoomRegistry> {
let metric_active_rooms = let metric_active_rooms = IntGauge::new("chat_rooms_active", "Number of alive room actors")?;
IntGauge::new("chat_rooms_active", "Number of alive room actors")?;
metrics.register(Box::new(metric_active_rooms.clone()))?; metrics.register(Box::new(metric_active_rooms.clone()))?;
let inner = RoomRegistryInner { let inner = RoomRegistryInner {
rooms: HashMap::new(), rooms: HashMap::new(),
@ -140,7 +133,7 @@ impl RoomHandle {
pub async fn send_message(&self, player_id: PlayerId, body: Str) { pub async fn send_message(&self, player_id: PlayerId, body: Str) {
let mut lock = self.0.write().await; let mut lock = self.0.write().await;
let res = lock.send_message(player_id, body).await; let res = lock.send_message(player_id, body).await;
if let Err(err) = res { if let Err(err) = res {
log::warn!("Failed to send message: {err:?}"); log::warn!("Failed to send message: {err:?}");
} }
@ -150,11 +143,7 @@ impl RoomHandle {
let lock = self.0.read().await; let lock = self.0.read().await;
RoomInfo { RoomInfo {
id: lock.room_id.clone(), id: lock.room_id.clone(),
members: lock members: lock.subscriptions.keys().map(|x| x.clone()).collect::<Vec<_>>(),
.subscriptions
.keys()
.map(|x| x.clone())
.collect::<Vec<_>>(),
topic: lock.topic.clone(), topic: lock.topic.clone(),
} }
} }
@ -191,7 +180,9 @@ impl Room {
async fn send_message(&mut self, author_id: PlayerId, body: Str) -> Result<()> { async fn send_message(&mut self, author_id: PlayerId, body: Str) -> Result<()> {
tracing::info!("Adding a message to room"); tracing::info!("Adding a message to room");
self.storage.insert_message(self.storage_id, self.message_count, &body).await?; self.storage
.insert_message(self.storage_id, self.message_count, &body)
.await?;
self.message_count += 1; self.message_count += 1;
let update = Updates::NewMessage { let update = Updates::NewMessage {
room_id: self.room_id.clone(), room_id: self.room_id.clone(),

View File

@ -5,7 +5,6 @@
impl_trait_in_assoc_type impl_trait_in_assoc_type
)] )]
mod core;
mod prelude; mod prelude;
mod projections; mod projections;
mod util; mod util;
@ -19,9 +18,9 @@ use figment::{providers::Toml, Figment};
use prometheus::Registry as MetricsRegistry; use prometheus::Registry as MetricsRegistry;
use serde::Deserialize; use serde::Deserialize;
use crate::core::player::PlayerRegistry; use lavina_core::player::PlayerRegistry;
use crate::core::repo::Storage; use lavina_core::repo::Storage;
use crate::core::room::RoomRegistry; use lavina_core::room::RoomRegistry;
use crate::prelude::*; use crate::prelude::*;
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
@ -29,7 +28,7 @@ struct ServerConfig {
telemetry: util::telemetry::ServerConfig, telemetry: util::telemetry::ServerConfig,
irc: projections::irc::ServerConfig, irc: projections::irc::ServerConfig,
xmpp: projections::xmpp::ServerConfig, xmpp: projections::xmpp::ServerConfig,
storage: core::repo::StorageConfig, storage: lavina_core::repo::StorageConfig,
} }
#[derive(Parser)] #[derive(Parser)]

View File

@ -12,9 +12,9 @@ use tokio::net::tcp::{ReadHalf, WriteHalf};
use tokio::net::{TcpListener, TcpStream}; use tokio::net::{TcpListener, TcpStream};
use tokio::sync::mpsc::channel; use tokio::sync::mpsc::channel;
use crate::core::player::*; use lavina_core::player::*;
use crate::core::repo::Storage; use lavina_core::repo::Storage;
use crate::core::room::{RoomId, RoomInfo, RoomRegistry}; use lavina_core::room::{RoomId, RoomInfo, RoomRegistry};
use crate::prelude::*; use crate::prelude::*;
use proto_irc::client::{client_message, ClientMessage}; use proto_irc::client::{client_message, ClientMessage};
use proto_irc::server::{AwayStatus, ServerMessage, ServerMessageBody}; use proto_irc::server::{AwayStatus, ServerMessage, ServerMessageBody};

View File

@ -19,8 +19,8 @@ use tokio::sync::mpsc::channel;
use tokio_rustls::rustls::{Certificate, PrivateKey}; use tokio_rustls::rustls::{Certificate, PrivateKey};
use tokio_rustls::TlsAcceptor; use tokio_rustls::TlsAcceptor;
use crate::core::player::{PlayerConnection, PlayerId, PlayerRegistry}; use lavina_core::player::{PlayerConnection, PlayerId, PlayerRegistry};
use crate::core::room::{RoomId, RoomRegistry}; use lavina_core::room::{RoomId, RoomRegistry};
use crate::prelude::*; use crate::prelude::*;
use proto_xmpp::bind::{BindResponse, Jid, Name, Resource, Server}; use proto_xmpp::bind::{BindResponse, Jid, Name, Resource, Server};
use proto_xmpp::client::{Iq, Message, MessageType, Presence}; use proto_xmpp::client::{Iq, Message, MessageType, Presence};
@ -332,7 +332,7 @@ async fn socket_final(
update = user_handle.receiver.recv() => { update = user_handle.receiver.recv() => {
if let Some(update) = update { if let Some(update) = update {
match update { match update {
crate::core::player::Updates::NewMessage { room_id, author_id, body } => { lavina_core::player::Updates::NewMessage { room_id, author_id, body } => {
Message { Message {
to: Some(Jid { to: Some(Jid {
name: Some(authenticated.xmpp_name.clone()), name: Some(authenticated.xmpp_name.clone()),

View File

@ -1,6 +1,5 @@
use crate::prelude::*; use crate::prelude::*;
pub mod table;
pub mod telemetry; pub mod telemetry;
#[cfg(test)] #[cfg(test)]
pub mod testkit; pub mod testkit;

View File

@ -11,8 +11,8 @@ use prometheus::{Encoder, Registry as MetricsRegistry, TextEncoder};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tokio::net::TcpListener; use tokio::net::TcpListener;
use crate::core::repo::Storage; use lavina_core::repo::Storage;
use crate::core::room::RoomRegistry; use lavina_core::room::RoomRegistry;
use crate::prelude::*; use crate::prelude::*;
use crate::util::Terminator; use crate::util::Terminator;