Skip to content

Commit

Permalink
featapi(open): Updated management page to show consumer rate limit in…
Browse files Browse the repository at this point in the history
…formation
  • Loading branch information
youngzil committed Nov 23, 2024
1 parent a9da81d commit 22ebb4f
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.google.common.util.concurrent.RateLimiter;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
Expand Down Expand Up @@ -53,6 +54,7 @@ public class ConsumerAuthenticationFilter implements Filter {
private static final int TOO_MANY_REQUESTS = 429;

private static final Cache<String, ImmutablePair<Long, RateLimiter>> LIMITER = CacheBuilder.newBuilder()
.expireAfterAccess(1, TimeUnit.HOURS)
.maximumSize(RATE_LIMITER_CACHE_MAX_SIZE).build();

public ConsumerAuthenticationFilter(ConsumerAuthUtil consumerAuthUtil, ConsumerAuditUtil consumerAuditUtil) {
Expand Down Expand Up @@ -108,7 +110,7 @@ public void destroy() {
}

private ImmutablePair<Long, RateLimiter> getOrCreateRateLimiterPair(String key, Integer limitCount) {
try{
try {
return LIMITER.get(key, () ->
ImmutablePair.of(System.currentTimeMillis(), RateLimiter.create(limitCount)));
} catch (ExecutionException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ public List<ConsumerRole> assignNamespaceRoleToConsumer(String token, String app
private ConsumerInfo convert(
Consumer consumer,
String token,
boolean allowCreateApplication
boolean allowCreateApplication,
Integer rateLimit
) {
ConsumerInfo consumerInfo = new ConsumerInfo();
consumerInfo.setConsumerId(consumer.getId());
Expand All @@ -208,6 +209,7 @@ private ConsumerInfo convert(
consumerInfo.setOwnerEmail(consumer.getOwnerEmail());
consumerInfo.setOrgId(consumer.getOrgId());
consumerInfo.setOrgName(consumer.getOrgName());
consumerInfo.setRateLimit(rateLimit);

consumerInfo.setToken(token);
consumerInfo.setAllowCreateApplication(allowCreateApplication);
Expand All @@ -223,13 +225,17 @@ public ConsumerInfo getConsumerInfoByAppId(String appId) {
if (consumer == null) {
return null;
}
return convert(consumer, consumerToken.getToken(), isAllowCreateApplication(consumer.getId()));
return convert(consumer, consumerToken.getToken(), isAllowCreateApplication(consumer.getId()), getRateLimit(consumer.getId()));
}

private boolean isAllowCreateApplication(Long consumerId) {
return isAllowCreateApplication(Collections.singletonList(consumerId)).get(0);
}

private Integer getRateLimit(Long consumerId) {
return getRateLimit(Collections.singletonList(consumerId)).get(0);
}

private List<Boolean> isAllowCreateApplication(List<Long> consumerIdList) {
Role createAppRole = getCreateAppRole();
if (createAppRole == null) {
Expand All @@ -252,6 +258,17 @@ private List<Boolean> isAllowCreateApplication(List<Long> consumerIdList) {
return list;
}

private List<Integer> getRateLimit(List<Long> consumerIdList) {
List<Integer> list = new ArrayList<>(consumerIdList.size());
for (Long consumerId : consumerIdList) {
ConsumerToken consumerToken = consumerTokenRepository.findByConsumerId(consumerId);
Integer rateLimit = consumerToken.getRateLimit();
list.add(rateLimit);
}

return list;
}

private Role getCreateAppRole() {
return rolePermissionService.findRoleByRoleName(CREATE_APPLICATION_ROLE_NAME);
}
Expand Down Expand Up @@ -405,14 +422,15 @@ public List<ConsumerInfo> findConsumerInfoList(Pageable page) {
List<Long> consumerIdList = consumerList.stream()
.map(Consumer::getId).collect(Collectors.toList());
List<Boolean> allowCreateApplicationList = isAllowCreateApplication(consumerIdList);
List<Integer> rateLimitList = getRateLimit(consumerIdList);

List<ConsumerInfo> consumerInfoList = new ArrayList<>(consumerList.size());

for (int i = 0; i < consumerList.size(); i++) {
Consumer consumer = consumerList.get(i);
// without token
ConsumerInfo consumerInfo = convert(
consumer, null, allowCreateApplicationList.get(i)
consumer, null, allowCreateApplicationList.get(i), rateLimitList.get(i)
);
consumerInfoList.add(consumerInfo);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public class ConsumerInfo {
private String token;
private boolean allowCreateApplication;

private Integer rateLimit;

public String getAppId() {
return appId;
}
Expand Down Expand Up @@ -104,4 +106,13 @@ public boolean isAllowCreateApplication() {
public void setAllowCreateApplication(boolean allowCreateApplication) {
this.allowCreateApplication = allowCreateApplication;
}

public Integer getRateLimit() {
return rateLimit;
}

public void setRateLimit(Integer rateLimit) {
this.rateLimit = rateLimit;
}

}
8 changes: 5 additions & 3 deletions apollo-portal/src/main/resources/static/open/manage.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ <h5>{{'Open.Manage.CreateThirdApp' | translate }}
<th style="width: 10%">{{'Common.AppId' | translate }}</th>
<th style="width: 15%">{{'Common.AppName' | translate }}</th>
<th style="width: 10%">{{'Open.Manage.Consumer.AllowCreateApplication' | translate }}</th>
<th style="width: 20%">{{'Common.Department' | translate }}</th>
<th style="width: 10%">{{'Open.Manage.Consumer.RateLimitValue' | translate }}</th>
<th style="width: 15%">{{'Common.Department' | translate }}</th>
<th style="width: 20%">{{'Common.AppOwner' | translate }}/{{'Common.Email' | translate }}</th>
<th style="width: 20%">{{'Common.Operation' | translate}}</th>
</tr>
Expand All @@ -78,8 +79,9 @@ <h5>{{'Open.Manage.CreateThirdApp' | translate }}
{{'Open.Manage.Consumer.AllowCreateApplication.No' | translate}}
</div>
</td>
<td style="width: 10%">{{ consumer.rateLimit }}</td>

<td style="width: 20%">{{ consumer.orgName + '(' + consumer.orgId + ')' }}</td>
<td style="width: 15%">{{ consumer.orgName + '(' + consumer.orgId + ')' }}</td>
<td style="width: 20%"><b>{{ consumer.ownerName }}</b>/{{ consumer.ownerEmail }}</td>
<td style="width: 20%;">
<button class="btn btn-default btn-md" ng-click="preGrantPermission(consumer)">
Expand Down Expand Up @@ -172,4 +174,4 @@ <h4>{{'Common.IsRootUser' | translate }}</h4>
<script type="application/javascript" src="../scripts/controller/open/OpenManageController.js"></script>
</body>

</html>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ function OpenManageController($scope, $translate, toastr, AppUtil, OrganizationS
}

if ($scope.consumer.rateLimitEnabled) {
if ($scope.consumer.rateLimit < 1) {
if (!$scope.consumer.rateLimit || $scope.consumer.rateLimit < 1) {
toastr.warning($translate.instant('Open.Manage.Consumer.RateLimitValue.Error'));
$scope.submitBtnDisabled = false;
return;
Expand Down

0 comments on commit 22ebb4f

Please sign in to comment.