Mountain/Vine/Client/
Shared.rs1#![allow(non_snake_case)]
2
3use std::{
8 collections::HashMap,
9 sync::{
10 Arc,
11 atomic::{AtomicBool, Ordering},
12 },
13 time::Instant,
14};
15
16use lazy_static::lazy_static;
17use parking_lot::Mutex;
18
19use crate::Vine::{Client::NotificationFrame, Error::VineError, Generated::cocoon_service_client::CocoonServiceClient};
20
21pub type CocoonClient = CocoonServiceClient<tonic::transport::Channel>;
23
24pub const DEFAULT_TIMEOUT_MS:u64 = 5000;
26pub const MAX_RETRY_ATTEMPTS:usize = 3;
28pub const RETRY_BASE_DELAY_MS:u64 = 100;
30pub const MAX_MESSAGE_SIZE_BYTES:usize = 4 * 1024 * 1024;
32pub const HEALTH_CHECK_INTERVAL_MS:u64 = 30000;
34#[allow(dead_code)]
36pub const CONNECTION_TIMEOUT_MS:u64 = 10000;
37
38pub const NOTIFICATION_BROADCAST_CAPACITY:usize = 4096;
42
43pub struct ConnectionMetadata {
45 pub LastActivity:Instant,
46 pub FailureCount:usize,
47 pub IsHealthy:bool,
48}
49
50lazy_static! {
51 pub static ref SIDECAR_CLIENTS: Arc<Mutex<HashMap<String, CocoonClient>>> = Arc::new(Mutex::new(HashMap::new()));
52 pub static ref CONNECTION_METADATA: Arc<Mutex<HashMap<String, ConnectionMetadata>>> =
53 Arc::new(Mutex::new(HashMap::new()));
54 pub static ref NOTIFICATION_BROADCAST: tokio::sync::broadcast::Sender<NotificationFrame::Struct> = {
55 let (Sender, _) = tokio::sync::broadcast::channel(NOTIFICATION_BROADCAST_CAPACITY);
56 Sender
57 };
58}
59
60pub static SHUTDOWN_FLAG:AtomicBool = AtomicBool::new(false);
64
65pub fn ShutdownFlagStore(Value:bool) { SHUTDOWN_FLAG.store(Value, Ordering::Relaxed); }
66pub fn ShutdownFlagLoad() -> bool { SHUTDOWN_FLAG.load(Ordering::Relaxed) }
67
68pub fn RecordSideCarFailure(SideCarIdentifier:&str) {
70 let mut Metadata = CONNECTION_METADATA.lock();
71 if let Some(Connection) = Metadata.get_mut(SideCarIdentifier) {
72 Connection.FailureCount += 1;
73 Connection.IsHealthy = false;
74 }
75}
76
77pub fn UpdateSideCarActivity(SideCarIdentifier:&str) {
79 let mut Metadata = CONNECTION_METADATA.lock();
80 if let Some(Connection) = Metadata.get_mut(SideCarIdentifier) {
81 Connection.LastActivity = Instant::now();
82 Connection.FailureCount = 0;
83 Connection.IsHealthy = true;
84 }
85}
86
87pub fn ValidateMessageSize(Data:&[u8]) -> Result<(), VineError> {
91 if Data.len() > MAX_MESSAGE_SIZE_BYTES {
92 Err(VineError::MessageTooLarge { ActualSize:Data.len(), MaxSize:MAX_MESSAGE_SIZE_BYTES })
93 } else {
94 Ok(())
95 }
96}