Skip to content

Commit

Permalink
shrink lock scope
Browse files Browse the repository at this point in the history
  • Loading branch information
ariesdevil committed Mar 10, 2022
1 parent 23cffad commit fe2fd5e
Showing 1 changed file with 29 additions and 23 deletions.
52 changes: 29 additions & 23 deletions query/src/sessions/session_mgr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,31 +125,37 @@ impl SessionManager {
}

pub async fn create_session(self: &Arc<Self>, typ: impl Into<String>) -> Result<SessionRef> {
let mut sessions = self.active_sessions.write().await;
{
let sessions = self.active_sessions.read().await;
if sessions.len() == self.max_sessions {
return Err(ErrorCode::TooManyUserConnections(
"The current accept connection has exceeded mysql_handler_thread_num config",
));
}
}
let session = Session::try_create(
self.conf.clone(),
uuid::Uuid::new_v4().to_string(),
typ.into(),
self.clone(),
)
.await?;

match sessions.len() == self.max_sessions {
true => Err(ErrorCode::TooManyUserConnections(
let mut sessions = self.active_sessions.write().await;
if sessions.len() < self.max_sessions {
label_counter(
super::metrics::METRIC_SESSION_CONNECT_NUMBERS,
&self.conf.query.tenant_id,
&self.conf.query.cluster_id,
);

sessions.insert(session.get_id(), session.clone());

Ok(SessionRef::create(session))
} else {
Err(ErrorCode::TooManyUserConnections(
"The current accept connection has exceeded mysql_handler_thread_num config",
)),
false => {
let session = Session::try_create(
self.conf.clone(),
uuid::Uuid::new_v4().to_string(),
typ.into(),
self.clone(),
)
.await?;

label_counter(
super::metrics::METRIC_SESSION_CONNECT_NUMBERS,
&self.conf.query.tenant_id,
&self.conf.query.cluster_id,
);

sessions.insert(session.get_id(), session.clone());

Ok(SessionRef::create(session))
}
))
}
}

Expand Down

0 comments on commit fe2fd5e

Please sign in to comment.