Skip to content

Commit

Permalink
refactor(ConsumerService): Optimize the implementation of getRateLimi…
Browse files Browse the repository at this point in the history
…t method
  • Loading branch information
youngzil committed Nov 27, 2024
1 parent 9126bb3 commit ddfd8b3
Showing 1 changed file with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import com.google.common.hash.Hashing;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import org.apache.commons.lang3.time.FastDateFormat;
import org.springframework.data.domain.Pageable;
Expand Down Expand Up @@ -235,7 +236,7 @@ private boolean isAllowCreateApplication(Long consumerId) {

private Integer getRateLimit(Long consumerId) {
List<Integer> list = getRateLimit(Collections.singletonList(consumerId));
if(CollectionUtils.isEmpty(list)){
if (CollectionUtils.isEmpty(list)) {
return 0;

Check warning on line 240 in apollo-portal/src/main/java/com/ctrip/framework/apollo/openapi/service/ConsumerService.java

View check run for this annotation

Codecov / codecov/patch

apollo-portal/src/main/java/com/ctrip/framework/apollo/openapi/service/ConsumerService.java#L240

Added line #L240 was not covered by tests
}
return list.get(0);
Expand Down Expand Up @@ -264,13 +265,16 @@ private List<Boolean> isAllowCreateApplication(List<Long> consumerIdList) {
}

private List<Integer> getRateLimit(List<Long> consumerIds) {
List<Integer> list = new ArrayList<>(consumerIds.size());
List<ConsumerToken> consumerTokens = consumerTokenRepository.findByConsumerIdIn(consumerIds);
for (ConsumerToken consumerToken : consumerTokens) {
Integer rateLimit = consumerToken != null ? consumerToken.getRateLimit() : 0;
list.add(rateLimit);
}
return list;
Map<Long, Integer> consumerRateLimits = consumerTokens.stream()
.collect(Collectors.toMap(
ConsumerToken::getConsumerId,
consumerToken -> consumerToken.getRateLimit() != null ? consumerToken.getRateLimit() : 0
));

return consumerIds.stream()
.map(id -> consumerRateLimits.getOrDefault(id, 0))
.collect(Collectors.toList());
}

private Role getCreateAppRole() {
Expand Down

0 comments on commit ddfd8b3

Please sign in to comment.