diff --git a/build.gradle b/build.gradle index 2bd84ee..a123d97 100644 --- a/build.gradle +++ b/build.gradle @@ -43,6 +43,8 @@ ext { set('springDataRedisVersion', '3.1.2') set('springSessionRedisVersion', '3.1.1') set('mapStructVersion', '1.5.5.Final') + set('resilienceReactorVersion', '1.6.1') + set('resilienceRateLimiterVersion', '1.6.1') } jar { @@ -56,6 +58,8 @@ dependencies { implementation "org.springframework.session:spring-session-data-redis:${springSessionRedisVersion}" implementation "org.springframework.boot:spring-boot-starter-data-redis:${springDataRedisVersion}" implementation "org.springframework.boot:spring-boot-starter-data-mongodb-reactive:${springDataMongoDbVersion}" + implementation "io.github.resilience4j:resilience4j-reactor:${resilienceReactorVersion}" + implementation "io.github.resilience4j:resilience4j-ratelimiter:${resilienceRateLimiterVersion}" implementation "org.mapstruct:mapstruct:${mapStructVersion}" implementation "net.i2p.crypto:eddsa:${i2pCryptoVersion}" implementation "commons-io:commons-io:${IOCommonsVersion}" diff --git a/src/main/java/com/danielvm/destiny2bot/service/PostGameCarnageService.java b/src/main/java/com/danielvm/destiny2bot/service/PostGameCarnageService.java index fb294a1..b953d8f 100644 --- a/src/main/java/com/danielvm/destiny2bot/service/PostGameCarnageService.java +++ b/src/main/java/com/danielvm/destiny2bot/service/PostGameCarnageService.java @@ -6,6 +6,10 @@ import com.danielvm.destiny2bot.entity.PGCRDetails; import com.danielvm.destiny2bot.mapper.PGCRMapper; import com.danielvm.destiny2bot.repository.PGCRRepository; +import io.github.resilience4j.ratelimiter.RateLimiter; +import io.github.resilience4j.ratelimiter.RateLimiterConfig; +import io.github.resilience4j.reactor.ratelimiter.operator.RateLimiterOperator; +import java.time.Duration; import java.util.Collections; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; @@ -16,6 +20,13 @@ @Slf4j public class PostGameCarnageService { + private static final RateLimiter PGCR_RATE_LIMITER = RateLimiter.of("pgcr-rate-limiter", + RateLimiterConfig.custom() + .limitForPeriod(25) + .limitRefreshPeriod(Duration.ofSeconds(1)) + .timeoutDuration(Duration.ofSeconds(30)) + .build()); + private final BungieClient bungieClient; private final PGCRMapper pgcrMapper; private final PGCRRepository pgcrRepository; @@ -41,6 +52,7 @@ public Mono retrievePGCR(Long activityInstanceId) { Collections.emptyList()); Mono retrievePGCR = bungieClient.getPostGameCarnageReport(activityInstanceId) + .transformDeferred(RateLimiterOperator.of(PGCR_RATE_LIMITER)) .onErrorResume(WebClientException.class, err -> { log.warn("Response too big to parse, ignoring and falling back to default value", err); return Mono.just(new BungieResponse<>(errorFallbackResponse));