Add \ClientBuilder::discovery_cache_timeout()\ and \Client::rediscover()\ to support configurable caching of homeserver discovery data.
-
Notifications
You must be signed in to change notification settings - Fork 455
feat: add discovery_cache_timeout to ClientBuilder and rediscover() t… #6712
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
3ec9d58
16b5b72
40d0c09
9f83e5d
c6a5011
54d3d0f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -91,7 +91,8 @@ use crate::{ | |
| Account, AuthApi, AuthSession, Error, HttpError, Media, Pusher, RefreshTokenError, Result, | ||
| Room, SessionTokens, TransmissionProgress, | ||
| authentication::{ | ||
| AuthCtx, AuthData, ReloadSessionCallback, SaveSessionCallback, matrix::MatrixAuth, | ||
| AuthCtx, AuthData, ReloadSessionCallback, SaveSessionCallback, | ||
| matrix::MatrixAuth, | ||
| oauth::OAuth, | ||
| }, | ||
| client::{ | ||
|
|
@@ -448,12 +449,14 @@ impl ClientInner { | |
| #[cfg(feature = "experimental-search")] search_index_handler: SearchIndex, | ||
| thread_subscription_catchup: OnceCell<Arc<ThreadSubscriptionCatchup>>, | ||
| media_fetcher: Arc<dyn MediaFetcher>, | ||
| discovery_cache_timeout: Duration, | ||
| ) -> Arc<Self> { | ||
| let caches = ClientCaches { | ||
| supported_versions: Cache::with_value(supported_versions), | ||
| well_known: Cache::with_value(well_known), | ||
| server_metadata: Cache::new(), | ||
| homeserver_capabilities: Cache::new(), | ||
| discovery_cache_timeout, | ||
| }; | ||
|
|
||
| let client = Self { | ||
|
|
@@ -3308,6 +3311,7 @@ impl Client { | |
| self.inner.search_index.clone(), | ||
| self.inner.thread_subscription_catchup.clone(), | ||
| (*self.inner.media_fetcher.read().await).clone(), | ||
| self.inner.caches.discovery_cache_timeout, | ||
| ) | ||
| .await, | ||
| }; | ||
|
|
@@ -3621,6 +3625,24 @@ impl Client { | |
| pub async fn get_media_fetcher(&self) -> Arc<dyn MediaFetcher> { | ||
| self.inner.media_fetcher.read().await.clone() | ||
| } | ||
|
|
||
| /// Force a refresh of all the cached discovery data (supported versions, | ||
| /// well-known info, homeserver capabilities, and OAuth 2.0 server | ||
| /// metadata), ignoring whether the existing cache is still fresh. | ||
| /// | ||
| /// If refreshing the well-known info fails, or if the OAuth 2.0 server | ||
| /// metadata request fails for any reason, it is silently ignored. | ||
| /// refresh failures are returned as an error, and any remaining | ||
| /// refreshes that haven't run yet are skipped. | ||
|
Comment on lines
+3633
to
+3636
|
||
| pub async fn rediscover(&self) -> Result<(), Error> { | ||
| self.refresh_supported_versions_cache(false).await?; | ||
| self.refresh_well_known_cache().await; | ||
| self.homeserver_capabilities().refresh().await?; | ||
| match self.oauth().server_metadata().await { | ||
| Ok(_) | Err(_) => {} | ||
| } | ||
|
Comment on lines
+3641
to
+3643
|
||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| /// Contains the disk size of the different stores, if known. It won't be | ||
|
|
@@ -3716,7 +3738,11 @@ pub(crate) mod tests { | |
| RoomId, ServerName, UserId, | ||
| api::{ | ||
| FeatureFlag, MatrixVersion, | ||
| client::{room::create_room::v3::Request as CreateRoomRequest, rtc::RtcTransport}, | ||
| client::{ | ||
| discovery::get_capabilities::v3::Capabilities, | ||
| room::create_room::v3::Request as CreateRoomRequest, | ||
| rtc::RtcTransport, | ||
| }, | ||
| }, | ||
| assign, | ||
| events::{ | ||
|
|
@@ -3940,6 +3966,27 @@ pub(crate) mod tests { | |
| client.server_versions().await.unwrap(); | ||
| } | ||
|
|
||
| #[async_test] | ||
| async fn test_rediscover() { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You need a lot more tests. This test does not check that Please check out https://github.com/matrix-org/matrix-rust-sdk/blob/main/CONTRIBUTING.md#ai-policy . I am not convinced that a human has read and understood this code, which would be a violation of our contribution policy. |
||
| let server = MatrixMockServer::new().await; | ||
|
|
||
| // Mock the two required endpoints | ||
| server.mock_versions().ok().mock_once().named("versions").mount().await; | ||
| server | ||
| .mock_get_homeserver_capabilities() | ||
| .ok_with_capabilities(Capabilities::default()) | ||
| .mock_once() | ||
| .named("capabilities") | ||
| .mount() | ||
| .await; | ||
|
|
||
| // Build the client | ||
| let client = server.client_builder().build().await; | ||
|
|
||
| // Call rediscover and assert it succeeded | ||
| client.rediscover().await.unwrap(); | ||
| } | ||
|
|
||
| #[async_test] | ||
| async fn test_discovery_broken_server() { | ||
| let server = MatrixMockServer::new().await; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The formatting here is not right. I think you want backticks where you currently have backslashes.