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

org.asteriskjava.pbx.agi.RateLimiter Maven / Gradle / Ivy

There is a newer version: 3.41.0
Show newest version
package org.asteriskjava.pbx.agi;

import java.util.LinkedList;
import java.util.List;

public class RateLimiter
{
    private static final long ONE_THOUSAND_MILLIS = 1000L;
    List available = new LinkedList<>();

    /**
     * this is NOT thread safe!
     * 
     * @param perSecond - number of 'acquire()' calls allowed per second, a call
     *            to acquire() will block(sleep) if the per second limit is
     *            exceeded
     */
    RateLimiter(int perSecond)
    {
        long now = System.currentTimeMillis();
        for (int i = 0; i < perSecond; i++)
        {
            available.add(now - ONE_THOUSAND_MILLIS);
        }
    }

    void acquire() throws InterruptedException
    {
        long now = System.currentTimeMillis();
        Long next = available.remove(0);
        long timeRemaining = next - now;
        if (timeRemaining > 0)
        {
            Thread.sleep(timeRemaining);
        }
        available.add(System.currentTimeMillis() + ONE_THOUSAND_MILLIS);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy