Skip to content

Commit

Permalink
Related to bug no #471 as AbstractCacheRateLimiter is Impacting perfo…
Browse files Browse the repository at this point in the history
…rmance "in case of redis" because of synchronized keyword , as Redis is single threaded and can maintain synchronization on his own , no need for extra synchronization (#472)

Fixes #

suggested fix to introduce new abstract class "AbstractNonBlockCacheRateLimiter" to handle RedisRateLimiter

Co-authored-by: Mohamed Fawzy <[email protected]>
  • Loading branch information
Mohamedfawzy1993 and Mohamed Fawzy authored Oct 20, 2021
1 parent 5cb01bc commit 4517f2b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.config.repository;

import com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.config.Rate;
import com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.config.RateLimiter;
import com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.config.properties.RateLimitProperties;

import java.time.Duration;

/**
* @author mohamed fawzy
*/
public abstract class AbstractNonBlockCacheRateLimiter implements RateLimiter {

@Override
public Rate consume(RateLimitProperties.Policy policy, String key, Long requestTime) {
final Duration refreshInterval = policy.getRefreshInterval();
final Long quota = policy.getQuota() != null ? policy.getQuota().toMillis() : null;
final Rate rate = new Rate(key, policy.getLimit(), quota, null, null);

calcRemainingLimit(policy.getLimit(), refreshInterval, requestTime, key, rate);
calcRemainingQuota(quota, refreshInterval, requestTime, key, rate);

return rate;
}

protected abstract void calcRemainingLimit(Long limit, Duration refreshInterval, Long requestTime, String key, Rate rate);

protected abstract void calcRemainingQuota(Long quota, Duration refreshInterval, Long requestTime, String key, Rate rate);
}

Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
* @author Marcos Barbero
* @author Liel Chayoun
*/
public class RedisRateLimiter extends AbstractCacheRateLimiter {
public class RedisRateLimiter extends AbstractNonBlockCacheRateLimiter {

private final RateLimiterErrorHandler rateLimiterErrorHandler;
private final StringRedisTemplate redisTemplate;
Expand Down

0 comments on commit 4517f2b

Please sign in to comment.