added rate limiter and improved payment start entity
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
package com.billpaymentservice.bbps.config;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import jakarta.servlet.FilterChain;
|
||||
import jakarta.servlet.ServletException;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.filter.OncePerRequestFilter;
|
||||
import com.billpaymentservice.bbps.util.BBPSConstant;
|
||||
import com.billpaymentservice.bbps.dto.ResponseModel;
|
||||
|
||||
import io.github.resilience4j.ratelimiter.RateLimiter;
|
||||
import io.github.resilience4j.ratelimiter.RateLimiterRegistry;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
@Component
|
||||
@Order(1)
|
||||
public class RateLimitingFilter extends OncePerRequestFilter {
|
||||
|
||||
private final RateLimiterRegistry rateLimiterRegistry;
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(RateLimitingFilter.class);
|
||||
|
||||
// Har MID ke liye alag limiter
|
||||
private final Map<String, RateLimiter> limiterCache = new ConcurrentHashMap<>();
|
||||
|
||||
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
||||
|
||||
public RateLimitingFilter(RateLimiterRegistry rateLimiterRegistry) {
|
||||
this.rateLimiterRegistry = rateLimiterRegistry;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doFilterInternal(HttpServletRequest request,
|
||||
HttpServletResponse response,
|
||||
FilterChain filterChain)
|
||||
throws ServletException, IOException {
|
||||
String mid = request.getHeader("mid");
|
||||
if(mid == null) {
|
||||
mid = "GLOBAL";
|
||||
}
|
||||
|
||||
// Validate MID
|
||||
if (mid == null || mid.isBlank()) {
|
||||
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
|
||||
response.setContentType("application/json");
|
||||
|
||||
ResponseModel<Object> apiResponse = new ResponseModel<>("mid cannot be null or blank", "FAILURE", HttpServletResponse.SC_BAD_REQUEST, null);
|
||||
response.getWriter().write(OBJECT_MAPPER.writeValueAsString(apiResponse));
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
RateLimiter rateLimiter = limiterCache.computeIfAbsent(
|
||||
mid,
|
||||
key -> rateLimiterRegistry.rateLimiter("default")
|
||||
);
|
||||
|
||||
// Rate limit check
|
||||
if (!rateLimiter.acquirePermission()) {
|
||||
|
||||
// Prefer existing constant if it maps to 429, otherwise fallback to literal 429
|
||||
int tooManyRequestsStatus =429;
|
||||
|
||||
response.setStatus(tooManyRequestsStatus);
|
||||
response.setContentType("application/json");
|
||||
|
||||
ResponseModel<Object> apiResponse = new ResponseModel<>("Rate limit exceeded for MID: " + mid, "FAILURE", tooManyRequestsStatus, null);
|
||||
response.getWriter().write(OBJECT_MAPPER.writeValueAsString(apiResponse));
|
||||
return;
|
||||
}
|
||||
|
||||
// Proceed with the chain and catch downstream exceptions to return consistent model
|
||||
try {
|
||||
filterChain.doFilter(request, response);
|
||||
} catch (Exception ex) {
|
||||
LOGGER.error("Unhandled exception in filter chain for MID: {}", mid, ex);
|
||||
if (!response.isCommitted()) {
|
||||
response.reset();
|
||||
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
|
||||
response.setContentType("application/json");
|
||||
ResponseModel<Object> apiResponse = new ResponseModel<>("Internal server error", "FAILURE", HttpServletResponse.SC_INTERNAL_SERVER_ERROR, null);
|
||||
response.getWriter().write(OBJECT_MAPPER.writeValueAsString(apiResponse));
|
||||
}
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
LOGGER.error("Error while rate limiting request", e);
|
||||
if (!response.isCommitted()) {
|
||||
response.reset();
|
||||
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
|
||||
response.setContentType("application/json");
|
||||
ResponseModel<Object> apiResponse = new ResponseModel<>("Internal server error", "FAILURE", HttpServletResponse.SC_INTERNAL_SERVER_ERROR, null);
|
||||
response.getWriter().write(OBJECT_MAPPER.writeValueAsString(apiResponse));
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean shouldNotFilter(HttpServletRequest request) {
|
||||
|
||||
String path = request.getRequestURI();
|
||||
|
||||
return path.startsWith("/swagger")
|
||||
|| path.startsWith("/v3/api-docs")
|
||||
|| path.startsWith("/actuator");
|
||||
}
|
||||
}
|
||||
@@ -130,6 +130,7 @@ public class BBPSConstant extends BBPS_RESPONSE_COD_CONSTANT {
|
||||
public static final int HTTP_OK_INT = 200;
|
||||
public static final int HTTP_NOT_FOUND = 404;
|
||||
public static final int HTTP_INTERNAL_SERVER_ERROR = 500;
|
||||
public static final int HTTP_TO_MANY_REQUEST=429;
|
||||
|
||||
// Response and helper keys
|
||||
public static final String HTTP_SERVICE_ERROR_PREFIX = "HttpService Error";
|
||||
|
||||
@@ -55,6 +55,9 @@ master.data.url = https://bbps-staging.digiledge.in/agent/cou-master/masters
|
||||
agent.data.url=https://bbps-staging.digiledge.in/agent/bill-payment-new
|
||||
payment.data.url=https://bbps-staging.digiledge.in/couapp
|
||||
|
||||
resilience4j.ratelimiter.configs.default.limit-for-period=1000
|
||||
resilience4j.ratelimiter.configs.default.limit-refresh-period=1m
|
||||
resilience4j.ratelimiter.configs.default.timeout-duration=0
|
||||
|
||||
# Resilience4j Retry
|
||||
resilience4j.retry.instances.callbackService.max-attempts=1
|
||||
|
||||
@@ -72,7 +72,13 @@ spring.mvc.pathmatch.matching-strategy=ANT_PATH_MATCHER
|
||||
jwt.secret=6C756148487475787A414646736D4B6E444E585A766145396E6A4555594A595566657A6E434D62536856674A704C494F6C6E666B366A4A6A6D5630323564716C614F
|
||||
|
||||
|
||||
# ===============================
|
||||
# Resilience4j Rate Limiter
|
||||
# ===============================
|
||||
|
||||
resilience4j.ratelimiter.configs.default.limit-for-period=5
|
||||
resilience4j.ratelimiter.configs.default.limit-refresh-period=1m
|
||||
resilience4j.ratelimiter.configs.default.timeout-duration=0
|
||||
# Resilience4j Retry
|
||||
resilience4j.retry.instances.callbackService.max-attempts=1
|
||||
resilience4j.retry.instances.callbackService.wait-duration=2s
|
||||
|
||||
Reference in New Issue
Block a user