|
| 1 | +// Copyright 2025 The kmesh Authors |
| 2 | +// |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +// |
| 16 | +// |
| 17 | + |
| 18 | +use super::{global_internal_connection_factory, AsyncStream}; |
| 19 | +use crate::{Error, Result}; |
| 20 | + |
| 21 | +#[derive(Debug, Clone)] |
| 22 | +pub struct InternalClusterConnector { |
| 23 | + listener_name: String, |
| 24 | + endpoint_id: Option<String>, |
| 25 | +} |
| 26 | + |
| 27 | +impl InternalClusterConnector { |
| 28 | + pub fn new(listener_name: String, endpoint_id: Option<String>) -> Self { |
| 29 | + Self { listener_name, endpoint_id } |
| 30 | + } |
| 31 | + |
| 32 | + pub fn listener_name(&self) -> &str { |
| 33 | + &self.listener_name |
| 34 | + } |
| 35 | + |
| 36 | + pub fn endpoint_id(&self) -> Option<&str> { |
| 37 | + self.endpoint_id.as_deref() |
| 38 | + } |
| 39 | + |
| 40 | + pub async fn connect(&self) -> Result<AsyncStream> { |
| 41 | + let factory = global_internal_connection_factory(); |
| 42 | + |
| 43 | + if !factory.is_listener_active(&self.listener_name).await { |
| 44 | + return Err(Error::new(format!( |
| 45 | + "Internal listener '{}' is not active or not registered", |
| 46 | + self.listener_name |
| 47 | + ))); |
| 48 | + } |
| 49 | + |
| 50 | + factory.connect_to_listener(&self.listener_name, self.endpoint_id.clone()).await |
| 51 | + } |
| 52 | + |
| 53 | + pub async fn is_available(&self) -> bool { |
| 54 | + let factory = global_internal_connection_factory(); |
| 55 | + factory.is_listener_active(&self.listener_name).await |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +#[derive(Debug, Clone)] |
| 60 | +pub struct InternalChannelConnector { |
| 61 | + connector: InternalClusterConnector, |
| 62 | + cluster_name: &'static str, |
| 63 | +} |
| 64 | + |
| 65 | +impl InternalChannelConnector { |
| 66 | + pub fn new(listener_name: String, cluster_name: &'static str, endpoint_id: Option<String>) -> Self { |
| 67 | + let connector = InternalClusterConnector::new(listener_name, endpoint_id); |
| 68 | + |
| 69 | + Self { connector, cluster_name } |
| 70 | + } |
| 71 | + |
| 72 | + pub fn cluster_name(&self) -> &'static str { |
| 73 | + self.cluster_name |
| 74 | + } |
| 75 | + |
| 76 | + pub fn listener_name(&self) -> &str { |
| 77 | + self.connector.listener_name() |
| 78 | + } |
| 79 | + |
| 80 | + pub async fn connect(&self) -> Result<InternalChannel> { |
| 81 | + let stream = self.connector.connect().await?; |
| 82 | + |
| 83 | + Ok(InternalChannel { |
| 84 | + stream, |
| 85 | + cluster_name: self.cluster_name, |
| 86 | + listener_name: self.connector.listener_name().to_string(), |
| 87 | + endpoint_id: self.connector.endpoint_id().map(|s| s.to_string()), |
| 88 | + }) |
| 89 | + } |
| 90 | + |
| 91 | + pub async fn is_available(&self) -> bool { |
| 92 | + self.connector.is_available().await |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +pub struct InternalChannel { |
| 97 | + pub stream: AsyncStream, |
| 98 | + pub cluster_name: &'static str, |
| 99 | + pub listener_name: String, |
| 100 | + pub endpoint_id: Option<String>, |
| 101 | +} |
| 102 | + |
| 103 | +impl InternalChannel { |
| 104 | + pub fn cluster_name(&self) -> &'static str { |
| 105 | + self.cluster_name |
| 106 | + } |
| 107 | + |
| 108 | + pub fn listener_name(&self) -> &str { |
| 109 | + &self.listener_name |
| 110 | + } |
| 111 | + |
| 112 | + pub fn endpoint_id(&self) -> Option<&str> { |
| 113 | + self.endpoint_id.as_deref() |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +pub mod cluster_helpers { |
| 118 | + use super::*; |
| 119 | + use orion_configuration::config::cluster::InternalEndpointAddress; |
| 120 | + |
| 121 | + pub fn create_internal_connector( |
| 122 | + internal_addr: &InternalEndpointAddress, |
| 123 | + cluster_name: &'static str, |
| 124 | + ) -> InternalChannelConnector { |
| 125 | + InternalChannelConnector::new( |
| 126 | + internal_addr.server_listener_name.to_string(), |
| 127 | + cluster_name, |
| 128 | + internal_addr.endpoint_id.as_ref().map(|s| s.to_string()), |
| 129 | + ) |
| 130 | + } |
| 131 | + |
| 132 | + pub async fn is_internal_listener_available(listener_name: &str) -> bool { |
| 133 | + let factory = global_internal_connection_factory(); |
| 134 | + factory.is_listener_active(listener_name).await |
| 135 | + } |
| 136 | + |
| 137 | + pub async fn get_internal_connection_stats() -> crate::transport::InternalConnectionStats { |
| 138 | + let factory = global_internal_connection_factory(); |
| 139 | + factory.get_stats().await |
| 140 | + } |
| 141 | + |
| 142 | + pub async fn list_internal_listeners() -> Vec<String> { |
| 143 | + let factory = global_internal_connection_factory(); |
| 144 | + factory.list_listeners().await |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +#[cfg(test)] |
| 149 | +mod tests { |
| 150 | + use super::*; |
| 151 | + |
| 152 | + #[tokio::test] |
| 153 | + async fn test_internal_connector_creation() { |
| 154 | + let connector = InternalClusterConnector::new(String::from("test_listener"), Some(String::from("endpoint1"))); |
| 155 | + assert_eq!(connector.listener_name(), "test_listener"); |
| 156 | + assert_eq!(connector.endpoint_id(), Some("endpoint1")); |
| 157 | + } |
| 158 | + |
| 159 | + #[tokio::test] |
| 160 | + async fn test_connection_to_non_existent_listener() { |
| 161 | + let connector = InternalClusterConnector::new(String::from("non_existent_listener"), None); |
| 162 | + let result = connector.connect().await; |
| 163 | + assert!(result.is_err()); |
| 164 | + } |
| 165 | + |
| 166 | + #[tokio::test] |
| 167 | + async fn test_availability_check() { |
| 168 | + let connector = InternalClusterConnector::new(String::from("non_existent_listener"), None); |
| 169 | + assert!(!connector.is_available().await); |
| 170 | + } |
| 171 | + |
| 172 | + #[tokio::test] |
| 173 | + async fn test_internal_channel_connector() { |
| 174 | + let channel_connector = InternalChannelConnector::new( |
| 175 | + String::from("test_listener"), |
| 176 | + "test_cluster", |
| 177 | + Some(String::from("endpoint1")), |
| 178 | + ); |
| 179 | + |
| 180 | + assert_eq!(channel_connector.cluster_name(), "test_cluster"); |
| 181 | + assert_eq!(channel_connector.listener_name(), "test_listener"); |
| 182 | + assert!(!channel_connector.is_available().await); |
| 183 | + } |
| 184 | + |
| 185 | + #[tokio::test] |
| 186 | + async fn test_cluster_helpers() { |
| 187 | + use cluster_helpers::*; |
| 188 | + use orion_configuration::config::cluster::InternalEndpointAddress; |
| 189 | + |
| 190 | + let internal_addr = InternalEndpointAddress { |
| 191 | + server_listener_name: String::from("test_listener").into(), |
| 192 | + endpoint_id: Some(String::from("endpoint1").into()), |
| 193 | + }; |
| 194 | + |
| 195 | + let connector = create_internal_connector(&internal_addr, "test_cluster"); |
| 196 | + assert_eq!(connector.cluster_name(), "test_cluster"); |
| 197 | + assert_eq!(connector.listener_name(), "test_listener"); |
| 198 | + |
| 199 | + assert!(!is_internal_listener_available("non_existent").await); |
| 200 | + |
| 201 | + let stats = get_internal_connection_stats().await; |
| 202 | + assert_eq!(stats.active_listeners, 0); |
| 203 | + |
| 204 | + let listeners = list_internal_listeners().await; |
| 205 | + assert!(listeners.is_empty()); |
| 206 | + } |
| 207 | +} |
0 commit comments