Skip to content

Commit

Permalink
Update vanilla build
Browse files Browse the repository at this point in the history
  • Loading branch information
wasdennnoch committed Dec 4, 2023
1 parent 45e3ced commit 35f314b
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 120 deletions.
58 changes: 0 additions & 58 deletions vanilla/build.gradle

This file was deleted.

55 changes: 55 additions & 0 deletions vanilla/build.gradle.kts
@@ -0,0 +1,55 @@
plugins {
application
java
kotlin("jvm") version "1.9.20"
}

group = "gg.beemo.vanilla"
version = "1.0.0"

dependencies {
// Kotlin
val kotlinCoroutinesVersion = "1.7.3"
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlinCoroutinesVersion")

// Beemo shared code
implementation("gg.beemo.latte:latte")

// Logging
val log4jVersion = "2.22.0"
implementation("org.apache.logging.log4j:log4j-api:$log4jVersion")
implementation("org.apache.logging.log4j:log4j-core:$log4jVersion")
implementation("org.apache.logging.log4j:log4j-slf4j2-impl:$log4jVersion")
}

repositories {
mavenCentral()
}

kotlin {
jvmToolchain(17)
}

application {
mainClass.set("gg.beemo.vanilla.Vanilla")
applicationDefaultJvmArgs = listOf(
"-XX:+AlwaysPreTouch",
"-XX:+PerfDisableSharedMem",
"-XX:+UseG1GC",
"-XX:-OmitStackTraceInFastThrow",
"-XX:MaxRAMPercentage=80",
"-XX:MinRAMPercentage=80",
)
}

// Like `installDist`, but with a stable main jar file name for local development
tasks.register("installDev") {
tasks.jar.get().apply {
archiveFileName.set("vanilla.jar")
manifest {
attributes["Main-Class"] = application.mainClass.get()
attributes["Class-Path"] = configurations.runtimeClasspath.get().files.joinToString(" ") { it.name }
}
}
dependsOn("installDist")
}
3 changes: 0 additions & 3 deletions vanilla/settings.gradle

This file was deleted.

3 changes: 3 additions & 0 deletions vanilla/settings.gradle.kts
@@ -0,0 +1,3 @@
includeBuild("../latte")

rootProject.name = "vanilla"
41 changes: 22 additions & 19 deletions vanilla/src/main/java/gg/beemo/vanilla/KafkaRatelimitClient.kt
Expand Up @@ -3,7 +3,7 @@ package gg.beemo.vanilla
import gg.beemo.latte.broker.BrokerClient
import gg.beemo.latte.broker.BrokerConnection
import gg.beemo.latte.broker.BrokerMessage
import gg.beemo.latte.logging.log
import gg.beemo.latte.logging.Log
import gg.beemo.latte.ratelimit.SharedRatelimitData
import gg.beemo.latte.ratelimit.SharedRatelimitData.RatelimitClientData
import gg.beemo.latte.util.SuspendingRatelimit
Expand All @@ -16,39 +16,42 @@ private val EXPIRY_GRACE_PERIOD = 5.seconds.inWholeMilliseconds

class RatelimitClient(connection: BrokerConnection) : BrokerClient(connection) {

private val log by Log
private val globalRatelimitProvider = RatelimitProvider(50, 1.seconds)
private val identifyRatelimitProvider = RatelimitProvider(1, 5.seconds)

private val globalRatelimitRpc = rpc<RatelimitClientData, Unit>(
SharedRatelimitData.RATELIMIT_TOPIC,
SharedRatelimitData.KEY_REQUEST_GLOBAL_QUOTA,
) {
handleRatelimitRequest(it, globalRatelimitProvider, "global")
}
init {
rpc<RatelimitClientData, Unit>(
SharedRatelimitData.RATELIMIT_TOPIC,
SharedRatelimitData.KEY_REQUEST_GLOBAL_QUOTA,
) {
handleRatelimitRequest(it, globalRatelimitProvider, "global")
}

private val identifyRatelimitRpc = rpc<RatelimitClientData, Unit>(
SharedRatelimitData.RATELIMIT_TOPIC,
SharedRatelimitData.KEY_REQUEST_IDENTIFY_QUOTA,
) {
handleRatelimitRequest(it, identifyRatelimitProvider, "identify")
rpc<RatelimitClientData, Unit>(
SharedRatelimitData.RATELIMIT_TOPIC,
SharedRatelimitData.KEY_REQUEST_IDENTIFY_QUOTA,
) {
handleRatelimitRequest(it, identifyRatelimitProvider, "identify")
}
}

private suspend fun handleRatelimitRequest(
msg: BrokerMessage<RatelimitClientData>,
ratelimitProvider: RatelimitProvider,
type: String,
) {
val sourceCluster = "${msg.headers.sourceService}/${msg.headers.sourceInstance}"
val client = msg.value.discordClientId ?: "default"
val clientId = msg.value.discordClientId ?: "default"
val service = "${msg.headers.sourceService}/${msg.headers.sourceInstance} (${clientId})"
val expiresAt = msg.value.requestExpiresAt
if (expiresAt != null && (expiresAt + EXPIRY_GRACE_PERIOD) < System.currentTimeMillis()) {
log.info("Incoming expired '$type' quota request from client '$client' in cluster $sourceCluster, ignoring")
log.info("Incoming expired $type quota request from service $service, ignoring")
// If the request has already expired, ignore it to not eat quotas unnecessarily
return
}
log.debug("Incoming '{}' quota request from client '{}' in cluster {}", type, client, sourceCluster)
ratelimitProvider.getClientRatelimit(client).requestQuota()
log.debug("Granted '{}' quota request for client '{}' in cluster {}", type, client, sourceCluster)
log.debug("Incoming {} quota request from service {}", type, service)
ratelimitProvider.getClientRatelimit(clientId).requestQuota()
log.debug("Granted {} quota request for service {}", type, service)
}

}
Expand All @@ -57,7 +60,7 @@ private class RatelimitProvider(private val burst: Int, private val duration: Du

private val limiters = ConcurrentHashMap<String, SuspendingRatelimit>()

fun getClientRatelimit(client: String): SuspendingRatelimit = limiters.computeIfAbsent(client) {
fun getClientRatelimit(clientId: String): SuspendingRatelimit = limiters.computeIfAbsent(clientId) {
SuspendingRatelimit(burst, duration)
}

Expand Down
40 changes: 0 additions & 40 deletions vanilla/src/main/java/gg/beemo/vanilla/Vanilla.java

This file was deleted.

35 changes: 35 additions & 0 deletions vanilla/src/main/java/gg/beemo/vanilla/Vanilla.kt
@@ -0,0 +1,35 @@
package gg.beemo.vanilla

import gg.beemo.latte.CommonConfig
import gg.beemo.latte.broker.kafka.KafkaConnection
import gg.beemo.latte.config.Configurator
import gg.beemo.latte.logging.Log
import kotlinx.coroutines.runBlocking

object Vanilla {

private val log by Log

@JvmStatic
fun main(args: Array<String>) = runBlocking {
log.debug("Loading configuration")
Configurator.create().mirror(Config::class.java)

log.debug("Initializing Kafka connection")
val brokerConnection = KafkaConnection(
Config.KAFKA_HOST,
CommonConfig.BrokerServices.VANILLA,
"0",
Config.KAFKA_USE_TLS,
)

log.debug("Initializing Kafka Ratelimit client")
RatelimitClient(brokerConnection)

log.debug("Starting Kafka connection")
brokerConnection.start()

log.info("Initialization done! Listening for ratelimit requests.")
}

}

0 comments on commit 35f314b

Please sign in to comment.