TCP connection creation overhead fixed by cloning once created TCP connection. Huge performance gain on login and register. Utilizing gRPC
This commit is contained in:
@@ -229,7 +229,7 @@ impl EventHandler {
|
|||||||
UiContext::Login => {
|
UiContext::Login => {
|
||||||
let login_action_message = match index {
|
let login_action_message = match index {
|
||||||
0 => {
|
0 => {
|
||||||
login::initiate_login(login_state, app_state, self.login_result_sender.clone())
|
login::initiate_login(login_state, app_state, self.auth_client.clone(), self.login_result_sender.clone())
|
||||||
},
|
},
|
||||||
1 => login::back_to_main(login_state, app_state, buffer_state).await,
|
1 => login::back_to_main(login_state, app_state, buffer_state).await,
|
||||||
_ => "Invalid Login Option".to_string(),
|
_ => "Invalid Login Option".to_string(),
|
||||||
@@ -239,7 +239,7 @@ impl EventHandler {
|
|||||||
UiContext::Register => {
|
UiContext::Register => {
|
||||||
let register_action_message = match index {
|
let register_action_message = match index {
|
||||||
0 => {
|
0 => {
|
||||||
register::initiate_registration(register_state, app_state, self.register_result_sender.clone())
|
register::initiate_registration(register_state, app_state, self.auth_client.clone(), self.register_result_sender.clone())
|
||||||
},
|
},
|
||||||
1 => register::back_to_login(register_state, app_state, buffer_state).await,
|
1 => register::back_to_login(register_state, app_state, buffer_state).await,
|
||||||
_ => "Invalid Login Option".to_string(),
|
_ => "Invalid Login Option".to_string(),
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ use common::proto::multieko2::auth::{
|
|||||||
};
|
};
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
pub struct AuthClient {
|
pub struct AuthClient {
|
||||||
client: AuthServiceClient<Channel>,
|
client: AuthServiceClient<Channel>,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -146,6 +146,7 @@ pub async fn back_to_main(
|
|||||||
pub fn initiate_login(
|
pub fn initiate_login(
|
||||||
login_state: &LoginState,
|
login_state: &LoginState,
|
||||||
app_state: &mut AppState,
|
app_state: &mut AppState,
|
||||||
|
mut auth_client: AuthClient,
|
||||||
sender: mpsc::Sender<LoginResult>,
|
sender: mpsc::Sender<LoginResult>,
|
||||||
) -> String {
|
) -> String {
|
||||||
let username = login_state.username.clone();
|
let username = login_state.username.clone();
|
||||||
@@ -166,16 +167,12 @@ pub fn initiate_login(
|
|||||||
|
|
||||||
// 3. Spawn the login task
|
// 3. Spawn the login task
|
||||||
spawn(async move {
|
spawn(async move {
|
||||||
let login_outcome = match AuthClient::new().await {
|
// Use the passed-in (and moved) auth_client directly
|
||||||
Ok(mut auth_client) => {
|
let login_outcome = match auth_client.login(username.clone(), password).await
|
||||||
match auth_client.login(username.clone(), password).await
|
|
||||||
.with_context(|| format!("Spawned login task failed for identifier: {}", username))
|
.with_context(|| format!("Spawned login task failed for identifier: {}", username))
|
||||||
{
|
{
|
||||||
Ok(response) => LoginResult::Success(response),
|
Ok(response) => LoginResult::Success(response),
|
||||||
Err(e) => LoginResult::Failure(format!("{}", e)),
|
Err(e) => LoginResult::Failure(format!("{}", e)),
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(e) => LoginResult::ConnectionError(format!("Failed to create AuthClient: {}", e)),
|
|
||||||
};
|
};
|
||||||
// Send result back to the main UI thread
|
// Send result back to the main UI thread
|
||||||
if let Err(e) = sender.send(login_outcome).await {
|
if let Err(e) = sender.send(login_outcome).await {
|
||||||
|
|||||||
@@ -65,6 +65,7 @@ pub async fn back_to_login(
|
|||||||
pub fn initiate_registration(
|
pub fn initiate_registration(
|
||||||
register_state: &RegisterState,
|
register_state: &RegisterState,
|
||||||
app_state: &mut AppState,
|
app_state: &mut AppState,
|
||||||
|
mut auth_client: AuthClient,
|
||||||
sender: mpsc::Sender<RegisterResult>,
|
sender: mpsc::Sender<RegisterResult>,
|
||||||
) -> String {
|
) -> String {
|
||||||
// Clone necessary data
|
// Clone necessary data
|
||||||
@@ -87,21 +88,14 @@ pub fn initiate_registration(
|
|||||||
|
|
||||||
// 3. Spawn the registration task
|
// 3. Spawn the registration task
|
||||||
spawn(async move {
|
spawn(async move {
|
||||||
let register_outcome = match AuthClient::new().await {
|
|
||||||
Ok(mut auth_client) => {
|
|
||||||
// Handle optional fields correctly for the gRPC call
|
|
||||||
let password_opt = if password.is_empty() { None } else { Some(password) };
|
let password_opt = if password.is_empty() { None } else { Some(password) };
|
||||||
let password_conf_opt = if password_confirmation.is_empty() { None } else { Some(password_confirmation) };
|
let password_conf_opt = if password_confirmation.is_empty() { None } else { Some(password_confirmation) };
|
||||||
let role_opt = if role.is_empty() { None } else { Some(role) };
|
let role_opt = if role.is_empty() { None } else { Some(role) };
|
||||||
|
let register_outcome = match auth_client.register(username.clone(), email, password_opt, password_conf_opt, role_opt).await
|
||||||
match auth_client.register(username.clone(), email, password_opt, password_conf_opt, role_opt).await
|
|
||||||
.with_context(|| format!("Spawned register task failed for username: {}", username))
|
.with_context(|| format!("Spawned register task failed for username: {}", username))
|
||||||
{
|
{
|
||||||
Ok(response) => RegisterResult::Success(response),
|
Ok(response) => RegisterResult::Success(response),
|
||||||
Err(e) => RegisterResult::Failure(format!("{}", e)),
|
Err(e) => RegisterResult::Failure(format!("{}", e)),
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(e) => RegisterResult::ConnectionError(format!("Failed to create AuthClient: {}", e)),
|
|
||||||
};
|
};
|
||||||
// Send result back to the main UI thread
|
// Send result back to the main UI thread
|
||||||
if let Err(e) = sender.send(register_outcome).await {
|
if let Err(e) = sender.send(register_outcome).await {
|
||||||
|
|||||||
Reference in New Issue
Block a user