feat: implement rate limiting for image downloads in MangaChapterService
This commit is contained in:
parent
c0d4e4084a
commit
948dd7fb9e
@ -15,4 +15,9 @@ public class RateLimiterConfig {
|
||||
public RateLimiter jikanRateLimiter() {
|
||||
return RateLimiter.create(1);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RateLimiter imageDownloadRateLimiter() {
|
||||
return RateLimiter.create(10);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
package com.magamochi.mangamochi.service;
|
||||
|
||||
import com.google.common.util.concurrent.RateLimiter;
|
||||
import com.magamochi.mangamochi.exception.UnprocessableException;
|
||||
import com.magamochi.mangamochi.model.dto.MangaChapterArchiveDTO;
|
||||
import com.magamochi.mangamochi.model.dto.MangaChapterImagesDTO;
|
||||
@ -36,6 +37,8 @@ public class MangaChapterService {
|
||||
|
||||
private final ContentProviderFactory contentProviderFactory;
|
||||
|
||||
private final RateLimiter imageDownloadRateLimiter;
|
||||
|
||||
@Transactional
|
||||
public void fetchChapter(Long chapterId) {
|
||||
var chapter = getMangaChapterThrowIfNotFound(chapterId);
|
||||
@ -50,9 +53,11 @@ public class MangaChapterService {
|
||||
}
|
||||
|
||||
var chapterImages =
|
||||
chapterImagesUrls.entrySet().stream()
|
||||
chapterImagesUrls.entrySet().parallelStream()
|
||||
.map(
|
||||
entry -> {
|
||||
imageDownloadRateLimiter.acquire();
|
||||
|
||||
try {
|
||||
var inputStream =
|
||||
new BufferedInputStream(
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user