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