From 558cd0e260129266ac4d95137ecd0517888843f0 Mon Sep 17 00:00:00 2001 From: Nisan Abeywickrama <29643986+nisan-abeywickrama@users.noreply.github.com> Date: Sun, 13 Oct 2024 10:54:39 +0530 Subject: [PATCH 1/2] Add nextAccess timestamp to synapse context on burst control throttled --- .../apimgt/gateway/handlers/throttling/ThrottleHandler.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/throttling/ThrottleHandler.java b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/throttling/ThrottleHandler.java index 5341f9ce8f30..a1ed0354a1ea 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/throttling/ThrottleHandler.java +++ b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/throttling/ThrottleHandler.java @@ -42,6 +42,7 @@ import org.apache.synapse.commons.throttle.core.ThrottleContext; import org.apache.synapse.commons.throttle.core.ThrottleException; import org.apache.synapse.commons.throttle.core.ThrottleFactory; +import org.apache.synapse.commons.throttle.core.CallerContext; import org.apache.synapse.commons.throttle.core.factory.ThrottleContextFactory; import org.apache.synapse.core.SynapseEnvironment; import org.apache.synapse.core.axis2.Axis2MessageContext; @@ -1233,6 +1234,9 @@ public boolean isSubscriptionLevelSpike(MessageContext synCtx, String throttleKe } if (info != null && !info.isAccessAllowed()) { + CallerContext callerContext = subscriptionLevelSpikeArrestThrottleContext.getCallerContext(throttleKey); + long timestamp = callerContext.getNextAccessTime(); + synCtx.setProperty(APIThrottleConstants.THROTTLED_NEXT_ACCESS_TIMESTAMP, timestamp); synCtx.setProperty(APIThrottleConstants.THROTTLED_OUT_REASON, APIThrottleConstants.SUBSCRIPTON_BURST_LIMIT_EXCEEDED); log.debug("Subscription level burst control limit exceeded for key " + throttleKey); return true; From 0f11d8b6f7d03fec805007b697395d12b35e4543 Mon Sep 17 00:00:00 2001 From: Nisan Abeywickrama <29643986+nisan-abeywickrama@users.noreply.github.com> Date: Sun, 13 Oct 2024 22:43:07 +0530 Subject: [PATCH 2/2] Remove subscription burst control throttle test --- .../throttling/ThrottleHandlerTest.java | 31 ------------------- 1 file changed, 31 deletions(-) diff --git a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/test/java/org/wso2/carbon/apimgt/gateway/handlers/throttling/ThrottleHandlerTest.java b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/test/java/org/wso2/carbon/apimgt/gateway/handlers/throttling/ThrottleHandlerTest.java index 475e9f4acb7b..0e08758387ba 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/test/java/org/wso2/carbon/apimgt/gateway/handlers/throttling/ThrottleHandlerTest.java +++ b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/test/java/org/wso2/carbon/apimgt/gateway/handlers/throttling/ThrottleHandlerTest.java @@ -472,37 +472,6 @@ public void testMsgThrottleOutWhenCustomThrottlingLimitExceeded() { throttleDataHolder.removeKeyTemplate("$user"); } - @Test - public void testMsgThrottleOutWhenHittingSubscriptionLevelSpike() { - ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance(); - - ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator, - accessInformation); - throttleHandler.setSandboxMaxCount("100"); - SynapseEnvironment synapseEnvironment = Mockito.mock(SynapseEnvironment.class); - throttleHandler.init(synapseEnvironment); - - MessageContext messageContext = TestUtils.getMessageContextWithAuthContext(apiContext, apiVersion); - messageContext.setProperty(VERB_INFO_DTO, verbInfoDTO); - ((Axis2MessageContext) messageContext).getAxis2MessageContext().getProperty(org.apache.axis2.context - .MessageContext.TRANSPORT_HEADERS); - AuthenticationContext authenticationContext = (AuthenticationContext) messageContext.getProperty - (API_AUTH_CONTEXT); - authenticationContext.setApiTier(throttlingTier); - authenticationContext.setKeyType("SANDBOX"); - authenticationContext.setSpikeArrestLimit(100); - authenticationContext.setStopOnQuotaReach(true); - - messageContext.setProperty(API_AUTH_CONTEXT, authenticationContext); - - verbInfo.setConditionGroups(conditionGroupDTOs); - ArrayList matchingConditions = new ArrayList<>(); - matchingConditions.add(conditionGroupDTO); - throttleDataHolder.addKeyTemplate("$user", "$user"); - Mockito.when(accessInformation.isAccessAllowed()).thenReturn(false); - Assert.assertFalse(throttleHandler.handleRequest(messageContext)); - } - @Test public void testHandleResponse() { ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance();