-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-Authored-By: rezvaneh <[email protected]> Co-Authored-By: markmandel <[email protected]>
- Loading branch information
1 parent
49adc6a
commit cc35b7a
Showing
14 changed files
with
1,076 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use cached::CachedAsync; | ||
use tokio::sync::Mutex; | ||
|
||
use crate::xds::{ | ||
service::discovery::v3::DiscoveryResponse, DiscoveryServiceProvider, ResourceType, | ||
}; | ||
|
||
const CACHE_LIFESPAN_IN_SECONDS: u64 = 5; | ||
|
||
/// A generic [DiscoveryServiceProvider] cache, that will cache any matching | ||
/// request from the underlying provider for a limited duration. | ||
pub struct Cache { | ||
provider: Box<dyn DiscoveryServiceProvider>, | ||
cache: Mutex<cached::TimedCache<(String, u64, ResourceType), DiscoveryResponse>>, | ||
} | ||
|
||
impl Cache { | ||
pub fn new<P: DiscoveryServiceProvider + 'static>(provider: P) -> Self { | ||
Self { | ||
provider: Box::from(provider), | ||
cache: Mutex::new(cached::TimedCache::with_lifespan(CACHE_LIFESPAN_IN_SECONDS)), | ||
} | ||
} | ||
} | ||
|
||
#[tonic::async_trait] | ||
impl crate::xds::DiscoveryServiceProvider for Cache { | ||
async fn discovery_request( | ||
&self, | ||
node_id: &str, | ||
version: u64, | ||
kind: ResourceType, | ||
names: &[String], | ||
) -> Result<DiscoveryResponse, tonic::Status> { | ||
let mut lock = self.cache.lock().await; | ||
|
||
lock.try_get_or_set_with((node_id.to_owned(), version, kind), || { | ||
self.provider | ||
.discovery_request(node_id, version, kind, names) | ||
}) | ||
.await | ||
.map(|response| response.clone()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
mod agones; | ||
|
||
pub use agones::AgonesProvider; | ||
|
||
use crate::xds::{service::discovery::v3::DiscoveryResponse, ResourceType}; | ||
|
||
/// A trait over a discovery service provider responsible for returning | ||
/// the [DiscoveryResponse]s. The type of resource returned is based on the [ResourceType]. | ||
#[tonic::async_trait] | ||
pub trait DiscoveryServiceProvider: Send + Sync { | ||
async fn discovery_request( | ||
&self, | ||
node_id: &str, | ||
version: u64, | ||
kind: ResourceType, | ||
names: &[String], | ||
) -> Result<DiscoveryResponse, tonic::Status>; | ||
} |
Oops, something went wrong.