Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ internal data class ExpiringValue<T> (val value: T, val expiresAt: Instant)
@OptIn(ExperimentalTime::class)
internal class CachedValue<T> (
private var value: ExpiringValue<T>? = null,
private val bufferTime: Duration = Duration.seconds(0),
private val bufferTime: Duration = Duration.ZERO,
private val clock: Clock = Clock.System
) {
constructor(value: T, expiresAt: Instant, bufferTime: Duration = Duration.seconds(0), clock: Clock = Clock.System) : this(ExpiringValue(value, expiresAt), bufferTime, clock)
constructor(value: T, expiresAt: Instant, bufferTime: Duration = Duration.ZERO, clock: Clock = Clock.System) : this(ExpiringValue(value, expiresAt), bufferTime, clock)
private val mu = Mutex()

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import aws.smithy.kotlin.runtime.time.Clock
import aws.smithy.kotlin.runtime.util.Platform
import aws.smithy.kotlin.runtime.util.PlatformProvider
import kotlin.time.Duration
import kotlin.time.Duration.Companion.seconds
import kotlin.time.ExperimentalTime

/**
Expand Down Expand Up @@ -70,8 +71,8 @@ public class ImdsClient private constructor(builder: Builder) : InstanceMetadata
init {
require(maxRetries > 0) { "maxRetries must be greater than zero" }
val engine = builder.engine ?: CrtHttpEngine {
connectTimeout = Duration.seconds(1)
socketReadTimeout = Duration.seconds(1)
connectTimeout = 1.seconds
socketReadTimeout = 1.seconds
}

httpClient = sdkHttpClient(engine)
Expand Down Expand Up @@ -161,7 +162,7 @@ public class ImdsClient private constructor(builder: Builder) : InstanceMetadata
/**
* Override the time-to-live for the session token
*/
public var tokenTtl: Duration = Duration.seconds(DEFAULT_TOKEN_TTL_SECONDS)
public var tokenTtl: Duration = DEFAULT_TOKEN_TTL_SECONDS.seconds

/**
* Configure the [SdkLogMode] used by the client
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import aws.smithy.kotlin.runtime.http.request.url
import aws.smithy.kotlin.runtime.http.response.complete
import aws.smithy.kotlin.runtime.time.Clock
import kotlin.time.Duration
import kotlin.time.Duration.Companion.seconds
import kotlin.time.ExperimentalTime

/**
Expand All @@ -32,10 +33,10 @@ internal const val X_AWS_EC2_METADATA_TOKEN = "x-aws-ec2-metadata-token"
@OptIn(ExperimentalTime::class)
internal class TokenMiddleware(
private val httpClient: SdkHttpClient,
private val ttl: Duration = Duration.seconds(DEFAULT_TOKEN_TTL_SECONDS),
private val ttl: Duration = DEFAULT_TOKEN_TTL_SECONDS.seconds,
private val clock: Clock = Clock.System
) : ModifyRequestMiddleware {
private var cachedToken = CachedValue<Token>(null, bufferTime = Duration.seconds(TOKEN_REFRESH_BUFFER_SECONDS), clock = clock)
private var cachedToken = CachedValue<Token>(null, bufferTime = TOKEN_REFRESH_BUFFER_SECONDS.seconds, clock = clock)

override fun install(op: SdkHttpOperation<*, *>) {
op.execution.finalize.register(this)
Expand Down Expand Up @@ -71,7 +72,7 @@ internal class TokenMiddleware(
HttpStatusCode.OK -> {
val ttl = call.response.headers[X_AWS_EC2_METADATA_TOKEN_TTL_SECONDS]?.toLong() ?: throw EC2MetadataError(200, "No TTL provided in IMDS response")
val token = call.response.body.readAll() ?: throw EC2MetadataError(200, "No token provided in IMDS response")
val expires = clock.now() + Duration.seconds(ttl)
val expires = clock.now() + ttl.seconds
Token(token, expires)
}
else -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.yield
import kotlin.test.*
import kotlin.time.Duration
import kotlin.time.Duration.Companion.seconds
import kotlin.time.ExperimentalTime

@OptIn(ExperimentalTime::class)
Expand All @@ -31,62 +31,62 @@ class CachedValueTest {
@Test
fun testExpiration() = runSuspendTest {
val epoch = Instant.fromEpochSeconds(0)
val expiresAt = epoch + Duration.seconds(10)
val expiresAt = epoch + 10.seconds
val clock = ManualClock(epoch)

val value = CachedValue("foo", expiresAt, clock = clock)

assertFalse(value.isExpired())
assertEquals("foo", value.get())

clock.advance(Duration.seconds(10))
clock.advance(10.seconds)
assertTrue(value.isExpired())
assertNull(value.get())
}

@Test
fun testExpirationBuffer() = runSuspendTest {
val epoch = Instant.fromEpochSeconds(0)
val expiresAt = epoch + Duration.seconds(100)
val expiresAt = epoch + 100.seconds
val clock = ManualClock(epoch)

val value = CachedValue("foo", expiresAt, bufferTime = Duration.seconds(30), clock = clock)
val value = CachedValue("foo", expiresAt, bufferTime = 30.seconds, clock = clock)

assertFalse(value.isExpired())
assertEquals("foo", value.get())

clock.advance(Duration.seconds(70))
clock.advance(70.seconds)
assertTrue(value.isExpired())
assertNull(value.get())
}

@Test
fun testGetOrLoad() = runSuspendTest {
val epoch = Instant.fromEpochSeconds(0)
val expiresAt = epoch + Duration.seconds(100)
val expiresAt = epoch + 100.seconds
val clock = ManualClock(epoch)

val value = CachedValue("foo", expiresAt, bufferTime = Duration.seconds(30), clock = clock)
val value = CachedValue("foo", expiresAt, bufferTime = 30.seconds, clock = clock)

var count = 0
val mu = Mutex()
val initializer = suspend {
mu.withLock { count++ }
ExpiringValue("bar", expiresAt + Duration.seconds(count * 100))
ExpiringValue("bar", expiresAt + count.seconds * 100)
}

assertFalse(value.isExpired())
assertEquals("foo", value.getOrLoad(initializer))
assertEquals(0, count)

// t = 90
clock.advance(Duration.seconds(90))
clock.advance(90.seconds)
assertEquals("bar", value.getOrLoad(initializer))
assertFalse(value.isExpired())
assertEquals(1, count)

// t = 180
clock.advance(Duration.seconds(90))
clock.advance(90.seconds)
repeat(10) {
async {
value.getOrLoad(initializer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import io.kotest.matchers.string.shouldContain
import kotlinx.coroutines.withTimeout
import kotlinx.serialization.json.*
import kotlin.test.*
import kotlin.time.Duration
import kotlin.time.Duration.Companion.seconds
import kotlin.time.ExperimentalTime

@OptIn(ExperimentalTime::class)
Expand Down Expand Up @@ -80,12 +80,12 @@ class ImdsClientTest {
engine = connection
endpointConfiguration = EndpointConfiguration.ModeOverride(EndpointMode.IPv6)
clock = testClock
tokenTtl = Duration.seconds(600)
tokenTtl = 600.seconds
}

val r1 = client.get("/latest/metadata")
assertEquals("output 1", r1)
testClock.advance(Duration.seconds(600))
testClock.advance(600.seconds)

val r2 = client.get("/latest/metadata")
assertEquals("output 2", r2)
Expand Down Expand Up @@ -127,17 +127,17 @@ class ImdsClientTest {
engine = connection
endpointConfiguration = EndpointConfiguration.ModeOverride(EndpointMode.IPv6)
clock = testClock
tokenTtl = Duration.seconds(600)
tokenTtl = 600.seconds
}

val r1 = client.get("/latest/metadata")
assertEquals("output 1", r1)
testClock.advance(Duration.seconds(400))
testClock.advance(400.seconds)

val r2 = client.get("/latest/metadata")
assertEquals("output 2", r2)

testClock.advance(Duration.seconds(150))
testClock.advance(150.seconds)
val r3 = client.get("/latest/metadata")
assertEquals("output 3", r3)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import kotlin.io.path.exists
import kotlin.io.path.name
import kotlin.io.path.readText
import kotlin.test.*
import kotlin.time.Duration
import kotlin.time.Duration.Companion.seconds
import kotlin.time.ExperimentalTime

private const val DEFAULT_SIGNING_ISO_DATE = "2015-08-30T12:36:00Z"
Expand Down Expand Up @@ -205,7 +205,7 @@ class Sigv4TestSuite {
config.service = json["service"]!!.jsonPrimitive.content

json["expiration_in_seconds"]?.jsonPrimitive?.int?.let {
config.expiresAfter = Duration.seconds(it)
config.expiresAfter = it.seconds
}

json["normalize"]?.jsonPrimitive?.boolean?.let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import kotlinx.coroutines.async
import kotlinx.coroutines.withTimeout
import kotlinx.coroutines.yield
import kotlin.test.Test
import kotlin.time.Duration
import kotlin.time.Duration.Companion.seconds
import kotlin.time.ExperimentalTime

class AsyncStressTest : TestWithLocalServer() {
Expand Down Expand Up @@ -90,7 +90,7 @@ class AsyncStressTest : TestWithLocalServer() {
}
}

withTimeout(Duration.seconds(5)) {
withTimeout(5.seconds) {
repeat(1_000) {
async {
try {
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import java.util.Properties
import java.net.URL

plugins {
kotlin("jvm") version "1.5.31" apply false
kotlin("jvm") version "1.6.10" apply false
id("org.jetbrains.dokka")
id("io.github.gradle-nexus.publish-plugin") version "1.1.0"
}
Expand Down
2 changes: 1 addition & 1 deletion examples/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
kotlin("jvm") version "1.5.31"
kotlin("jvm") version "1.6.10"
}

val awsSdkKotlinVersion: String by project
Expand Down
16 changes: 8 additions & 8 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@ sdkVersion=0.9.6-SNAPSHOT
smithyVersion=1.13.1
smithyGradleVersion=0.5.3
# smithy-kotlin codegen and runtime are versioned together
smithyKotlinVersion=0.7.4-beta
smithyKotlinVersion=0.7.5-SNAPSHOT
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like I missed this in my version bump PR


# kotlin
kotlinVersion=1.5.31
dokkaVersion=1.5.31
kotlinVersion=1.6.10
dokkaVersion=1.6.0

# kotlin JVM
kotlinJVMTargetVersion=1.8

# kotlin libraries
coroutinesVersion=1.5.1
atomicFuVersion=0.16.1
kotlinxSerializationVersion=1.3.0
ktorVersion=1.6.3
coroutinesVersion=1.5.2
atomicFuVersion=0.17.0
kotlinxSerializationVersion=1.3.1
ktorVersion=1.6.7

# crt
crtKotlinVersion=0.5.0-alpha
crtKotlinVersion=0.5.1-SNAPSHOT

# testing/utility
junitVersion=5.6.2
Expand Down