Search in sources :

Example 6 with Throttle

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

the class APIThrottleHandler method initThrottle.

private void initThrottle(MessageContext synCtx, ConfigurationContext cc) {
    if (policyKey == null) {
        throw new SynapseException("Throttle policy unspecified for the API");
    }
    Entry entry = synCtx.getConfiguration().getEntryDefinition(policyKey);
    if (entry == null) {
        handleException("Cannot find throttling policy using key: " + policyKey);
    }
    Object entryValue = null;
    boolean reCreate = false;
    if (entry.isDynamic()) {
        if ((!entry.isCached()) || (entry.isExpired()) || throttle == null) {
            entryValue = synCtx.getEntry(this.policyKey);
            if (this.version != entry.getVersion()) {
                // if there is a change, it will recreate
                reCreate = true;
            }
        }
    } else if (this.throttle == null) {
        entryValue = synCtx.getEntry(this.policyKey);
    }
    if (reCreate || throttle == null) {
        if (entryValue == null || !(entryValue instanceof OMElement)) {
            handleException("Unable to load throttling policy using key: " + policyKey);
        }
        version = entry.getVersion();
        // is not null and throttle is not null , then must reload.
        if (isClusteringEnable && concurrentAccessController != null && throttle != null) {
            // set null ,
            concurrentAccessController = null;
        // because need to reload
        }
        try {
            // Creates the throttle from the policy
            synchronized (this) {
                if (throttle == null || reCreate) {
                    OMElement policyOMElement = (OMElement) entryValue;
                    throttle = ThrottleFactory.createMediatorThrottle(PolicyEngine.getPolicy(policyOMElement));
                    // load the resource level tiers
                    Object resEntryValue = synCtx.getEntry(this.policyKeyResource);
                    if (resEntryValue == null || !(resEntryValue instanceof OMElement)) {
                        handleException("Unable to load throttling policy using key: " + this.policyKeyResource);
                    }
                    // create throttle for the resource level
                    Throttle resThrottle = ThrottleFactory.createMediatorThrottle(PolicyEngine.getPolicy((OMElement) resEntryValue));
                    // get the throttle Context for the resource level
                    ThrottleContext throttleContext = resThrottle.getThrottleContext(ThrottleConstants.ROLE_BASED_THROTTLE_KEY);
                    if (throttleContext != null) {
                        ThrottleConfiguration throttleConfiguration = throttleContext.getThrottleConfiguration();
                        ThrottleContext resourceContext = createThrottleContext(throttleConfiguration);
                        throttle.addThrottleContext(RESOURCE_THROTTLE_KEY, resourceContext);
                    }
                    OMElement hardThrottlingPolicy = createHardThrottlingPolicy();
                    if (hardThrottlingPolicy != null) {
                        Throttle tempThrottle = ThrottleFactory.createMediatorThrottle(PolicyEngine.getPolicy(hardThrottlingPolicy));
                        ThrottleConfiguration newThrottleConfig = tempThrottle.getThrottleConfiguration(ThrottleConstants.ROLE_BASED_THROTTLE_KEY);
                        ThrottleContext hardThrottling = createThrottleContext(newThrottleConfig);
                        throttle.addThrottleContext(APIThrottleConstants.HARD_THROTTLING_CONFIGURATION, hardThrottling);
                    }
                    // We check to what tiers allows to continue on quota reached.
                    OMElement assertionElement = policyOMElement.getFirstChildWithName(APIConstants.ASSERTION_ELEMENT);
                    Iterator tierElementIterator = assertionElement.getChildrenWithName(APIConstants.POLICY_ELEMENT);
                    if (continueOnLimitReachedMap == null) {
                        continueOnLimitReachedMap = new HashMap<String, Boolean>();
                    } else if (!continueOnLimitReachedMap.isEmpty()) {
                        continueOnLimitReachedMap.clear();
                    }
                    while (tierElementIterator.hasNext()) {
                        OMElement tierElement = (OMElement) tierElementIterator.next();
                        String tierName = tierElement.getFirstChildWithName(APIConstants.THROTTLE_ID_ELEMENT).getText();
                        try {
                            Map<String, Object> tierAttributes = APIDescriptionGenUtil.getTierAttributes(tierElement);
                            for (Map.Entry<String, Object> tierEntry : tierAttributes.entrySet()) {
                                if (APIConstants.THROTTLE_TIER_QUOTA_ACTION_ATTRIBUTE.equalsIgnoreCase(tierEntry.getKey())) {
                                    // We are putting the inverse value of the attribute to the map.
                                    // The reason is that we have the value whether to stop when quota reached.
                                    // The map contains the inverse of this, whether to continue when quota reached.
                                    continueOnLimitReachedMap.put(tierName, !Boolean.parseBoolean((String) tierEntry.getValue()));
                                    break;
                                }
                            }
                        } catch (APIManagementException e) {
                            // We do not throw the exception. If there is any exception, then the others can
                            // still function without any issue.
                            log.warn("Unable to get the action for quota reached of tier : " + tierName);
                        }
                    }
                }
            }
            // then must re-initiates
            if (throttle != null && (concurrentAccessController == null || !isClusteringEnable)) {
                concurrentAccessController = throttle.getConcurrentAccessController();
                if (concurrentAccessController != null) {
                    cc.setProperty(key, concurrentAccessController);
                } else {
                    cc.removeProperty(key);
                }
            }
        } catch (ThrottleException e) {
            handleException("Error processing the throttling policy", e);
        }
    }
}
Also used : ThrottleContext(org.apache.synapse.commons.throttle.core.ThrottleContext) SynapseException(org.apache.synapse.SynapseException) ThrottleConfiguration(org.apache.synapse.commons.throttle.core.ThrottleConfiguration) OMElement(org.apache.axiom.om.OMElement) Throttle(org.apache.synapse.commons.throttle.core.Throttle) Entry(org.apache.synapse.config.Entry) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ThrottleException(org.apache.synapse.commons.throttle.core.ThrottleException) Iterator(java.util.Iterator) Map(java.util.Map) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap)

