Skip to content

Commit

Permalink
Slight logging improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
wasdennnoch committed Nov 28, 2024
1 parent 9198371 commit 7c01903
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
13 changes: 10 additions & 3 deletions vanilla/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@
# your environment variables in quotes unless it is part of it.

#------------
# Kafka Connection
# RabbitMQ Connection
#---------------
KAFKA_HOST=
KAFKA_USE_TLS=
RABBIT_HOST=
RABBIT_USE_TLS=
RABBIT_USERNAME=
RABBIT_PASSWORD=

#------------
# gRPC Server
#---------------
GRPC_PORT=
30 changes: 22 additions & 8 deletions vanilla/src/main/java/gg/beemo/vanilla/GrpcRatelimitService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,41 @@ class GrpcRatelimitService : RatelimitGrpcKt.RatelimitCoroutineImplBase() {
private val identifyRatelimits = ClientRatelimits(1, 5.seconds)

override suspend fun reserveQuota(request: RatelimitRequest): RatelimitQuota {
val (ratelimitMap, typeString) = when (request.type) {
RatelimitType.GLOBAL -> globalRatelimits to "global"
RatelimitType.IDENTIFY -> identifyRatelimits to "identify"
val clientRatelimits = when (request.type) {
RatelimitType.GLOBAL -> globalRatelimits
RatelimitType.IDENTIFY -> identifyRatelimits
else -> throw IllegalArgumentException("Unknown ratelimit type ${request.type}")
}

val ratelimit = ratelimitMap.getClientRatelimit(request.clientId)
val ratelimit = clientRatelimits.getClientRatelimit(request.clientId)
val maxDelay = if (request.hasMaxDelay()) request.maxDelay.toLong() else null
val (granted, at) = ratelimit.reserveQuota(request.probeOnly, maxDelay)
val delay = (at - System.currentTimeMillis()).coerceAtLeast(0)

if (request.probeOnly) {
log.debug("Probed {} quota slot for clientId {} is at {}", typeString, request.clientId, at)
log.debug(
"Probed {} quota slot for clientId {} is at {} (in {} ms)",
request.type,
request.clientId,
at,
delay
)
} else if (granted) {
log.debug("Reserved {} quota slot for clientId {} at {}", typeString, request.clientId, at)
log.debug(
"Reserved {} quota slot for clientId {} at {} (in {} ms)",
request.type,
request.clientId,
at,
delay
)
} else {
val maxTimestamp = if (maxDelay != null) System.currentTimeMillis() + maxDelay else null
log.debug(
"Failed to reserve {} quota slot for clientId {}, next slot would be at {}, requested max delay was {} (-> {})",
typeString,
"Failed to reserve {} quota slot for clientId {}, next slot would be at {} (in {} ms), requested max delay was {} (-> {})",
request.type,
request.clientId,
at,
delay,
maxDelay,
maxTimestamp
)
Expand Down

0 comments on commit 7c01903

Please sign in to comment.