Skip to content

Commit

Permalink
ConfigState::cluster_state return Option<ClusterInformation>
Browse files Browse the repository at this point in the history
  • Loading branch information
Keksoj committed Nov 22, 2023
1 parent 4d0c51f commit c193757
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 33 deletions.
4 changes: 2 additions & 2 deletions bin/src/command/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1057,7 +1057,7 @@ impl CommandServer {
),
Some(RequestType::QueryClusterById(cluster_id)) => Some(
ContentType::Clusters(ClusterInformations {
vec: vec![self.state.cluster_state(cluster_id)],
vec: self.state.cluster_state(cluster_id).into_iter().collect(),
})
.into(),
),
Expand All @@ -1067,7 +1067,7 @@ impl CommandServer {
.get_cluster_ids_by_domain(domain.hostname.clone(), domain.path.clone());
let vec = cluster_ids
.iter()
.map(|cluster_id| self.state.cluster_state(cluster_id))
.filter_map(|cluster_id| self.state.cluster_state(cluster_id))
.collect();
Some(ContentType::Clusters(ClusterInformations { vec }).into())
}
Expand Down
64 changes: 36 additions & 28 deletions command/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1168,43 +1168,51 @@ impl ConfigState {

/// Gives details about a given cluster.
/// Types like `HttpFrontend` are converted into protobuf ones, like `RequestHttpFrontend`
pub fn cluster_state(&self, cluster_id: &str) -> ClusterInformation {
let mut http_frontends = Vec::new();
let mut https_frontends = Vec::new();
let mut tcp_frontends = Vec::new();
let mut backends = Vec::new();

for http_frontend in self.http_fronts.values() {
if let Some(id) = &http_frontend.cluster_id {
if id == cluster_id {
http_frontends.push(http_frontend.clone().into());
}
}
pub fn cluster_state(&self, cluster_id: &str) -> Option<ClusterInformation> {
let configuration = self.clusters.get(cluster_id).cloned();
if configuration.is_none() {
return None;
}

for https_frontend in self.https_fronts.values() {
if let Some(id) = &https_frontend.cluster_id {
if id == cluster_id {
https_frontends.push(https_frontend.clone().into());
}
}
}
let http_frontends: Vec<RequestHttpFrontend> = self
.http_fronts
.values()
.filter(|front| front.cluster_id.as_deref() == Some(cluster_id))
.map(|front| front.clone().into())
.collect();

for tcp_f in self.tcp_fronts.get(cluster_id).cloned().unwrap_or_default() {
tcp_frontends.push(tcp_f.clone().into());
}
let https_frontends: Vec<RequestHttpFrontend> = self
.https_fronts
.values()
.filter(|front| front.cluster_id.as_deref() == Some(cluster_id))
.map(|front| front.clone().into())
.collect();

for backend in self.backends.get(cluster_id).cloned().unwrap_or_default() {
backends.push(backend.clone().into())
}
let tcp_frontends: Vec<RequestTcpFrontend> = self
.tcp_fronts
.get(cluster_id)
.cloned()
.unwrap_or_default()
.iter()
.map(|front| front.clone().into())
.collect();

ClusterInformation {
configuration: self.clusters.get(cluster_id).cloned(),
let backends: Vec<AddBackend> = self
.backends
.get(cluster_id)
.cloned()
.unwrap_or_default()
.iter()
.map(|backend| backend.clone().into())
.collect();

Some(ClusterInformation {
configuration,
http_frontends,
https_frontends,
tcp_frontends,
backends,
}
})
}

pub fn count_backends(&self) -> usize {
Expand Down
5 changes: 4 additions & 1 deletion lib/src/protocol/kawa_h1/parser.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::{fmt::{self, Write}, str::from_utf8_unchecked};
use std::{
fmt::{self, Write},
str::from_utf8_unchecked,
};

use nom::{
bytes::{self, complete::take_while},
Expand Down
8 changes: 6 additions & 2 deletions lib/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,11 @@ impl Server {
push_queue(WorkerResponse::ok_with_content(
message.id.clone(),
ContentType::Clusters(ClusterInformations {
vec: vec![self.config_state.cluster_state(cluster_id)],
vec: self
.config_state
.cluster_state(cluster_id)
.into_iter()
.collect(),
})
.into(),
));
Expand All @@ -915,7 +919,7 @@ impl Server {
.get_cluster_ids_by_domain(domain.hostname.clone(), domain.path.clone());
let vec = cluster_ids
.iter()
.map(|cluster_id| self.config_state.cluster_state(cluster_id))
.filter_map(|cluster_id| self.config_state.cluster_state(cluster_id))
.collect();

push_queue(WorkerResponse::ok_with_content(
Expand Down

0 comments on commit c193757

Please sign in to comment.