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);
}
}
}
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);
}
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");
}
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;
}
Aggregations