Code Monkey home page Code Monkey logo

Comments (4)

jiashuaizhang avatar jiashuaizhang commented on May 15, 2024

搭了个环境验证了,和我的猜想一致,当前版本1.1.1由于hashTag设置有误,集群下是不能正确运行的。我fork改好了,但是改动有点多,改了些不需要改的东西,所以就不merge回来了吧...说下主要修改项吧

  1. 配置redisSerializer
    @Bean
    @ConditionalOnMissingBean
    public StringRedisSerializer stringRedisSerializer() {
        return new StringRedisSerializer();
    }

2.RedisScript泛型设置为String(返回值被序列化为String,用Long接收报错,也可以想其他办法解决,但没必要)

    private RedisScript<String> redisScript;

    public RedisRateLimiterCounterImpl(RedisScript<String> redisScript){
        this.redisScript=redisScript;
    }

3.lua脚本修改,除了key以外其他参数用ARGV接收,lastClearTimeKey设置hashTag

local key = KEYS[1];
local limit = tonumber(ARGV[1]);
local step = tonumber(ARGV[2]);
local interval = tonumber(ARGV[3]);
local nowTime = tonumber(ARGV[4]);

local lastClearTimeKey='syj-rateLimiter-lastClearTime'..'{'..key..'}'
 ......
return '1';

4.传入参数修改,指定redisSerializer,防止redisTemplate被外层覆盖而影响到序列化逻辑

    @Override
    public void consume(String key, long limit, long refreshInterval, long tokenBucketStepNum, long tokenBucketTimeInterval) {
        List<Object> keyList = Arrays.asList(key);
        Object[] argv = {String.valueOf(limit), String.valueOf(tokenBucketStepNum),
                String.valueOf(tokenBucketTimeInterval), String.valueOf(Instant.now().getEpochSecond())};
        String result = (String) redisTemplate.execute(redisScript, stringRedisSerializer, stringRedisSerializer, keyList, argv);
        if(Const.REDIS_ERROR.equals(result)){
            throw new RateLimitException(RateLimitErrorEnum.TOO_MANY_REQUESTS);
        }
    }

from syj-ratelimit.

jiashuaizhang avatar jiashuaizhang commented on May 15, 2024

追评: 我最初测试的时候是直接调用consume方法测的,没有使用注解模拟完整调用链。后来发现使用注解的情况下key是从RateLimiterUtil拼接而成的,上面有hashTag,所以1.1.1版本的代码,是可以正确运行的。大佬,对不起!
但是,把hashTag写死成常量是不合理的,这样所有的key都会路由到同一个slot,造成集群数据倾斜。通常的做法是把hashTag设置成其中一个key,离散度取决于这个key,本例取决于用户输入。

from syj-ratelimit.

shiyujun avatar shiyujun commented on May 15, 2024

多谢提出问题,现已升级为1.1.2版本.现解决方案为:
如果当前key中存在hashTag则使用key
如果不存在则依据这个key添加

from syj-ratelimit.

huadahuang1983 avatar huadahuang1983 commented on May 15, 2024

1.1.2的lua脚本貌似没有及时更新

from syj-ratelimit.

Related Issues (7)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.