Skip to content

Commit

Permalink
Increased PGCR limit to 32kb
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Villavicencio authored and Daniel Villavicencio committed Jul 25, 2024
1 parent 1c09f25 commit 3637edd
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,11 @@ public BungieClient defaultBungieClient(WebClient.Builder builder) {
@Bean
public WebClient pgcrWebClient(WebClient.Builder builder) {
// Don't keep alive connections with Bungie.net
HttpClient httpClient = HttpClient.create()
.keepAlive(false);
return builder
.baseUrl(this.statsBaseUrl)
.clientConnector(new ReactorClientHttpConnector(httpClient))
.defaultHeader(API_KEY_HEADER_NAME, this.key)
.defaultHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Implementation of this interface are responsible for creating the human-readable message that
* will be sent through Discord to respond to a particular slash-command
*/
@FunctionalInterface
public interface ApplicationCommandSource {

/**
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/com/deahtstroke/rivenbot/service/PGCRService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.deahtstroke.rivenbot.service;

import com.deahtstroke.rivenbot.config.BungieConfiguration;
import com.deahtstroke.rivenbot.dto.destiny.BungieResponse;
import com.deahtstroke.rivenbot.dto.destiny.PostGameCarnageReport;
import com.deahtstroke.rivenbot.entity.PGCRDetails;
Expand All @@ -8,8 +9,10 @@
import com.deahtstroke.rivenbot.repository.PGCRRepository;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.resilience4j.reactor.ratelimiter.operator.RateLimiterOperator;
import java.util.concurrent.atomic.AtomicInteger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.BodyExtractors;
Expand All @@ -24,7 +27,7 @@
public class PGCRService {

private static final String PGCR_ENDPOINT_URL = "/Destiny2/Stats/PostGameCarnageReport/{activityId}/";
private static final Integer PGCR_SIZE_LIMIT_BYTES = 16_000;
private static final Integer PGCR_SIZE_LIMIT_BYTES = 32_000;

private final WebClient webClient;
private final PGCRMapper pgcrMapper;
Expand Down Expand Up @@ -62,7 +65,7 @@ public Mono<PGCRDetails> retrievePGCR(Long activityInstanceId) {
clientResponse -> clientResponse.body(BodyExtractors.toDataBuffers()))
.concatMap(dataBuffer -> {
int chunkSize = dataBuffer.readableByteCount();
if (chunkSize + currentSize.get() > PGCR_SIZE_LIMIT_BYTES) {
if (dataBuffer.readableByteCount() + currentSize.get() > PGCR_SIZE_LIMIT_BYTES) {
return Mono.error(new PGCRSizeLimitException(
"PGCR with Id [%s] exceeded the size limit of 16 KBs".formatted(
activityInstanceId)));
Expand All @@ -71,6 +74,7 @@ public Mono<PGCRDetails> retrievePGCR(Long activityInstanceId) {
return Mono.just(dataBuffer);
}
})
.transformDeferred(RateLimiterOperator.of(BungieConfiguration.PGCR_RATE_LIMITER))
.collectList()
.flatMap(buffers -> DataBufferUtils.join(Flux.fromIterable(buffers)))
.flatMap(buffer -> {
Expand All @@ -86,9 +90,9 @@ public Mono<PGCRDetails> retrievePGCR(Long activityInstanceId) {
.onErrorResume(PGCRSizeLimitException.class, ex -> Mono.just(
BungieResponse.of(PostGameCarnageReport.EMPTY_RESPONSE)))
.flatMap(response -> pgcrRepository.save(
pgcrMapper.dtoToEntity(response.getResponse(), activityInstanceId)));
pgcrMapper.dtoToEntity(response.getResponse(), activityInstanceId)))
.doOnDiscard(DataBuffer.class, DataBufferUtils::release);
}
});
}

}

0 comments on commit 3637edd

Please sign in to comment.