Search in sources :

Example 6 with ThrottleDataPublisher

use of org.wso2.carbon.apimgt.gateway.throttling.publisher.ThrottleDataPublisher in project carbon-apimgt by wso2.

the class ServerStartupListener method completedServerStartup.

@Override
public void completedServerStartup() {
    // This prevents errors in an All in one setup caused by the ThrottleDataPublisher trying to connect to the
    // event receiver, before the event receiver has been started on completion of server startup.
    ServiceReferenceHolder.getInstance().setThrottleDataPublisher(new ThrottleDataPublisher());
    ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
    APIThrottleDataServiceImpl throttleDataServiceImpl = new APIThrottleDataServiceImpl(throttleDataHolder);
    CacheInvalidationService cacheInvalidationService = new CacheInvalidationServiceImpl();
    // Register APIThrottleDataService so that ThrottleData maps are available to other components.
    ServiceReferenceHolder.getInstance().setCacheInvalidationService(cacheInvalidationService);
    ServiceReferenceHolder.getInstance().setAPIThrottleDataService(throttleDataServiceImpl);
    ServiceReferenceHolder.getInstance().setThrottleDataHolder(throttleDataHolder);
    ServiceReferenceHolder.getInstance().setRevokedTokenService(new RevokedTokenDataImpl());
    SubscriptionsDataService subscriptionsDataService = new SubscriptionsDataServiceImpl();
    ServiceReferenceHolder.getInstance().setSubscriptionsDataService(subscriptionsDataService);
    log.debug("APIThrottleDataService Registered...");
}
Also used : CacheInvalidationServiceImpl(org.wso2.carbon.apimgt.impl.caching.CacheInvalidationServiceImpl) RevokedTokenDataImpl(org.wso2.carbon.apimgt.gateway.service.RevokedTokenDataImpl) ThrottleDataHolder(org.wso2.carbon.apimgt.gateway.throttling.ThrottleDataHolder) APIThrottleDataServiceImpl(org.wso2.carbon.apimgt.gateway.service.APIThrottleDataServiceImpl) ThrottleDataPublisher(org.wso2.carbon.apimgt.gateway.throttling.publisher.ThrottleDataPublisher) SubscriptionsDataService(org.wso2.carbon.apimgt.impl.webhooks.SubscriptionsDataService) SubscriptionsDataServiceImpl(org.wso2.carbon.apimgt.gateway.webhooks.SubscriptionsDataServiceImpl) CacheInvalidationService(org.wso2.carbon.apimgt.impl.caching.CacheInvalidationService)

Example 7 with ThrottleDataPublisher

use of org.wso2.carbon.apimgt.gateway.throttling.publisher.ThrottleDataPublisher in project carbon-apimgt by wso2.

the class ThrottleHandler method handleRequest.

/**
 * Handle incoming requests and call throttling method to perform throttling.
 *
 * @param messageContext message context object which contains message details.
 * @return return true if message flow need to continue and pass requests to next handler in chain. Else return
 * false to notify error with handler
 */
public boolean handleRequest(MessageContext messageContext) {
    if (Utils.isGraphQLSubscriptionRequest(messageContext)) {
        if (log.isDebugEnabled()) {
            log.debug("Skipping GraphQL subscription handshake request.");
        }
        return true;
    }
    if (ServiceReferenceHolder.getInstance().getThrottleDataPublisher() == null) {
        log.error("Cannot publish events to traffic manager because ThrottleDataPublisher " + "has not been initialised");
        return true;
    }
    Timer timer3 = getTimer(MetricManager.name(APIConstants.METRICS_PREFIX, this.getClass().getSimpleName(), THROTTLE_MAIN));
    Timer.Context context3 = timer3.start();
    TracingSpan throttleLatencySpan = null;
    if (Util.tracingEnabled()) {
        TracingSpan responseLatencySpan = (TracingSpan) messageContext.getProperty(APIMgtGatewayConstants.RESOURCE_SPAN);
        TracingTracer tracer = Util.getGlobalTracer();
        throttleLatencySpan = Util.startSpan(APIMgtGatewayConstants.THROTTLE_LATENCY, responseLatencySpan, tracer);
    }
    long executionStartTime = System.currentTimeMillis();
    if (!ExtensionListenerUtil.preProcessRequest(messageContext, type)) {
        return false;
    }
    try {
        boolean throttleResponse = doThrottle(messageContext);
        if (!ExtensionListenerUtil.postProcessRequest(messageContext, type)) {
            return false;
        }
        return throttleResponse;
    } catch (Exception e) {
        if (Util.tracingEnabled() && throttleLatencySpan != null) {
            Util.setTag(throttleLatencySpan, APIMgtGatewayConstants.ERROR, APIMgtGatewayConstants.THROTTLE_HANDLER_ERROR);
        }
        throw e;
    } finally {
        messageContext.setProperty(APIMgtGatewayConstants.THROTTLING_LATENCY, System.currentTimeMillis() - executionStartTime);
        context3.stop();
        if (Util.tracingEnabled()) {
            Util.finishSpan(throttleLatencySpan);
        }
    }
}
Also used : Timer(org.wso2.carbon.metrics.manager.Timer) TracingTracer(org.wso2.carbon.apimgt.tracing.TracingTracer) TracingSpan(org.wso2.carbon.apimgt.tracing.TracingSpan) XMLStreamException(javax.xml.stream.XMLStreamException) ThrottleException(org.apache.synapse.commons.throttle.core.ThrottleException)

Aggregations

ThrottleDataPublisher (org.wso2.carbon.apimgt.gateway.throttling.publisher.ThrottleDataPublisher)5 ThrottleDataHolder (org.wso2.carbon.apimgt.gateway.throttling.ThrottleDataHolder)3 MessageContext (org.apache.synapse.MessageContext)2 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)2 JSONObject (org.json.JSONObject)2 Test (org.junit.Test)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 AuthenticationContext (org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext)2 DataPublisher (org.wso2.carbon.databridge.agent.DataPublisher)2 ArrayList (java.util.ArrayList)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 ThrottleException (org.apache.synapse.commons.throttle.core.ThrottleException)1 SynapseEnvironment (org.apache.synapse.core.SynapseEnvironment)1 Before (org.junit.Before)1 ConditionGroupDTO (org.wso2.carbon.apimgt.api.dto.ConditionGroupDTO)1 ServiceReferenceHolder (org.wso2.carbon.apimgt.gateway.internal.ServiceReferenceHolder)1 APIThrottleDataServiceImpl (org.wso2.carbon.apimgt.gateway.service.APIThrottleDataServiceImpl)1 RevokedTokenDataImpl (org.wso2.carbon.apimgt.gateway.service.RevokedTokenDataImpl)1 SubscriptionsDataServiceImpl (org.wso2.carbon.apimgt.gateway.webhooks.SubscriptionsDataServiceImpl)1 APIManagerConfiguration (org.wso2.carbon.apimgt.impl.APIManagerConfiguration)1