Configuration replacement not working as expected #481
-
Hi, I have a problem with configuration replacement feature. Maybe I'm missing something or I made some wrong assumptions. But from the beginning: language: Koltin Below is my code. lettuceBasedProxyManager: @Bean("lettuceProxyManager")
fun lettuceBasedProxyManager(redisClient: RedisClient): LettuceBasedProxyManager<String> {
val redisConnection = redisClient.connect(RedisCodec.of(UTF8, INSTANCE))
return LettuceBasedProxyManager.builderFor(redisConnection)
.withClientSideConfig(
ClientSideConfig.getDefault()
.withExpirationAfterWriteStrategy(
ExpirationAfterWriteStrategy
.basedOnTimeForRefillingBucketUpToMax(10.seconds.toJavaDuration()),
),
)
.build()
} My Component class: @Component
internal class RateLimitExampleOperations(
private val rateStorage: RateStorage,
@Qualifier("lettuceProxyManager")
private val proxyManager: LettuceBasedProxyManager<String>,
) {
private val rateLimitBuckets = ConcurrentHashMap<String, Bucket>()
fun consumeToken(name: String): ConsumptionProbe {
return touchRateLimitBucket(name)
.tryConsumeAndReturnRemaining(1)
}
fun reloadRateLimit(name: String) {
val rateDto = rateStorage.findByNameOrThrow(name)
touchRateLimitBucket(name).replaceConfiguration(
createConfiguration(rateDto).get(),
PROPORTIONALLY,
)
}
private fun touchRateLimitBucket(name: String): Bucket {
return rateLimitBuckets.getOrPut(name) {
val rateDto = rateStorage.findByNameOrThrow(name)
createRateLimitBucket(rateDto)
}
}
private fun createRateLimitBucket(rateDto: RateDto): Bucket {
return proxyManager.builder()
.build(
rateDto.name,
createConfiguration(rateDto),
)
}
private fun createConfiguration(rateDto: RateDto): Supplier<BucketConfiguration> {
return Supplier<BucketConfiguration> {
createBucketConfiguration(
tokens = rateDto.limit.toLong(),
refillTokens = 100,
)
}
}
private fun createBucketConfiguration(
tokens: Long,
refillTokens: Long,
): BucketConfiguration {
return BucketConfiguration.builder()
.addLimit { limit ->
limit
.capacity(tokens)
.refillGreedy(
refillTokens,
60.seconds.toJavaDuration(),
)
.initialTokens(tokens)
}
.build()
}
} The I was trying also https://bucket4j.com/8.10.1/toc.html#implicit-configuration-replacement approach but without success. I thought that when bucket is persisted (saved in Redis) and I execute _ replaceConfiguration_ on it that will update the base configuration of bucket. logs:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Hello @mapoallegro Explanation
The explanation is simple:
How to fix?First of all, you use deprecated method, |
Beta Was this translation helpful? Give feedback.
Hello @mapoallegro
Explanation
The explanation is simple:
How to fix?
First of all, you use deprecated met…