use of org.wso2.carbon.apimgt.gateway.handlers.streaming.sse.throttling.ThrottleInfo in project carbon-apimgt by wso2.
the class SseApiHandler method isThrottled.
private boolean isThrottled(org.apache.axis2.context.MessageContext axisCtx, MessageContext synCtx) {
AuthenticationContext authenticationContext = APISecurityUtils.getAuthenticationContext(synCtx);
ThrottleInfo throttleInfo = getThrottlingInfo(authenticationContext, synCtx);
boolean isThrottled = SseUtils.isRequestBlocked(authenticationContext, throttleInfo.getApiContext(), throttleInfo.getApiVersion(), authenticationContext.getUsername(), throttleInfo.getRemoteIp(), CarbonContext.getThreadLocalCarbonContext().getTenantDomain());
if (!isThrottled) {
// do throttling if request is not blocked by global conditions only
isThrottled = SseUtils.isThrottled(throttleInfo.getSubscriberTenantDomain(), throttleInfo.getResourceLevelThrottleKey(), throttleInfo.getSubscriptionLevelThrottleKey(), throttleInfo.getApplicationLevelThrottleKey());
}
if (isThrottled) {
handleThrottledOut(synCtx);
return true;
}
if (APIUtil.isAnalyticsEnabled()) {
AnalyticsDataProvider provider = new SseResponseEventDataProvider(synCtx);
axisCtx.setProperty(SSE_ANALYTICS_INFO, provider);
}
return false;
}
use of org.wso2.carbon.apimgt.gateway.handlers.streaming.sse.throttling.ThrottleInfo in project carbon-apimgt by wso2.
the class SseUtils method publishNonThrottledEvent.
public static void publishNonThrottledEvent(int eventCount, String messageId, ThrottleInfo throttleInfo, JSONObject properties) {
Object[] objects = new Object[] { messageId, throttleInfo.getApplicationLevelThrottleKey(), throttleInfo.getApplicationTier(), throttleInfo.getApiLevelThrottleKey(), throttleInfo.getApiTier(), throttleInfo.getSubscriptionLevelThrottleKey(), throttleInfo.getTier(), throttleInfo.getResourceLevelThrottleKey(), throttleInfo.getResourceTier(), throttleInfo.getAuthorizedUser(), throttleInfo.getApiContext(), throttleInfo.getApiVersion(), throttleInfo.getSubscriberTenantDomain(), throttleInfo.getSubscriberTenantDomain(), throttleInfo.getApplicationId(), throttleInfo.getApiName(), properties.toString() };
org.wso2.carbon.databridge.commons.Event event = new org.wso2.carbon.databridge.commons.Event(THROTTLE_STREAM_ID, System.currentTimeMillis(), null, null, objects);
ThrottleDataPublisher throttleDataPublisher = ServiceReferenceHolder.getInstance().getThrottleDataPublisher();
if (throttleDataPublisher != null) {
int count = 1;
DataPublisher publisher = ThrottleDataPublisher.getDataPublisher();
while (count <= eventCount) {
publisher.tryPublish(event);
count++;
}
} else {
log.error("Cannot publish events to traffic manager because ThrottleDataPublisher " + "has not been initialised");
}
}
use of org.wso2.carbon.apimgt.gateway.handlers.streaming.sse.throttling.ThrottleInfo in project carbon-apimgt by wso2.
the class SseApiHandler method getThrottlingInfo.
private ThrottleInfo getThrottlingInfo(AuthenticationContext authenticationContext, MessageContext synCtx) {
org.apache.axis2.context.MessageContext axis2MC = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
String apiContext = (String) synCtx.getProperty(RESTConstants.REST_API_CONTEXT);
String apiVersion = (String) synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION);
List<VerbInfoDTO> verbInfoList = (List<VerbInfoDTO>) synCtx.getProperty(APIConstants.VERB_INFO_DTO);
String resourceLevelThrottleKey = null;
String resourceLevelTier = null;
if (verbInfoList != null) {
// for sse, there will be only one verb info list
VerbInfoDTO verbInfoDTO = verbInfoList.get(0);
resourceLevelThrottleKey = verbInfoDTO.getRequestKey();
resourceLevelTier = verbInfoDTO.getThrottling();
}
String remoteIP = GatewayUtils.getIp(axis2MC);
ThrottleInfo throttleInfo = new ThrottleInfo(authenticationContext, apiContext, apiVersion, resourceLevelThrottleKey, resourceLevelTier, remoteIP);
axis2MC.setProperty(SSE_THROTTLE_DTO, throttleInfo);
return throttleInfo;
}
use of org.wso2.carbon.apimgt.gateway.handlers.streaming.sse.throttling.ThrottleInfo in project carbon-apimgt by wso2.
the class SseResponseStreamInterceptor method handleThrottlingAndAnalytics.
private boolean handleThrottlingAndAnalytics(int eventCount, MessageContext axi2Ctx) {
Object throttleObject = axi2Ctx.getProperty(SSE_THROTTLE_DTO);
if (throttleObject != null) {
String messageId = UIDGenerator.generateURNString();
ThrottleInfo throttleInfo = (ThrottleInfo) throttleObject;
String remoteIP = throttleInfo.getRemoteIp();
JSONObject propertiesMap = new JSONObject();
Utils.setRemoteIp(propertiesMap, remoteIP);
boolean isThrottled = isThrottled(throttleInfo.getSubscriberTenantDomain(), throttleInfo.getResourceLevelThrottleKey(), throttleInfo.getSubscriptionLevelThrottleKey(), throttleInfo.getApplicationLevelThrottleKey());
if (isThrottled) {
log.warn("Request is throttled out");
return false;
}
throttlePublisherService.execute(() -> SseUtils.publishNonThrottledEvent(eventCount, messageId, throttleInfo, propertiesMap));
if (APIUtil.isAnalyticsEnabled()) {
try {
publishAnalyticsData(eventCount, axi2Ctx);
} catch (AnalyticsException e) {
log.error("Error while publishing analytics data", e);
}
}
return true;
} else {
log.error("Throttle object cannot be null.");
}
return true;
}
Aggregations