Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

9672 prodapi usage implement the changes required to fix the event stats #7248

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ public class PapiRateLimitRedisClient {
private int CASH_EXPIRY_IN_SECONDS; // caching for 2 days to have time to
// synch with DB

@Value("${org.orcid.papi.rate.limit.anonymous.requests:10000}")
private int anonymousRequestLimit;

@Value("${org.orcid.papi.rate.limit.known.requests:40000}")
private int knownRequestLimit;

@Autowired
private PublicApiDailyRateLimitDao papiRateLimitingDao;

Expand All @@ -46,7 +52,7 @@ public String getTodayKeyByClient(String client) {
}

public String getRequestDateKeyByClient(String client, LocalDate requestDate) {
return client + KEY_DELIMITATOR + requestDate.toString() ;
return client + KEY_DELIMITATOR + requestDate.toString();
}

public JSONObject getDailyLimitsForClient(String client, LocalDate requestDate) {
Expand Down Expand Up @@ -75,17 +81,23 @@ public void saveRedisPapiLimitDateToDB(LocalDate requestDate) throws JSONExcepti
PublicApiDailyRateLimitEntity redisRateLimitEntity = redisObjJsonToEntity(allValuesForKey.get(key));
PublicApiDailyRateLimitEntity pgRateLimitEntity = null;
boolean isClient = false;
if(StringUtils.isNotEmpty(redisRateLimitEntity.getIpAddress())) {
pgRateLimitEntity = papiRateLimitingDao.findByIpAddressAndRequestDate(redisRateLimitEntity.getIpAddress(), requestDate);
}
else if(StringUtils.isNotEmpty(redisRateLimitEntity.getClientId())){
pgRateLimitEntity = papiRateLimitingDao.findByClientIdAndRequestDate(redisRateLimitEntity.getClientId(), requestDate);
if (StringUtils.isNotEmpty(redisRateLimitEntity.getIpAddress())) {
pgRateLimitEntity = papiRateLimitingDao.findByIpAddressAndRequestDate(redisRateLimitEntity.getIpAddress(), requestDate);
} else if (StringUtils.isNotEmpty(redisRateLimitEntity.getClientId())) {
pgRateLimitEntity = papiRateLimitingDao.findByClientIdAndRequestDate(redisRateLimitEntity.getClientId(), requestDate);
isClient = true;
}
if(pgRateLimitEntity != null) {
papiRateLimitingDao.updatePublicApiDailyRateLimit(pgRateLimitEntity, isClient);
// only save the exceeded limits
if (pgRateLimitEntity != null) {
if (((pgRateLimitEntity.getRequestCount() > knownRequestLimit) && isClient)
|| ((pgRateLimitEntity.getRequestCount() > anonymousRequestLimit) && !isClient)) {
papiRateLimitingDao.updatePublicApiDailyRateLimit(pgRateLimitEntity, isClient);
}
} else {
papiRateLimitingDao.persist(redisObjJsonToEntity(allValuesForKey.get(key)));
if (((redisRateLimitEntity.getRequestCount() > knownRequestLimit) && isClient)
|| ((redisRateLimitEntity.getRequestCount() > anonymousRequestLimit) && !isClient)) {
papiRateLimitingDao.persist(redisRateLimitEntity);
}
}
redisClient.remove(key);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,12 @@ private void rateLimitAnonymousRequest(String ipAddress, LocalDate today, HttpSe
dailyLimitsObj.put(PapiRateLimitRedisClient.KEY_LAST_MODIFIED, System.currentTimeMillis());
papiRedisClient.setTodayLimitsForClient(ipAddress, dailyLimitsObj);
if (Features.ENABLE_PAPI_RATE_LIMITING.isActive() && (limitValue + 1) >= anonymousRequestLimit) {
if (enablePanoplyPapiExceededRateInProduction) {
PanoplyPapiDailyRateExceededItem item = new PanoplyPapiDailyRateExceededItem();
item.setIpAddress(ipAddress);
item.setRequestDate(today);
setPapiRateExceededItemInPanoply(item);
}
httpServletResponse.setStatus(HttpStatus.TOO_MANY_REQUESTS.value());
if (!httpServletResponse.isCommitted()) {
try (PrintWriter writer = httpServletResponse.getWriter()) {
Expand Down