V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
hujianxin
V2EX  ›  程序员

Guava 的 RateLimiter warmup 慢启动限流,在这个例子中,永远无法达到最大值,一直处于限流状态

  •  
  •   hujianxin · 2020-01-15 10:20:28 +08:00 · 1419 次点击
    这是一个创建于 1534 天前的主题,其中的信息可能已经有所发展或是发生改变。
    @RestController
    class HelloWorldController {
        @GetMapping("/")
        public String sayHello() {
            return "hello world";
        }
    
        @GetMapping("/download")
        public ResponseEntity<Resource> download() throws FileNotFoundException {
            RateLimiter rateLimiter = GuavaRateLimiter.create(52428800L, 500, TimeUnit.MILLISECONDS)
            FileInputStream in = new FileInputStream("/home/hujianxin/Downloads/officesp2010-kb2687455-fullfile-x64-en-us.exe");
            InputStreamResource resource = new InputStreamResource(new InputStream() {
                @Override
                public int read() throws IOException {
                    rateLimiter.acquire();
                    return in.read();
                }
    
                @Override
                public int read(byte[] b) throws IOException {
                    rateLimiter.acquire(b.length);
                    return in.read(b);
                }
    
                @Override
                public int read(byte[] b, int off, int len) throws IOException {
                    rateLimiter.acquire(len);
                    return in.read(b, off, len);
                }
            });
            return ResponseEntity.ok().body(resource);
        }
    }
    

    在这个例子中,最大值是 50MB/S,但是实际速度只有 7M/s,如果移除限流相关代码,实际速度可以达到 200MB/s

    有遇到过类似问题的大佬吗?

    4 条回复    2020-01-16 09:39:18 +08:00
    inwar
        1
    inwar  
       2020-01-15 12:23:53 +08:00 via Android
    这个不是频率上线么?
    hujianxin
        2
    hujianxin  
    OP
       2020-01-15 15:26:36 +08:00
    @inwar 请教,频率上线是什么意思呢?之前没听过
    Jrue0011
        3
    Jrue0011  
       2020-01-15 17:36:30 +08:00
    1L 的意思是 RateLimiter 一般用来限制每秒对接口的访问量吧,而不是每秒从 InputStream 流读取的字节数量

    create 不加入后两个参数能有效限制在 50MB/s 吗,能的话就看看是不是后面两个参数设置的问题。。。
    hujianxin
        4
    hujianxin  
    OP
       2020-01-16 09:39:18 +08:00
    @Jrue0011 后面两个参数调过好多种了,对结果没有影响。。。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3640 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 10:44 · PVG 18:44 · LAX 03:44 · JFK 06:44
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.