@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
有遇到过类似问题的大佬吗?
1
inwar 2020-01-15 12:23:53 +08:00 via Android
这个不是频率上线么?
|
3
Jrue0011 2020-01-15 17:36:30 +08:00
1L 的意思是 RateLimiter 一般用来限制每秒对接口的访问量吧,而不是每秒从 InputStream 流读取的字节数量
create 不加入后两个参数能有效限制在 50MB/s 吗,能的话就看看是不是后面两个参数设置的问题。。。 |