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;
26
27pub const MAX_RETRY_ATTEMPTS:usize = 3;
29
30pub const RETRY_BASE_DELAY_MS:u64 = 100;
32
33pub const MAX_MESSAGE_SIZE_BYTES:usize = 4 * 1024 * 1024;
35
36pub const HEALTH_CHECK_INTERVAL_MS:u64 = 30000;
38
39#[allow(dead_code)]
41pub const CONNECTION_TIMEOUT_MS:u64 = 10000;
42
43pub const NOTIFICATION_BROADCAST_CAPACITY:usize = 4096;
47
48pub struct ConnectionMetadata {
50 pub LastActivity:Instant,
51
52 pub FailureCount:usize,
53
54 pub IsHealthy:bool,
55}
56
57lazy_static! {
58 pub static ref SIDECAR_CLIENTS: Arc<Mutex<HashMap<String, CocoonClient>>> = Arc::new(Mutex::new(HashMap::new()));
59 pub static ref CONNECTION_METADATA: Arc<Mutex<HashMap<String, ConnectionMetadata>>> =
60 Arc::new(Mutex::new(HashMap::new()));
61 pub static ref NOTIFICATION_BROADCAST: tokio::sync::broadcast::Sender<NotificationFrame::Struct> = {
62 let (Sender, _) = tokio::sync::broadcast::channel(NOTIFICATION_BROADCAST_CAPACITY);
63
64 Sender
65 };
66}
67
68pub static SHUTDOWN_FLAG:AtomicBool = AtomicBool::new(false);
72
73pub fn ShutdownFlagStore(Value:bool) { SHUTDOWN_FLAG.store(Value, Ordering::Relaxed); }
74
75pub fn ShutdownFlagLoad() -> bool { SHUTDOWN_FLAG.load(Ordering::Relaxed) }
76
77pub fn RecordSideCarFailure(SideCarIdentifier:&str) {
79 let mut Metadata = CONNECTION_METADATA.lock();
80
81 if let Some(Connection) = Metadata.get_mut(SideCarIdentifier) {
82 Connection.FailureCount += 1;
83
84 Connection.IsHealthy = false;
85 }
86}
87
88pub fn UpdateSideCarActivity(SideCarIdentifier:&str) {
90 let mut Metadata = CONNECTION_METADATA.lock();
91
92 if let Some(Connection) = Metadata.get_mut(SideCarIdentifier) {
93 Connection.LastActivity = Instant::now();
94
95 Connection.FailureCount = 0;
96
97 Connection.IsHealthy = true;
98 }
99}
100
101pub fn ValidateMessageSize(Data:&[u8]) -> Result<(), VineError> {
105 if Data.len() > MAX_MESSAGE_SIZE_BYTES {
106 Err(VineError::MessageTooLarge { ActualSize:Data.len(), MaxSize:MAX_MESSAGE_SIZE_BYTES })
107 } else {
108 Ok(())
109 }
110}