Skip to content

Commit

Permalink
feat(jans-auth-server): added cache support to /stat endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
yuriyz committed Feb 3, 2022
1 parent 4e0ea4a commit e1dba92
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ public class AppConfiguration implements Configuration {
private Boolean umaRestrictResourceToAssociatedClient = false;

private int statTimerIntervalInSeconds;
private int statWebServiceIntervalLimitInSeconds;
private String statAuthorizationScope;

private int spontaneousScopeLifetime;
Expand Down Expand Up @@ -1160,14 +1159,6 @@ public void setUserInfoEncryptionAlgValuesSupported(List<String> userInfoEncrypt
this.userInfoEncryptionAlgValuesSupported = userInfoEncryptionAlgValuesSupported;
}

public int getStatWebServiceIntervalLimitInSeconds() {
return statWebServiceIntervalLimitInSeconds;
}

public void setStatWebServiceIntervalLimitInSeconds(int statWebServiceIntervalLimitInSeconds) {
this.statWebServiceIntervalLimitInSeconds = statWebServiceIntervalLimitInSeconds;
}

public int getStatTimerIntervalInSeconds() {
return statTimerIntervalInSeconds;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.jans.as.server.ws.rs.stat;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import io.jans.as.common.model.stat.StatEntry;
import io.jans.as.model.common.ComponentType;
import io.jans.as.model.config.Constants;
Expand Down Expand Up @@ -39,6 +41,7 @@
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static io.jans.as.model.util.Util.escapeLog;

Expand All @@ -54,8 +57,6 @@
@Path("/internal/stat")
public class StatWS {

private static final int DEFAULT_WS_INTERVAL_LIMIT_IN_SECONDS = 60;

@Inject
private Logger log;

Expand All @@ -74,7 +75,10 @@ public class StatWS {
@Inject
private TokenService tokenService;

private long lastProcessedAt;
private final Cache<String, StatResponse> responseCache = CacheBuilder
.newBuilder()
.expireAfterWrite(1, TimeUnit.HOURS)
.build();

public static String createOpenMetricsResponse(StatResponse statResponse) throws IOException {
Writer writer = new StringWriter();
Expand Down Expand Up @@ -169,13 +173,6 @@ public Response stat(String authorization, String month, String format) {
validateAuthorization(authorization);
final List<String> months = validateMonth(month);

if (!allowToRun()) {
log.trace("Interval request limit exceeded. Request is rejected. Current interval limit: {} (or 60 seconds if not set).", appConfiguration.getStatWebServiceIntervalLimitInSeconds());
throw errorResponseFactory.createWebApplicationException(Response.Status.FORBIDDEN, TokenErrorResponseType.ACCESS_DENIED, "Interval request limit exceeded.");
}

lastProcessedAt = System.currentTimeMillis();

try {
if (log.isTraceEnabled())
log.trace("Recognized months: {}", escapeLog(months));
Expand Down Expand Up @@ -203,6 +200,12 @@ public Response stat(String authorization, String month, String format) {
}

private StatResponse buildResponse(List<String> months) {
final String cacheKey = months.toString();
final StatResponse cachedResponse = responseCache.getIfPresent(cacheKey);
if (cachedResponse != null) {
return cachedResponse;
}

StatResponse response = new StatResponse();
for (String month : months) {
final StatResponseItem responseItem = buildItem(month);
Expand All @@ -211,6 +214,7 @@ private StatResponse buildResponse(List<String> months) {
}
}

responseCache.put(cacheKey, response);
return response;
}

Expand Down Expand Up @@ -326,17 +330,4 @@ private List<String> validateMonth(String month) {

return months;
}

private boolean allowToRun() {
int interval = appConfiguration.getStatWebServiceIntervalLimitInSeconds();
if (interval <= 0) {
interval = DEFAULT_WS_INTERVAL_LIMIT_IN_SECONDS;
}

long timerInterval = interval * 1000L;

long timeDiff = System.currentTimeMillis() - lastProcessedAt;

return timeDiff >= timerInterval;
}
}

0 comments on commit e1dba92

Please sign in to comment.