Example 7 with Throttle

use of org.apache.synapse.commons.throttle.core.Throttle in project wso2-synapse by wso2.

the class ThrottleRuntimeTest method testThrottleRuntimeOnlyRole.

public void testThrottleRuntimeOnlyRole() throws Exception {
    StatCollector.enableStatCollection = true;
    TestThrottleHandler throttleHandler = new TestThrottleHandler();
    Throttle throttle = ThrottleTestFactory.getThrottle(ThrottleTestFactory.roleBaseOnlyPolicy);
    // Throttle throttle = ThrottleTestFactory.getThrottle(ThrottleTestFactory.roleBaseOnlyPolicy_2);
    // Throttle throttle = ThrottleTestFactory.getThrottle(ThrottleTestFactory.roleBasePolicy_WithIP);
    // Throttle throttle = ThrottleTestFactory.getThrottle(ThrottleTestFactory.roleBasePolicy_WithGlobalCLimit);
    // Throttle throttle = ThrottleTestFactory.getThrottle(ThrottleTestFactory.modulePolicy);
    // create throttle simulator
    ThrottleRequestSimulator simulator = new ThrottleRequestSimulator(throttleHandler, throttle);
    // schedule job
    final ScheduledFuture<?> scheduleHandle = scheduler.scheduleAtFixedRate(simulator, INIT_DELAY_IN_MILLI_SECS, PERIOD_IN_MILLI_SECS, TimeUnit.MILLISECONDS);
    scheduler.schedule(new Runnable() {

        public void run() {
            scheduleHandle.cancel(true);
            StatCollector.displayStats(ThrottleConstants.ROLE_BASE);
            StatCollector.displayStats(ThrottleConstants.IP_BASE);
            StatCollector.flushStats();
            scheduler.shutdown();
        }
    }, TOTAL_DURATION_IN_SECS, TimeUnit.SECONDS);
}
Also used : Throttle(org.apache.synapse.commons.throttle.core.Throttle)

Example 8 with Throttle

use of org.apache.synapse.commons.throttle.core.Throttle in project wso2-synapse by wso2.

the class ThrottleTestFactory method main.

public static void main(String[] args) throws Exception {
    Throttle throttle = ThrottleTestFactory.getThrottle(roleBaseOnlyPolicy);
    System.out.println(roleBaseOnlyPolicy);
    System.out.println("ok");
}
Also used : Throttle(org.apache.synapse.commons.throttle.core.Throttle)

Example 9 with Throttle

use of org.apache.synapse.commons.throttle.core.Throttle in project wso2-synapse by wso2.

the class ThrottleTestFactory method getThrottle.

public static Throttle getThrottle(String policyStr) throws Exception {
    OMElement policyOM = createOMElement(policyStr);
    Policy policy = PolicyEngine.getPolicy(policyOM);
    Throttle throttle = ThrottleFactory.createModuleThrottle(policy);
    return throttle;
}
Also used : Policy(org.apache.neethi.Policy) OMElement(org.apache.axiom.om.OMElement) Throttle(org.apache.synapse.commons.throttle.core.Throttle)

Aggregations

Throttle (org.apache.synapse.commons.throttle.core.Throttle)9 ThrottleException (org.apache.synapse.commons.throttle.core.ThrottleException)6 OMElement (org.apache.axiom.om.OMElement)4 ThrottleContext (org.apache.synapse.commons.throttle.core.ThrottleContext)4 Map (java.util.Map)3 ThrottleConfiguration (org.apache.synapse.commons.throttle.core.ThrottleConfiguration)3 HashMap (java.util.HashMap)2 QName (javax.xml.namespace.QName)2 Policy (org.apache.neethi.Policy)2 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 List (java.util.List)1 TreeMap (java.util.TreeMap)1 ConfigurationContext (org.apache.axis2.context.ConfigurationContext)1 AxisOperation (org.apache.axis2.description.AxisOperation)1 AxisService (org.apache.axis2.description.AxisService)1 AxisConfiguration (org.apache.axis2.engine.AxisConfiguration)1 SynapseException (org.apache.synapse.SynapseException)1 CallerConfiguration (org.apache.synapse.commons.throttle.core.CallerConfiguration)1 ConcurrentAccessController (org.apache.synapse.commons.throttle.core.ConcurrentAccessController)1