@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
有遇到过类似问题的大佬吗?