Search in sources :

Example 1 with RateLimiterBucket

use of io.apiman.gateway.engine.rates.RateLimiterBucket 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 RateLimiterBucket

use of io.apiman.gateway.engine.rates.RateLimiterBucket 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 RateLimiterBucket

use of io.apiman.gateway.engine.rates.RateLimiterBucket in project apiman by apiman.

the class SingleNodeRateLimiterComponent method saveBuckets.

/**
 * Saves the current list of buckets into a file.
 */
private void saveBuckets() {
    if (lastModifiedOn > lastSavedOn) {
        // $NON-NLS-1$
        LOGGER.info("Persisting current rates to: {0}", savedRates);
        Properties props = new Properties();
        for (Entry<String, RateLimiterBucket> entry : buckets.entrySet()) {
            // $NON-NLS-1$
            String value = entry.getValue().getCount() + "|" + entry.getValue().getLast();
            props.setProperty(entry.getKey(), value);
        }
        try (FileWriter writer = new FileWriter(savedRates)) {
            // $NON-NLS-1$
            props.store(writer, "All Apiman rate limits");
        } catch (IOException e) {
            e.printStackTrace();
        }
        lastSavedOn = System.currentTimeMillis();
    }
}
Also used : FileWriter(java.io.FileWriter) RateLimiterBucket(io.apiman.gateway.engine.rates.RateLimiterBucket) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) Properties(java.util.Properties)

Example 4 with RateLimiterBucket

use of io.apiman.gateway.engine.rates.RateLimiterBucket in project apiman by apiman.

the class SingleNodeRateLimiterComponent method loadBuckets.

/**
 * Loads the saved rates from a file.  Done only on startup.
 */
private void loadBuckets() {
    Properties props = new Properties();
    try (FileReader reader = new FileReader(savedRates)) {
        props.load(reader);
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
    for (Entry<Object, Object> entry : props.entrySet()) {
        String key = entry.getKey().toString();
        String value = entry.getValue().toString();
        // $NON-NLS-1$
        String[] split = value.split("\\|");
        long count = Long.parseLong(split[0]);
        long last = Long.parseLong(split[1]);
        RateLimiterBucket bucket = new RateLimiterBucket();
        bucket.setCount(count);
        bucket.setLast(last);
        buckets.put(key, bucket);
    }
}
Also used : RateLimiterBucket(io.apiman.gateway.engine.rates.RateLimiterBucket) FileReader(java.io.FileReader) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) Properties(java.util.Properties)

Example 5 with RateLimiterBucket

use of io.apiman.gateway.engine.rates.RateLimiterBucket 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

RateLimiterBucket (io.apiman.gateway.engine.rates.RateLimiterBucket)6 RateLimitResponse (io.apiman.gateway.engine.components.rate.RateLimitResponse)4 IOException (java.io.IOException)2 UncheckedIOException (java.io.UncheckedIOException)2 Properties (java.util.Properties)2 FileReader (java.io.FileReader)1 FileWriter (java.io.FileWriter)1 GetRequest (org.elasticsearch.action.get.GetRequest)1 GetResponse (org.elasticsearch.action.get.GetResponse)1