All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.netflix.astyanax.connectionpool.impl.SimpleRateLimiterImpl Maven / Gradle / Ivy

There is a newer version: 3.10.2
Show newest version
package com.netflix.astyanax.connectionpool.impl;

import java.util.concurrent.LinkedBlockingDeque;

import com.netflix.astyanax.connectionpool.ConnectionPoolConfiguration;
import com.netflix.astyanax.connectionpool.RateLimiter;

public class SimpleRateLimiterImpl implements RateLimiter {
    private final LinkedBlockingDeque queue = new LinkedBlockingDeque();
    private final ConnectionPoolConfiguration config;

    public SimpleRateLimiterImpl(ConnectionPoolConfiguration config) {
        this.config = config;
    }

    @Override
    public boolean check() {
        return check(System.currentTimeMillis());
    }

    @Override
    public boolean check(long currentTimeMillis) {
        int maxCount = config.getConnectionLimiterMaxPendingCount();
        if (maxCount == 0)
            return true;

        // Haven't reached the count limit yet
        if (queue.size() < maxCount) {
            queue.addFirst(currentTimeMillis);
            return true;
        }
        else {
            long last = queue.getLast();
            if (currentTimeMillis - last < config.getConnectionLimiterWindowSize()) {
                return false;
            }
            queue.addFirst(currentTimeMillis);
            queue.removeLast();
            return true;
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy