Search in sources :

Example 1 with CallerConfiguration

use of org.apache.synapse.commons.throttle.core.CallerConfiguration in project carbon-apimgt by wso2.

the class APIThrottleHandlerTest method init.

@Before
public void init() {
    throttleContext = Mockito.mock(ThrottleContext.class);
    timer = Mockito.mock(Timer.class);
    context = Mockito.mock(Timer.Context.class);
    throttleConfiguration = Mockito.mock(ThrottleConfiguration.class);
    callerConfiguration = Mockito.mock(CallerConfiguration.class);
    Mockito.when(timer.start()).thenReturn(context);
    messageContext = TestUtils.getMessageContextWithAuthContext(apiContext, apiVersion);
    axisConfiguration = new AxisConfiguration();
    configurationContext = new ConfigurationContext(axisConfiguration);
    throttleKey = "throttle_" + throttleID + "_cac_key";
    verbInfoDTO = new VerbInfoDTO();
    verbInfoDTO.setThrottling("Silver");
    verbInfoDTO.setRequestKey("/weather");
    verbInfoDTO.setHttpVerb("GET");
    apiThrottleHandler = new APIThrottleHandlerWrapper(timer, throttleContext);
}
Also used : ThrottleContext(org.apache.synapse.commons.throttle.core.ThrottleContext) RoleBaseThrottleContext(org.apache.synapse.commons.throttle.core.impl.rolebase.RoleBaseThrottleContext) ConfigurationContext(org.apache.axis2.context.ConfigurationContext) AuthenticationContext(org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext) ThrottleContext(org.apache.synapse.commons.throttle.core.ThrottleContext) MessageContext(org.apache.synapse.MessageContext) RoleBaseThrottleContext(org.apache.synapse.commons.throttle.core.impl.rolebase.RoleBaseThrottleContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) AxisConfiguration(org.apache.axis2.engine.AxisConfiguration) ConfigurationContext(org.apache.axis2.context.ConfigurationContext) CallerConfiguration(org.apache.synapse.commons.throttle.core.CallerConfiguration) Timer(org.wso2.carbon.metrics.manager.Timer) VerbInfoDTO(org.wso2.carbon.apimgt.impl.dto.VerbInfoDTO) ThrottleConfiguration(org.apache.synapse.commons.throttle.core.ThrottleConfiguration) Before(org.junit.Before)

Example 2 with CallerConfiguration

use of org.apache.synapse.commons.throttle.core.CallerConfiguration in project carbon-apimgt by wso2.

the class ThrottleHandler method initThrottleForSubscriptionLevelSpikeArrest.

/**
 * This method will intialize subscription level throttling context and throttle object.
 * This method need to be called for each and every request of spike arrest is enabled.
 * If throttle context for incoming message is already created method will do nothing. Else
 * it will create throttle object and context.
 *
 * @param synCtx synapse messaginitThrottleForSubscriptionLevelSpikeArreste context which contains message data
 */
private void initThrottleForSubscriptionLevelSpikeArrest(MessageContext synCtx, AuthenticationContext authenticationContext) {
    policyKey = authenticationContext.getTier();
    String apiContext = (String) synCtx.getProperty(RESTConstants.REST_API_CONTEXT);
    String apiVersion = (String) synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION);
    String subscriptionLevelThrottleKey = getSubscriptionLevelThrottleKey(policyKey, authenticationContext, apiContext, apiVersion);
    int maxRequestCount = authenticationContext.getSpikeArrestLimit();
    if (maxRequestCount != 0) {
        String unitTime = authenticationContext.getSpikeArrestUnit();
        int spikeArrestWindowUnitTime;
        if (APIThrottleConstants.MIN.equalsIgnoreCase(unitTime)) {
            spikeArrestWindowUnitTime = 60000;
        } else {
            spikeArrestWindowUnitTime = 1000;
        }
        try {
            synchronized (this) {
                if (throttle == null) {
                    OMElement spikeArrestSubscriptionLevelPolicy = createSpikeArrestSubscriptionLevelPolicy(subscriptionLevelThrottleKey, maxRequestCount, spikeArrestWindowUnitTime);
                    if (spikeArrestSubscriptionLevelPolicy != null) {
                        throttle = ThrottleFactory.createMediatorThrottle(PolicyEngine.getPolicy(spikeArrestSubscriptionLevelPolicy));
                    }
                } else {
                    boolean createSpikeArrestSubscriptionLevelPolicy = false;
                    if (throttle.getThrottleContext(subscriptionLevelThrottleKey) == null) {
                        createSpikeArrestSubscriptionLevelPolicy = true;
                    } else {
                        CallerConfiguration existingCallerConfig = throttle.getThrottleContext(subscriptionLevelThrottleKey).getThrottleConfiguration().getCallerConfiguration(subscriptionLevelThrottleKey);
                        if (existingCallerConfig.getMaximumRequestPerUnitTime() != maxRequestCount || existingCallerConfig.getUnitTime() != spikeArrestWindowUnitTime) {
                            createSpikeArrestSubscriptionLevelPolicy = true;
                        }
                    }
                    if (createSpikeArrestSubscriptionLevelPolicy) {
                        OMElement spikeArrestSubscriptionLevelPolicy = createSpikeArrestSubscriptionLevelPolicy(subscriptionLevelThrottleKey, maxRequestCount, spikeArrestWindowUnitTime);
                        if (spikeArrestSubscriptionLevelPolicy != null) {
                            Throttle tempThrottle = ThrottleFactory.createMediatorThrottle(PolicyEngine.getPolicy(spikeArrestSubscriptionLevelPolicy));
                            ThrottleConfiguration newThrottleConfig = tempThrottle.getThrottleConfiguration(ThrottleConstants.ROLE_BASED_THROTTLE_KEY);
                            ThrottleContext subscriptionLevelSpikeThrottle = ThrottleContextFactory.createThrottleContext(ThrottleConstants.ROLE_BASE, newThrottleConfig);
                            throttle.addThrottleContext(subscriptionLevelThrottleKey, subscriptionLevelSpikeThrottle);
                        }
                    }
                }
            }
        } catch (ThrottleException e) {
            log.error("Error while initializing throttling object for subscription level spike arrest policy" + e.getMessage());
        }
    }
}
Also used : ThrottleContext(org.apache.synapse.commons.throttle.core.ThrottleContext) CallerConfiguration(org.apache.synapse.commons.throttle.core.CallerConfiguration) ThrottleException(org.apache.synapse.commons.throttle.core.ThrottleException) ThrottleConfiguration(org.apache.synapse.commons.throttle.core.ThrottleConfiguration) OMElement(org.apache.axiom.om.OMElement) Throttle(org.apache.synapse.commons.throttle.core.Throttle)

Aggregations

CallerConfiguration (org.apache.synapse.commons.throttle.core.CallerConfiguration)2 ThrottleConfiguration (org.apache.synapse.commons.throttle.core.ThrottleConfiguration)2 ThrottleContext (org.apache.synapse.commons.throttle.core.ThrottleContext)2 OMElement (org.apache.axiom.om.OMElement)1 ConfigurationContext (org.apache.axis2.context.ConfigurationContext)1 AxisConfiguration (org.apache.axis2.engine.AxisConfiguration)1 MessageContext (org.apache.synapse.MessageContext)1 Throttle (org.apache.synapse.commons.throttle.core.Throttle)1 ThrottleException (org.apache.synapse.commons.throttle.core.ThrottleException)1 RoleBaseThrottleContext (org.apache.synapse.commons.throttle.core.impl.rolebase.RoleBaseThrottleContext)1 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)1 Before (org.junit.Before)1 AuthenticationContext (org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext)1 VerbInfoDTO (org.wso2.carbon.apimgt.impl.dto.VerbInfoDTO)1 Timer (org.wso2.carbon.metrics.manager.Timer)1