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

netflix.ocelli.functions.Limiters Maven / Gradle / Ivy

There is a newer version: 0.1.0-rc.2
Show newest version
package netflix.ocelli.functions;

import netflix.ocelli.stats.ExponentialAverage;
import rx.functions.Func1;

public abstract class Limiters {
    /**
     * Guard against excessive backup requests using exponential moving average
     * on a per sample basis which is incremented by 1 for each request and by 0 
     * for each backup request.  A backup request is allowed as long as the average
     * is able the expected percentile of primary requests to backup requests.
     * 
     * Note that this implementation favors simplicity over accuracy and has 
     * many drawbacks.
     * 1.  Backup requests are per request and not tied to any time window
     * 2.  I have yet to determine an equation that selects the proper window
     *     for the requested ratio so that the updated exponential moving average
     *     allows the ratio number of requests.
     * @param ratio
     * @param window
     * @return
     */
    public static Func1 exponential(final double ratio, final int window) {
        return new Func1() {
            private ExponentialAverage exp = new ExponentialAverage(window, 0);
            @Override
            public Boolean call(Boolean isPrimary) {
                if (isPrimary) {
                    exp.add(1L);
                    return true;
                }
                if (exp.get() > ratio) {
                    exp.add(0L);
                    return true;
                }
                return false;
            }
        };
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy