Skip to content
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

Implement initial lazy caching credentials provider #578

Merged
merged 12 commits into from
Jul 20, 2021
Merged

Implement initial lazy caching credentials provider #578

merged 12 commits into from
Jul 20, 2021

Conversation

jdisanti
Copy link
Collaborator

@jdisanti jdisanti commented Jul 3, 2021

This implements an initial lazy caching credentials provider, but doesn't attempt to add async runtime-agnostic timeouts or panic handling yet. Both of those will take a considerable amount of work still.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Copy link
Collaborator

@rcoh rcoh left a comment

Choose a reason for hiding this comment

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

My feeling here is we're adding a lot of layers but the layers don't necessarily abstract complexity in an ideal way. I think the refreshing Cache is a layer that makes sense (and I'd pull it into it's own module with its own tests). But I'm not sure we need more than one layer for the actual credentials provider implementation

aws/rust-runtime/aws-auth/src/provider/lazy_caching.rs Outdated Show resolved Hide resolved
aws/rust-runtime/aws-auth/src/provider/lazy_caching.rs Outdated Show resolved Hide resolved
aws/rust-runtime/aws-auth/src/provider/lazy_caching.rs Outdated Show resolved Hide resolved
aws/rust-runtime/aws-auth/src/provider/lazy_caching.rs Outdated Show resolved Hide resolved
aws/rust-runtime/aws-auth/src/provider/lazy_caching.rs Outdated Show resolved Hide resolved
aws/rust-runtime/aws-auth/src/provider/lazy_caching.rs Outdated Show resolved Hide resolved
aws/rust-runtime/aws-auth/src/provider/lazy_caching.rs Outdated Show resolved Hide resolved
aws/rust-runtime/aws-auth/src/provider/lazy_caching.rs Outdated Show resolved Hide resolved
@jdisanti jdisanti requested a review from rcoh July 15, 2021 21:30
Copy link
Collaborator

@rcoh rcoh left a comment

Choose a reason for hiding this comment

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

LGTM, happy to merge and keep iterating


#[derive(Clone)]
pub(super) struct Cache {
margin: Duration,
Copy link
Collaborator

Choose a reason for hiding this comment

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

can you add a doc comment explaining what this field is?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think building time based expiry into this cache explicitly might not be exactly what we want, but fine to keep it this way for now

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added comment and renamed to buffer_time.

Hard to say right now, but I think alternate caching credentials provider implementations will use the cache the same way. Seems like a safe bet to defer refactoring until then.

self.value.read().await.get().cloned()
}

pub async fn refresh<F, Fut>(&self, f: F) -> CredentialsResult
Copy link
Collaborator

Choose a reason for hiding this comment

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

probably worth a doc comment explaining that the caller should ensure f is always the same and that there is no guarantee that this f will be called.

Also, I'm not sure about the name refresh here—it implies to me that it would reload load them. What about get_or_load? or get_or_fetch?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added doc comment and renamed to get_or_load.

}

/// If the credentials are expired, clears the cache. Otherwise, yields the current credentials value.
pub async fn yield_or_clear_if_expired(&self, now: SystemTime) -> Option<Credentials> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we'll probably need a way to also explicitly expire credentials.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

What is the use-case for this?

Copy link
Collaborator

Choose a reason for hiding this comment

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

credentials can become invalidated (eg. manually invalidated session credentials)


const DEFAULT_REFRESH_TIMEOUT: Duration = Duration::from_secs(5);
const DEFAULT_CREDENTIAL_EXPIRATION: Duration = Duration::from_secs(15 * 60);
const DEFAULT_EXPIRATION_MARGIN: Duration = Duration::from_secs(10);
Copy link
Collaborator

Choose a reason for hiding this comment

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

what about DEFAULT_CREDENTIAL_TTL? I think TTL is a little clearer than MARGIN here

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Time-to-live is kind of the opposite of what this is representing. I had chosen "margin" as in "safety margin". Chose to rename to buffer_time, since maybe that is slightly clearer, but I can't think of a good name for it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

oh I totally misunderstood the purpose of this constant 🤕

@jdisanti jdisanti merged commit 11029ed into smithy-lang:main Jul 20, 2021
@jdisanti jdisanti deleted the lazy-caching-creds-provider branch July 22, 2021 16:50
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.

2 participants