Skip to content

Commit 8460072

Browse files
authored
misc: Add cached() convenience function (#1006)
1 parent 4edcce7 commit 8460072

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"id": "cec7bdeb-990d-4e13-895e-f8ab8762a304",
3+
"type": "feature",
4+
"description": "Add a convenience function for creating `CachedCredentialsProvider`"
5+
}

runtime/auth/aws-credentials/api/aws-credentials.api

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ public final class aws/smithy/kotlin/runtime/auth/awscredentials/CachedCredentia
77

88
public final class aws/smithy/kotlin/runtime/auth/awscredentials/CachedCredentialsProviderKt {
99
public static final field DEFAULT_CREDENTIALS_REFRESH_SECONDS I
10+
public static final fun cached-vLdBGDU (Laws/smithy/kotlin/runtime/auth/awscredentials/CredentialsProvider;JJLaws/smithy/kotlin/runtime/time/Clock;)Laws/smithy/kotlin/runtime/auth/awscredentials/CachedCredentialsProvider;
11+
public static synthetic fun cached-vLdBGDU$default (Laws/smithy/kotlin/runtime/auth/awscredentials/CredentialsProvider;JJLaws/smithy/kotlin/runtime/time/Clock;ILjava/lang/Object;)Laws/smithy/kotlin/runtime/auth/awscredentials/CachedCredentialsProvider;
1012
}
1113

1214
public abstract interface class aws/smithy/kotlin/runtime/auth/awscredentials/CloseableCredentialsProvider : aws/smithy/kotlin/runtime/auth/awscredentials/CredentialsProvider, java/io/Closeable {

runtime/auth/aws-credentials/common/src/aws/smithy/kotlin/runtime/auth/awscredentials/CachedCredentialsProvider.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,21 @@ public class CachedCredentialsProvider(
7474
source.closeIfCloseable()
7575
}
7676
}
77+
78+
/**
79+
* A utility function which wraps a [CredentialsProvider] in a [CachedCredentialsProvider].
80+
*
81+
* @param expireCredentialsAfter the default expiration time period for sourced credentials. For a given set of
82+
* cached credentials, the refresh time period will be the minimum of this time and any expiration timestamp on
83+
* the credentials themselves.
84+
* @param refreshBufferWindow amount of time before the actual credential expiration time when credentials are
85+
* considered expired. For example, if credentials are expiring in 15 minutes, and the buffer time is 10 seconds,
86+
* then any requests made after 14 minutes and 50 seconds will load new credentials. Defaults to 10 seconds.
87+
* @param clock the source of time for this provider
88+
* @return the newly-constructed credentials provider
89+
*/
90+
public fun CredentialsProvider.cached(
91+
expireCredentialsAfter: Duration = DEFAULT_CREDENTIALS_REFRESH_SECONDS.seconds,
92+
refreshBufferWindow: Duration = DEFAULT_CREDENTIALS_REFRESH_BUFFER_SECONDS.seconds,
93+
clock: Clock = Clock.System,
94+
): CachedCredentialsProvider = CachedCredentialsProvider(this, expireCredentialsAfter, refreshBufferWindow, clock)

runtime/auth/aws-credentials/common/test/aws/smithy/kotlin/runtime/auth/awscredentials/CachedCredentialsProviderTest.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,18 @@ class CachedCredentialsProviderTest {
136136
}
137137
assertEquals(1, source.callCount)
138138
}
139+
140+
@Test
141+
fun testCachedConvenienceFunction() = runTest {
142+
val source = TestCredentialsProvider(expiration = testExpiration)
143+
val provider = source.cached(clock = testClock)
144+
145+
val creds = provider.resolve()
146+
val expected = Credentials("AKID", "secret", expiration = testExpiration)
147+
assertEquals(expected, creds)
148+
assertEquals(1, source.callCount)
149+
150+
provider.resolve()
151+
assertEquals(1, source.callCount)
152+
}
139153
}

0 commit comments

Comments
 (0)