Skip to content

Commit

Permalink
Use AtomicFU compiler plugin (#740)
Browse files Browse the repository at this point in the history
It's supposed to be less fragile since it transforms Kotlin IR instead
of JVM bytecode. E.g. it fixes
Kotlin/kotlinx-atomicfu#186 which allows
simplifying KordConfiguration.

see https://github.com/Kotlin/kotlinx-atomicfu#atomicfu-compiler-plugin
  • Loading branch information
lukellmann committed Jan 24, 2023
1 parent f64c5d2 commit 98ac68f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 28 deletions.
30 changes: 3 additions & 27 deletions common/src/main/kotlin/KordConfiguration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,17 @@ import kotlinx.atomicfu.atomic

@KordExperimental
public object KordConfiguration {
// not able to write it like `public var REST_VERSION: Int by atomic(10)` because of
// https://github.com/Kotlin/kotlinx.atomicfu/issues/186
// TODO use delegation when AtomicFU fixes this

private const val REST_GATEWAY_DEFAULT = 10


private val REST = atomic(REST_GATEWAY_DEFAULT)

/**
* The [version of Discord's REST API](https://discord.com/developers/docs/reference#api-versioning) Kord uses.
*
* Changing this version might lead to errors since Kord is designed to work with the initially set version.
*/
@KordExperimental
@set:KordUnsafe
public var REST_VERSION: Int
get() = REST.value
set(value) {
REST.value = value
}


private val GATEWAY = atomic(REST_GATEWAY_DEFAULT)
public var REST_VERSION: Int by atomic(REST_GATEWAY_DEFAULT)

/**
* The
Expand All @@ -40,14 +27,7 @@ public object KordConfiguration {
*/
@KordExperimental
@set:KordUnsafe
public var GATEWAY_VERSION: Int
get() = GATEWAY.value
set(value) {
GATEWAY.value = value
}


private val VOICE = atomic(4)
public var GATEWAY_VERSION: Int by atomic(REST_GATEWAY_DEFAULT)

/**
* The
Expand All @@ -58,9 +38,5 @@ public object KordConfiguration {
*/
@KordExperimental
@set:KordUnsafe
public var VOICE_GATEWAY_VERSION: Int
get() = VOICE.value
set(value) {
VOICE.value = value
}
public var VOICE_GATEWAY_VERSION: Int by atomic(4)
}
5 changes: 5 additions & 0 deletions gateway/api/gateway.api
Original file line number Diff line number Diff line change
Expand Up @@ -1605,9 +1605,14 @@ public final class dev/kord/gateway/RequestGuildMembers$Companion {

public final class dev/kord/gateway/RequestGuildMembers$Nonce {
public static final field INSTANCE Ldev/kord/gateway/RequestGuildMembers$Nonce;
public static final field counter$dev$VolatileWrapper Ldev/kord/gateway/RequestGuildMembers$Nonce$Counter$dev$VolatileWrapper;
public final fun new ()Ljava/lang/String;
}

public final class dev/kord/gateway/RequestGuildMembers$Nonce$Counter$dev$VolatileWrapper {
public fun <init> ()V
}

public final class dev/kord/gateway/Resume : dev/kord/gateway/Command {
public static final field Companion Ldev/kord/gateway/Resume$Companion;
public synthetic fun <init> (ILjava/lang/String;Ljava/lang/String;ILkotlinx/serialization/internal/SerializationConstructorMarker;)V
Expand Down
4 changes: 4 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ org.gradle.jvmargs=-XX:MaxMetaspaceSize=1024m
org.gradle.parallel=true
kotlin.code.style=official

# https://github.com/Kotlin/kotlinx-atomicfu#atomicfu-compiler-plugin
kotlinx.atomicfu.enableJvmIrTransformation=true
kotlinx.atomicfu.enableJsIrTransformation=true

# remove when upgrading to gradle 8.0
# https://docs.gradle.org/7.6/userguide/upgrading_version_7.html#strict-kotlin-dsl-precompiled-scripts-accessors
systemProp.org.gradle.kotlin.dsl.precompiled.accessors.strict=true
2 changes: 1 addition & 1 deletion rest/src/main/kotlin/ratelimit/AbstractRateLimiter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public abstract class AbstractRateLimiter internal constructor(public val clock:
internal abstract val logger: KLogger

internal val autoBanRateLimiter = IntervalRateLimiter(limit = 25000, interval = 10.minutes)
internal val globalSuspensionPoint = atomic(Reset(clock.now()))
private val globalSuspensionPoint = atomic(Reset(clock.now()))
internal val buckets = ConcurrentHashMap<BucketKey, Bucket>()
internal val routeBuckets = ConcurrentHashMap<RequestIdentifier, MutableSet<BucketKey>>()

Expand Down

0 comments on commit 98ac68f

Please sign in to comment.