Skip to content

Commit

Permalink
Use tokio::sync::RwLock
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernauer committed May 27, 2024
1 parent ce3d9a7 commit 04cc325
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions rust/stackable-cockpit/src/utils/k8s/client.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::BTreeMap, string::FromUtf8Error, sync::RwLock};
use std::{collections::BTreeMap, string::FromUtf8Error};

use k8s_openapi::api::{
apps::v1::{Deployment, StatefulSet},
Expand All @@ -13,6 +13,7 @@ use kube::{
use serde::Deserialize;
use snafu::{OptionExt, ResultExt, Snafu};
use stackable_operator::{commons::listener::Listener, kvp::Labels};
use tokio::sync::RwLock;

use crate::{
platform::{cluster, credentials::Credentials},
Expand Down Expand Up @@ -391,15 +392,15 @@ impl Client {
&self,
gvk: &GroupVersionKind,
) -> Result<Option<(ApiResource, ApiCapabilities)>> {
let resolved = self.discovery.read().unwrap().resolve_gvk(gvk);
let resolved = self.discovery.read().await.resolve_gvk(gvk);

Ok(match resolved {
Some(resolved) => Some(resolved),
None => {
tracing::warn!(?gvk, "Discovery did not include gvk, re-running discovery");

// We take the lock early here to avoid running multiple discoveries in parallel (as they are expensive)
let mut old_discovery = self.discovery.write().unwrap();
let mut old_discovery = self.discovery.write().await;

// We create a new Discovery object here, as [`Discovery::run`] consumes self
let new_discovery = Self::run_discovery(self.client.clone()).await?;
Expand All @@ -408,7 +409,7 @@ impl Client {
// discovery = discovery.run().await.context(GVKDiscoveryRunSnafu)?;
// Release the lock as quickly as possible
drop(old_discovery);
self.discovery.read().unwrap().resolve_gvk(gvk)
self.discovery.read().await.resolve_gvk(gvk)
}
})
}
Expand Down

0 comments on commit 04cc325

Please sign in to comment.