Skip to content

fix: Cache loaded identities#479

Merged
adamspofford-dfinity merged 4 commits intomainfrom
spofford/cache-identity
Apr 1, 2026
Merged

fix: Cache loaded identities#479
adamspofford-dfinity merged 4 commits intomainfrom
spofford/cache-identity

Conversation

@adamspofford-dfinity
Copy link
Copy Markdown
Contributor

No description provided.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds in-process caching to the ICP identity loader so repeated identity loads (within a single CLI execution) reuse an already-loaded Arc<dyn Identity> instead of re-reading identity files / re-prompting for secrets.

Changes:

  • Add an internal Mutex<HashMap<IdentitySelection, Arc<dyn Identity>>> cache to identity::Loader and route construction through a new Loader::new(...) constructor.
  • Update IdentitySelection to derive Eq + Hash to support use as a HashMap key.
  • Update context initialization to construct the loader via Loader::new(...) rather than a struct literal.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
crates/icp/src/identity/mod.rs Introduces cached identity loading, constructor, and hashing support for IdentitySelection.
crates/icp/src/context/init.rs Switches identity loader initialization to the new Loader::new(...) API.

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

Comment on lines 134 to +137
async fn load(&self, id: IdentitySelection) -> Result<Arc<dyn Identity>, LoadError> {
if let Some(cached) = self.cache.lock().unwrap().get(&id) {
return Ok(Arc::clone(cached));
}
Copy link

Copilot AI Mar 31, 2026

Choose a reason for hiding this comment

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

On cache hits this returns early without re-setting telemetry_data.identity_type, which can leave telemetry reflecting a previously loaded (different) identity selection if the loader is reused within the same process. Consider caching the computed IdentityStorageType alongside the Arc<dyn Identity> and calling telemetry_data.set_identity_type(...) even when serving from cache (or otherwise ensuring telemetry is updated per load() call).

Copilot uses AI. Check for mistakes.
@adamspofford-dfinity adamspofford-dfinity merged commit 49bd301 into main Apr 1, 2026
88 checks passed
@adamspofford-dfinity adamspofford-dfinity deleted the spofford/cache-identity branch April 1, 2026 15:03
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.

3 participants