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

2020-01-15 10:20:28 +08:00
 hujianxin
@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

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

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

create 不加入后两个参数能有效限制在 50MB/s 吗,能的话就看看是不是后面两个参数设置的问题。。。
hujianxin
2020-01-16 09:39:18 +08:00
@Jrue0011 后面两个参数调过好多种了,对结果没有影响。。。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/638020

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX