-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds rate limit service, basic configuration for bucket and quotas.
- Loading branch information
paulbrejla
committed
Dec 1, 2023
1 parent
e9822cf
commit 61f5db3
Showing
4 changed files
with
72 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/main/kotlin/de/paulbrejla/holidays/application/api/RateLimitService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package de.paulbrejla.holidays.application.api | ||
|
||
import io.github.bucket4j.Bucket | ||
|
||
interface RateLimitService { | ||
fun resolveBucket(bucketId: String): Bucket | ||
fun fetchBucket(bucketId: String): Bucket? | ||
fun isWithinQuota(maxRequests: Int, currentRequests: Int): Boolean | ||
} |
56 changes: 56 additions & 0 deletions
56
src/main/kotlin/de/paulbrejla/holidays/application/impl/GlobalRateLimitServiceImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package de.paulbrejla.holidays.application.impl | ||
|
||
import de.paulbrejla.holidays.application.api.RateLimitService | ||
import io.github.bucket4j.Bandwidth | ||
import io.github.bucket4j.Bucket | ||
import io.github.bucket4j.Refill | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.stereotype.Component | ||
import java.time.Duration | ||
import java.util.concurrent.ConcurrentHashMap | ||
|
||
@Component("rateLimitService") | ||
class GlobalRateLimitServiceImpl : RateLimitService { | ||
// For now we create one global bucket that all requests consume from. | ||
@Value("\${rateLimit.globalBucket.id}") | ||
var globalBucketId: String = "" | ||
|
||
@Value("\${rateLimit.globalBucket.capacity}") | ||
var globalBucketCapacity: Long = 0 | ||
|
||
|
||
private var buckets: MutableMap<String, Bucket> = ConcurrentHashMap<String, Bucket>() | ||
override fun resolveBucket(bucketId: String): Bucket { | ||
return if (buckets.containsKey(bucketId)) { | ||
buckets[bucketId]!! | ||
} else { | ||
createBucket().let { | ||
buckets[bucketId] = it | ||
it | ||
} | ||
} | ||
} | ||
|
||
override fun fetchBucket(bucketId: String): Bucket? { | ||
return buckets[bucketId] | ||
} | ||
|
||
override fun isWithinQuota(maxRequests: Int, currentRequests: Int): Boolean { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
/** | ||
* We only allow 1000 requests per hour from now on. These 1000 requests are refilled when the new | ||
* hour starts. | ||
*/ | ||
private fun createBucket(): Bucket { | ||
return Bucket.builder() | ||
.addLimit( | ||
Bandwidth.classic( | ||
globalBucketCapacity, | ||
Refill.intervally(globalBucketCapacity, Duration.ofHours(1)) | ||
) | ||
) | ||
.build() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters