Search in sources :

Example 1 with RateLimitResponse

use of io.apiman.gateway.engine.components.rate.RateLimitResponse in project apiman by apiman.

the class EsRateLimiterComponent method accept.

/**
 * @see io.apiman.gateway.engine.components.IRateLimiterComponent#accept(java.lang.String, io.apiman.gateway.engine.rates.RateBucketPeriod, long, long, io.apiman.gateway.engine.async.IAsyncResultHandler)
 */
@Override
public void accept(final String bucketId, final RateBucketPeriod period, final long limit, final long increment, final IAsyncResultHandler<RateLimitResponse> handler) {
    final String id = id(bucketId);
    try {
        GetResponse response = getClient().get(new GetRequest(getFullIndexName()).id(id), RequestOptions.DEFAULT);
        RateLimiterBucket bucket;
        long version;
        if (response.isExists()) {
            // use the existing bucket
            version = response.getVersion();
            String sourceAsString = response.getSourceAsString();
            bucket = JSON_MAPPER.readValue(sourceAsString, RateLimiterBucket.class);
        } else {
            // make a new bucket
            version = 0;
            bucket = new RateLimiterBucket();
        }
        bucket.resetIfNecessary(period);
        final RateLimitResponse rlr = new RateLimitResponse();
        if (bucket.getCount() > limit) {
            rlr.setAccepted(false);
        } else {
            rlr.setAccepted(bucket.getCount() < limit);
            bucket.setCount(bucket.getCount() + increment);
            bucket.setLast(System.currentTimeMillis());
        }
        int reset = (int) (bucket.getResetMillis(period) / 1000L);
        rlr.setReset(reset);
        rlr.setRemaining(limit - bucket.getCount());
        updateBucketAndReturn(id, bucket, rlr, version, bucketId, period, limit, increment, handler);
    } catch (Throwable e) {
        handler.handle(AsyncResultImpl.create(e, RateLimitResponse.class));
    }
}
Also used : GetRequest(org.elasticsearch.action.get.GetRequest) RateLimiterBucket(io.apiman.gateway.engine.rates.RateLimiterBucket) RateLimitResponse(io.apiman.gateway.engine.components.rate.RateLimitResponse) GetResponse(org.elasticsearch.action.get.GetResponse)

Example 2 with RateLimitResponse

use of io.apiman.gateway.engine.components.rate.RateLimitResponse in project apiman by apiman.

the class SingleNodeRateLimiterComponent method accept.

/**
 * @see io.apiman.gateway.engine.components.IRateLimiterComponent#accept(java.lang.String, io.apiman.gateway.engine.rates.RateBucketPeriod, long, long, io.apiman.gateway.engine.async.IAsyncResultHandler)
 */
@Override
public void accept(String bucketId, RateBucketPeriod period, long limit, long increment, IAsyncResultHandler<RateLimitResponse> handler) {
    RateLimiterBucket bucket;
    RateLimitResponse response = new RateLimitResponse();
    synchronized (buckets) {
        bucket = buckets.get(bucketId);
        if (bucket == null) {
            bucket = new RateLimiterBucket();
            buckets.put(bucketId, bucket);
        }
        boolean rateModified = bucket.resetIfNecessary(period);
        if (bucket.getCount() > limit) {
            response.setAccepted(false);
        } else {
            response.setAccepted(bucket.getCount() < limit);
            bucket.setCount(bucket.getCount() + increment);
            bucket.setLast(System.currentTimeMillis());
            rateModified = true;
        }
        int reset = (int) (bucket.getResetMillis(period) / 1000L);
        response.setReset(reset);
        if (rateModified) {
            lastModifiedOn = System.currentTimeMillis();
        }
    }
    response.setRemaining(limit - bucket.getCount());
    handler.handle(AsyncResultImpl.create(response));
}
Also used : RateLimiterBucket(io.apiman.gateway.engine.rates.RateLimiterBucket) RateLimitResponse(io.apiman.gateway.engine.components.rate.RateLimitResponse)

Example 3 with RateLimitResponse

use of io.apiman.gateway.engine.components.rate.RateLimitResponse in project apiman by apiman.

the class RateLimitingPolicy method doApply.

/**
 * @see io.apiman.gateway.engine.policies.AbstractMappedPolicy#doApply(io.apiman.gateway.engine.beans.ApiRequest, io.apiman.gateway.engine.policy.IPolicyContext, java.lang.Object, io.apiman.gateway.engine.policy.IPolicyChain)
 */
@Override
protected void doApply(final ApiRequest request, final IPolicyContext context, final RateLimitingConfig config, final IPolicyChain<ApiRequest> chain) {
    String bucketId = bucketId(request, config);
    final RateBucketPeriod period = bucketFactory.getPeriod(config);
    if (bucketId.equals(BucketFactory.NO_USER_AVAILABLE)) {
        IPolicyFailureFactoryComponent failureFactory = context.getComponent(IPolicyFailureFactoryComponent.class);
        // $NON-NLS-1$
        PolicyFailure failure = failureFactory.createFailure(PolicyFailureType.Other, PolicyFailureCodes.NO_USER_FOR_RATE_LIMITING, Messages.i18n.format("RateLimitingPolicy.NoUser"));
        chain.doFailure(failure);
        return;
    }
    if (bucketId.equals(BucketFactory.NO_CLIENT_AVAILABLE)) {
        IPolicyFailureFactoryComponent failureFactory = context.getComponent(IPolicyFailureFactoryComponent.class);
        // $NON-NLS-1$
        PolicyFailure failure = failureFactory.createFailure(PolicyFailureType.Other, PolicyFailureCodes.NO_APP_FOR_RATE_LIMITING, Messages.i18n.format("RateLimitingPolicy.NoApp"));
        chain.doFailure(failure);
        return;
    }
    IRateLimiterComponent rateLimiter = context.getComponent(IRateLimiterComponent.class);
    rateLimiter.accept(bucketId, period, config.getLimit(), 1, new IAsyncResultHandler<RateLimitResponse>() {

        @Override
        public void handle(IAsyncResult<RateLimitResponse> result) {
            if (result.isError()) {
                chain.throwError(result.getError());
            } else {
                RateLimitResponse rtr = result.getResult();
                Map<String, String> responseHeaders = responseHeaders(config, rtr, defaultLimitHeader(), defaultRemainingHeader(), defaultResetHeader());
                if (rtr.isAccepted()) {
                    // $NON-NLS-1$
                    context.setAttribute("rate-limit-response-headers", responseHeaders);
                    chain.doApply(request);
                } else {
                    IPolicyFailureFactoryComponent failureFactory = context.getComponent(IPolicyFailureFactoryComponent.class);
                    PolicyFailure failure = limitExceededFailure(failureFactory);
                    failure.getHeaders().putAll(responseHeaders);
                    chain.doFailure(failure);
                }
            }
        }
    });
}
Also used : IRateLimiterComponent(io.apiman.gateway.engine.components.IRateLimiterComponent) PolicyFailure(io.apiman.gateway.engine.beans.PolicyFailure) RateBucketPeriod(io.apiman.gateway.engine.rates.RateBucketPeriod) RateLimitResponse(io.apiman.gateway.engine.components.rate.RateLimitResponse) IPolicyFailureFactoryComponent(io.apiman.gateway.engine.components.IPolicyFailureFactoryComponent) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with RateLimitResponse

use of io.apiman.gateway.engine.components.rate.RateLimitResponse in project apiman by apiman.

the class TransferQuotaPolicy method doApply.

@Override
protected void doApply(final ApiRequest request, final IPolicyContext context, final TransferQuotaConfig config, final IPolicyChain<ApiRequest> chain) {
    // *************************************************************
    // Step 1:  check to see if we're already in violation of this
    // policy.  If so, fail fast.
    // *************************************************************
    // $NON-NLS-1$
    String bucketId = bucketId(request, config);
    final RateBucketPeriod period = bucketFactory.getPeriod(config);
    if (bucketId.equals(BucketFactory.NO_USER_AVAILABLE)) {
        IPolicyFailureFactoryComponent failureFactory = context.getComponent(IPolicyFailureFactoryComponent.class);
        // $NON-NLS-1$
        PolicyFailure failure = failureFactory.createFailure(PolicyFailureType.Other, PolicyFailureCodes.NO_USER_FOR_RATE_LIMITING, Messages.i18n.format("TransferQuotaPolicy.NoUser"));
        chain.doFailure(failure);
        return;
    }
    if (bucketId.equals(BucketFactory.NO_CLIENT_AVAILABLE)) {
        IPolicyFailureFactoryComponent failureFactory = context.getComponent(IPolicyFailureFactoryComponent.class);
        // $NON-NLS-1$
        PolicyFailure failure = failureFactory.createFailure(PolicyFailureType.Other, PolicyFailureCodes.NO_APP_FOR_RATE_LIMITING, Messages.i18n.format("TransferQuotaPolicy.NoApp"));
        chain.doFailure(failure);
        return;
    }
    context.setAttribute(BUCKET_ID_ATTR, bucketId);
    context.setAttribute(PERIOD_ATTR, period);
    IRateLimiterComponent rateLimiter = context.getComponent(IRateLimiterComponent.class);
    rateLimiter.accept(bucketId, period, config.getLimit(), 0, new IAsyncResultHandler<RateLimitResponse>() {

        @Override
        public void handle(IAsyncResult<RateLimitResponse> result) {
            if (result.isError()) {
                chain.throwError(result.getError());
            } else {
                RateLimitResponse rtr = result.getResult();
                context.setAttribute(RATE_LIMIT_RESPONSE_ATTR, rtr);
                if (!rtr.isAccepted()) {
                    doQuotaExceededFailure(context, config, chain, rtr);
                } else {
                    chain.doApply(request);
                }
            }
        }
    });
}
Also used : IRateLimiterComponent(io.apiman.gateway.engine.components.IRateLimiterComponent) PolicyFailure(io.apiman.gateway.engine.beans.PolicyFailure) RateBucketPeriod(io.apiman.gateway.engine.rates.RateBucketPeriod) RateLimitResponse(io.apiman.gateway.engine.components.rate.RateLimitResponse) IPolicyFailureFactoryComponent(io.apiman.gateway.engine.components.IPolicyFailureFactoryComponent)

Example 5 with RateLimitResponse

use of io.apiman.gateway.engine.components.rate.RateLimitResponse in project apiman by apiman.

the class InMemoryRateLimiterComponent method accept.

/**
 * @see io.apiman.gateway.engine.components.IRateLimiterComponent#accept(java.lang.String, io.apiman.gateway.engine.rates.RateBucketPeriod, long, long, io.apiman.gateway.engine.async.IAsyncResultHandler)
 */
@Override
public void accept(String bucketId, RateBucketPeriod period, long limit, long increment, IAsyncResultHandler<RateLimitResponse> handler) {
    RateLimiterBucket bucket;
    RateLimitResponse response = new RateLimitResponse();
    synchronized (buckets) {
        bucket = buckets.get(bucketId);
        if (bucket == null) {
            bucket = new RateLimiterBucket();
            buckets.put(bucketId, bucket);
        }
        bucket.resetIfNecessary(period);
        if (bucket.getCount() > limit) {
            response.setAccepted(false);
        } else {
            response.setAccepted(bucket.getCount() < limit);
            bucket.setCount(bucket.getCount() + increment);
            bucket.setLast(System.currentTimeMillis());
        }
        int reset = (int) (bucket.getResetMillis(period) / 1000L);
        response.setReset(reset);
    }
    response.setRemaining(limit - bucket.getCount());
    handler.handle(AsyncResultImpl.create(response));
}
Also used : RateLimiterBucket(io.apiman.gateway.engine.rates.RateLimiterBucket) RateLimitResponse(io.apiman.gateway.engine.components.rate.RateLimitResponse)

Aggregations

RateLimitResponse (io.apiman.gateway.engine.components.rate.RateLimitResponse)11 IRateLimiterComponent (io.apiman.gateway.engine.components.IRateLimiterComponent)6 RateBucketPeriod (io.apiman.gateway.engine.rates.RateBucketPeriod)4 RateLimiterBucket (io.apiman.gateway.engine.rates.RateLimiterBucket)4 PolicyFailure (io.apiman.gateway.engine.beans.PolicyFailure)2 IPolicyFailureFactoryComponent (io.apiman.gateway.engine.components.IPolicyFailureFactoryComponent)2 Map (java.util.Map)2 RateLimitingProbeResponse (io.apiman.gateway.engine.policies.probe.RateLimitingProbeResponse)1 TransferQuotaProbeResponse (io.apiman.gateway.engine.policies.probe.TransferQuotaProbeResponse)1 HashMap (java.util.HashMap)1 GetRequest (org.elasticsearch.action.get.GetRequest)1 GetResponse (org.elasticsearch.action.get.GetResponse)1 IndexRequest (org.elasticsearch.action.index.IndexRequest)1 IndexResponse (org.elasticsearch.action.index.IndexResponse)1