WIP: Introduce a db field to Session

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Antonio Scandurra 2022-11-17 17:30:26 +01:00
parent c34a5f3177
commit 0a4517f97e
3 changed files with 21 additions and 7 deletions

6
Cargo.lock generated
View file

@ -266,11 +266,12 @@ dependencies = [
[[package]] [[package]]
name = "async-lock" name = "async-lock"
version = "2.5.0" version = "2.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e97a171d191782fba31bb902b14ad94e24a68145032b7eedf871ab0bc0d077b6" checksum = "c8101efe8695a6c17e02911402145357e718ac92d3ff88ae8419e84b1707b685"
dependencies = [ dependencies = [
"event-listener", "event-listener",
"futures-lite",
] ]
[[package]] [[package]]
@ -1031,7 +1032,6 @@ name = "collab"
version = "0.2.2" version = "0.2.2"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"async-trait",
"async-tungstenite", "async-tungstenite",
"axum", "axum",
"axum-extra", "axum-extra",

View file

@ -19,7 +19,6 @@ rpc = { path = "../rpc" }
util = { path = "../util" } util = { path = "../util" }
anyhow = "1.0.40" anyhow = "1.0.40"
async-trait = "0.1.50"
async-tungstenite = "0.16" async-tungstenite = "0.16"
axum = { version = "0.5", features = ["json", "headers", "ws"] } axum = { version = "0.5", features = ["json", "headers", "ws"] }
axum-extra = { version = "0.3", features = ["erased-json"] } axum-extra = { version = "0.3", features = ["erased-json"] }

View file

@ -2,7 +2,7 @@ mod store;
use crate::{ use crate::{
auth, auth,
db::{self, ProjectId, RoomId, User, UserId}, db::{self, DefaultDb, ProjectId, RoomId, User, UserId},
AppState, Result, AppState, Result,
}; };
use anyhow::anyhow; use anyhow::anyhow;
@ -80,6 +80,17 @@ struct Response<R> {
struct Session { struct Session {
user_id: UserId, user_id: UserId,
connection_id: ConnectionId, connection_id: ConnectionId,
db: Arc<Mutex<DbHandle>>,
}
struct DbHandle(Arc<DefaultDb>);
impl Deref for DbHandle {
type Target = DefaultDb;
fn deref(&self) -> &Self::Target {
self.0.as_ref()
}
} }
impl<R: RequestMessage> Response<R> { impl<R: RequestMessage> Response<R> {
@ -352,6 +363,8 @@ impl Server {
let handle_io = handle_io.fuse(); let handle_io = handle_io.fuse();
futures::pin_mut!(handle_io); futures::pin_mut!(handle_io);
let db = Arc::new(Mutex::new(DbHandle(this.app_state.db.clone())));
// Handlers for foreground messages are pushed into the following `FuturesUnordered`. // Handlers for foreground messages are pushed into the following `FuturesUnordered`.
// This prevents deadlocks when e.g., client A performs a request to client B and // This prevents deadlocks when e.g., client A performs a request to client B and
// client B performs a request to client A. If both clients stop processing further // client B performs a request to client A. If both clients stop processing further
@ -382,6 +395,7 @@ impl Server {
let session = Session { let session = Session {
user_id, user_id,
connection_id, connection_id,
db: db.clone(),
}; };
let handle_message = (handler)(this.clone(), message, session); let handle_message = (handler)(this.clone(), message, session);
drop(span_enter); drop(span_enter);
@ -1409,9 +1423,10 @@ impl Server {
session: Session, session: Session,
) -> Result<()> { ) -> Result<()> {
let project_id = ProjectId::from_proto(request.project_id); let project_id = ProjectId::from_proto(request.project_id);
let project_connection_ids = self let project_connection_ids = session
.app_state
.db .db
.lock()
.await
.project_connection_ids(project_id, session.connection_id) .project_connection_ids(project_id, session.connection_id)
.await?; .await?;