Skip to content

feat: add discovery_cache_timeout to ClientBuilder and rediscover() t…#6712

Open
SIDHARTH20K4 wants to merge 6 commits into
matrix-org:mainfrom
SIDHARTH20K4:feat/discovery-cache-timeout
Open

feat: add discovery_cache_timeout to ClientBuilder and rediscover() t…#6712
SIDHARTH20K4 wants to merge 6 commits into
matrix-org:mainfrom
SIDHARTH20K4:feat/discovery-cache-timeout

Conversation

@SIDHARTH20K4

@SIDHARTH20K4 SIDHARTH20K4 commented Jul 2, 2026

Copy link
Copy Markdown

The SDK makes fresh network requests on every startup to discover homeserver versions, well-known info, and other discovery data, even though this data rarely changes. This adds unnecessary latency and load on the homeserver.

This PR adds:

  • ClientBuilder::discovery_cache_timeout(Duration) to configure how long discovery data is considered fresh (default: 24h)
  • Client::rediscover() to force a refresh of all cached discovery data

Closes #1469

  • I've documented the public API changes in the appropriate changelog files (see Writing changelog entries).
  • This PR was made with the help of AI.

Signed-off-by: Sidharth sidharth.120504@gmail.com

…o Client

Signed-off-by: SIDHARTH20K4 <91970588+SIDHARTH20K4@users.noreply.github.com>
Signed-off-by: SIDHARTH20K4 <91970588+SIDHARTH20K4@users.noreply.github.com>
Signed-off-by: SIDHARTH20K4 <91970588+SIDHARTH20K4@users.noreply.github.com>
@codspeed-hq

codspeed-hq Bot commented Jul 9, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 48.67%

⚡ 1 improved benchmark
✅ 49 untouched benchmarks

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Simulation Restore session [memory store] 168.4 ms 113.2 ms +48.67%

Tip

Curious why this is faster? Comment @codspeedbot explain why this is faster on this PR, or directly use the CodSpeed MCP with your agent.


Comparing SIDHARTH20K4:feat/discovery-cache-timeout (c6a5011) with main (8980b4b)

Open in CodSpeed

Signed-off-by: SIDHARTH20K4 <91970588+SIDHARTH20K4@users.noreply.github.com>
@codecov

codecov Bot commented Jul 9, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 79.31034% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.90%. Comparing base (8980b4b) to head (c6a5011).
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
crates/matrix-sdk/src/client/builder/mod.rs 20.00% 4 Missing ⚠️
crates/matrix-sdk/src/client/mod.rs 91.66% 0 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6712      +/-   ##
==========================================
- Coverage   89.90%   89.90%   -0.01%     
==========================================
  Files         398      398              
  Lines      111296   111325      +29     
  Branches   111296   111325      +29     
==========================================
+ Hits       100062   100083      +21     
+ Misses       7435     7433       -2     
- Partials     3799     3809      +10     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

Signed-off-by: SIDHARTH20K4 <91970588+SIDHARTH20K4@users.noreply.github.com>
Signed-off-by: SIDHARTH20K4 <91970588+SIDHARTH20K4@users.noreply.github.com>
@SIDHARTH20K4 SIDHARTH20K4 marked this pull request as ready for review July 9, 2026 11:43
@SIDHARTH20K4 SIDHARTH20K4 requested a review from a team as a code owner July 9, 2026 11:43
@SIDHARTH20K4 SIDHARTH20K4 requested review from andybalaam and Copilot and removed request for a team July 9, 2026 11:43

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds API surface to control Matrix discovery-data caching and to force-refresh that discovery state, aiming to reduce startup-time network requests and homeserver load.

Changes:

  • Add ClientBuilder::discovery_cache_timeout(Duration) with a default of 24h.
  • Add Client::rediscover() to force-refresh supported versions, well-known, homeserver capabilities, and OAuth server metadata.
  • Add a basic unit test covering rediscover() success with mocked endpoints.

Reviewed changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 3 comments.

File Description
crates/matrix-sdk/src/client/mod.rs Wires discovery_cache_timeout through ClientInner, adds Client::rediscover(), and adds a rediscover test.
crates/matrix-sdk/src/client/caches.rs Extends ClientCaches with a discovery_cache_timeout field.
crates/matrix-sdk/src/client/builder/mod.rs Adds builder field/default and the public discovery_cache_timeout(...) setter.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +690 to +694
/// By default, this is 24 hours
pub fn discovery_cache_timeout(mut self, stale_timeout: Duration) -> Self {
self.discovery_cache_timeout = stale_timeout;
self
}
Comment on lines +3633 to +3636
/// 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 +3641 to +3643
match self.oauth().server_metadata().await {
Ok(_) | Err(_) => {}
}
@SIDHARTH20K4

Copy link
Copy Markdown
Author

Thanks for the Copilot feedback. I noticed that discovery_cache_timeout is stored but not actually wired into the staleness checks yet. TtlValue::has_expired() still uses the hardcoded STALE_THRESHOLD.

I was thinking of adding a has_expired_with_timeout(Duration) method to TtlValue and updating the refresh functions to use self.inner.caches.discovery_cache_timeout when checking staleness. But before going ahead, wanted to check if this fits the existing design or if there's a preferred approach.

Happy to implement once there's some direction!

Copy link
Copy Markdown
Member

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.

}

#[async_test]
async fn test_rediscover() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need a lot more tests. This test does not check that rediscover actually hits the endpoints, or that it uses what is being returned from them. You don't have any tests that check that the timeout is being used, and as far as I can see it's not being used, so as it stands this PR doesn't make any sense at all.

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @andybalaam,

I'm new to Rust and OSS, and I used AI as a learning tool to understand the codebase and get unstuck on syntax issues. I'll be more careful going forward, especially with PR/issue text, to make sure it's my own words.

You're right about the test, it doesn't properly verify that rediscover() actually hits the endpoints or uses their responses. I'll fix that.

For the timeout issue (also flagged by Copilot, and in my earlier comment), it would help to know if you have a preferred approach before I dig in, since it touches TtlValue and a few call sites.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, thanks for getting back to me and good luck in your learning journey! I would strongly suggest learning by writing code yourself, rather than trying to adapt or understand code that has been generated by AI. It's particularly key (in my opinion) not to believe what AI says about the code it's written: it is usually giving incorrect information.

I've read your comment about timeouts above several times and I can't understand it. I think what is confusing me is that the code doesn't actually do what it's supposed to do. I suggest you try to write code that solves the problem you've described and then we can talk more easily about whether it's the best way to do it.

By the way, it would be interesting to know a bit more about why you've chosen to work on this problem: is it solving an issue that you are experiencing?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Homeserver discovery state caching

3 participants