feat: implement rate limiting for image downloads in MangaChapterService

This commit is contained in:
Rodrigo Verdiani 2025-10-27 14:53:13 -03:00
parent c0d4e4084a
commit 948dd7fb9e
2 changed files with 11 additions and 1 deletions

View File

@ -15,4 +15,9 @@ public class RateLimiterConfig {
public RateLimiter jikanRateLimiter() { public RateLimiter jikanRateLimiter() {
return RateLimiter.create(1); return RateLimiter.create(1);
} }
@Bean
public RateLimiter imageDownloadRateLimiter() {
return RateLimiter.create(10);
}
} }

View File

@ -1,5 +1,6 @@
package com.magamochi.mangamochi.service; package com.magamochi.mangamochi.service;
import com.google.common.util.concurrent.RateLimiter;
import com.magamochi.mangamochi.exception.UnprocessableException; import com.magamochi.mangamochi.exception.UnprocessableException;
import com.magamochi.mangamochi.model.dto.MangaChapterArchiveDTO; import com.magamochi.mangamochi.model.dto.MangaChapterArchiveDTO;
import com.magamochi.mangamochi.model.dto.MangaChapterImagesDTO; import com.magamochi.mangamochi.model.dto.MangaChapterImagesDTO;
@ -36,6 +37,8 @@ public class MangaChapterService {
private final ContentProviderFactory contentProviderFactory; private final ContentProviderFactory contentProviderFactory;
private final RateLimiter imageDownloadRateLimiter;
@Transactional @Transactional
public void fetchChapter(Long chapterId) { public void fetchChapter(Long chapterId) {
var chapter = getMangaChapterThrowIfNotFound(chapterId); var chapter = getMangaChapterThrowIfNotFound(chapterId);
@ -50,9 +53,11 @@ public class MangaChapterService {
} }
var chapterImages = var chapterImages =
chapterImagesUrls.entrySet().stream() chapterImagesUrls.entrySet().parallelStream()
.map( .map(
entry -> { entry -> {
imageDownloadRateLimiter.acquire();
try { try {
var inputStream = var inputStream =
new BufferedInputStream( new BufferedInputStream(