Search in sources :

Example 1 with ThrottleInfo

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;
}
Also used : SseResponseEventDataProvider(org.wso2.carbon.apimgt.gateway.handlers.streaming.sse.analytics.SseResponseEventDataProvider) AuthenticationContext(org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext) ThrottleInfo(org.wso2.carbon.apimgt.gateway.handlers.streaming.sse.throttling.ThrottleInfo) AnalyticsDataProvider(org.wso2.carbon.apimgt.common.analytics.collectors.AnalyticsDataProvider)

Example 2 with ThrottleInfo

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");
    }
}
Also used : ThrottleDataPublisher(org.wso2.carbon.apimgt.gateway.throttling.publisher.ThrottleDataPublisher) ThrottleDataPublisher(org.wso2.carbon.apimgt.gateway.throttling.publisher.ThrottleDataPublisher) DataPublisher(org.wso2.carbon.databridge.agent.DataPublisher) JSONObject(org.json.JSONObject)

Example 3 with ThrottleInfo

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;
}
Also used : ThrottleInfo(org.wso2.carbon.apimgt.gateway.handlers.streaming.sse.throttling.ThrottleInfo) VerbInfoDTO(org.wso2.carbon.apimgt.impl.dto.VerbInfoDTO) List(java.util.List) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 4 with 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;
}
Also used : ThrottleInfo(org.wso2.carbon.apimgt.gateway.handlers.streaming.sse.throttling.ThrottleInfo) JSONObject(org.json.JSONObject) AnalyticsException(org.wso2.carbon.apimgt.common.analytics.exceptions.AnalyticsException) JSONObject(org.json.JSONObject)

Aggregations

ThrottleInfo (org.wso2.carbon.apimgt.gateway.handlers.streaming.sse.throttling.ThrottleInfo)3 JSONObject (org.json.JSONObject)2 List (java.util.List)1 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)1 AnalyticsDataProvider (org.wso2.carbon.apimgt.common.analytics.collectors.AnalyticsDataProvider)1 AnalyticsException (org.wso2.carbon.apimgt.common.analytics.exceptions.AnalyticsException)1 AuthenticationContext (org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext)1 SseResponseEventDataProvider (org.wso2.carbon.apimgt.gateway.handlers.streaming.sse.analytics.SseResponseEventDataProvider)1 ThrottleDataPublisher (org.wso2.carbon.apimgt.gateway.throttling.publisher.ThrottleDataPublisher)1 VerbInfoDTO (org.wso2.carbon.apimgt.impl.dto.VerbInfoDTO)1 DataPublisher (org.wso2.carbon.databridge.agent.DataPublisher)1