reused grpc connections, not a constant refreshes anymore, all fixed now, keep on fixing other bugs

This commit is contained in:
Priec
2025-09-12 18:15:46 +02:00
parent 85c7c89c28
commit cae47da5f2
6 changed files with 33 additions and 198 deletions

View File

@@ -14,12 +14,20 @@ pub struct AuthClient {
impl AuthClient {
pub async fn new() -> Result<Self> {
// Kept for backward compatibility; opens a new connection.
let client = AuthServiceClient::connect("http://[::1]:50051")
.await
.context("Failed to connect to auth service")?;
Ok(Self { client })
}
/// Preferred: reuse an existing Channel (from GrpcClient).
pub async fn with_channel(channel: Channel) -> Result<Self> {
Ok(Self {
client: AuthServiceClient::new(channel),
})
}
/// Login user via gRPC.
pub async fn login(&mut self, identifier: String, password: String) -> Result<LoginResponse> {
let request = tonic::Request::new(LoginRequest { identifier, password });

View File

@@ -2,7 +2,9 @@
use common::proto::komp_ac::common::Empty;
use common::proto::komp_ac::table_structure::table_structure_service_client::TableStructureServiceClient;
use common::proto::komp_ac::table_structure::{GetTableStructureRequest, TableStructureResponse};
use common::proto::komp_ac::table_structure::{
GetTableStructureRequest, TableStructureResponse,
};
use common::proto::komp_ac::table_definition::{
table_definition_client::TableDefinitionClient,
PostTableDefinitionRequest, ProfileTreeResponse, TableDefinitionResponse,
@@ -26,11 +28,13 @@ use crate::search::SearchGrpc;
use common::proto::komp_ac::search::SearchResponse;
use anyhow::{Context, Result};
use std::collections::HashMap;
use tonic::transport::Channel;
use tonic::transport::{Channel, Endpoint};
use prost_types::Value;
use std::time::Duration;
#[derive(Clone)]
pub struct GrpcClient {
channel: Channel,
table_structure_client: TableStructureServiceClient<Channel>,
table_definition_client: TableDefinitionClient<Channel>,
table_script_client: TableScriptClient<Channel>,
@@ -40,7 +44,14 @@ pub struct GrpcClient {
impl GrpcClient {
pub async fn new() -> Result<Self> {
let channel = Channel::from_static("http://[::1]:50051")
let endpoint = Endpoint::from_static("http://[::1]:50051")
.connect_timeout(Duration::from_secs(5))
.tcp_keepalive(Some(Duration::from_secs(30)))
.keep_alive_while_idle(true)
.http2_keep_alive_interval(Duration::from_secs(15))
.keep_alive_timeout(Duration::from_secs(5));
let channel = endpoint
.connect()
.await
.context("Failed to create gRPC channel")?;
@@ -54,6 +65,7 @@ impl GrpcClient {
let search_client = SearchGrpc::new(channel.clone());
Ok(Self {
channel,
table_structure_client,
table_definition_client,
table_script_client,
@@ -62,6 +74,11 @@ impl GrpcClient {
})
}
// Expose the shared channel so other typed clients can reuse it.
pub fn channel(&self) -> Channel {
self.channel.clone()
}
pub async fn get_table_structure(
&mut self,
profile_name: String,