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

com.javanut.pronghorn.pipe.PipeRegulator Maven / Gradle / Ivy

Go to download

Ring buffer based queuing utility for applications that require high performance and/or a small footprint. Well suited for embedded and stream based processing.

There is a newer version: 1.1.27
Show newest version
package com.javanut.pronghorn.pipe;

public class PipeRegulator {
    
    private long regulatorTimeBaseNS = System.currentTimeMillis()*MS_TO_NS;
    private long regulatorPositionBase = 0;
    private long divisorSizeNS;
    private static final long MS_TO_NS = 1_000_000L;
    
    /**
     * Helper method so the scheduler can get the number of nanos that this stage should wait before reschedule.
     * 
     * This supports both regulating the consumer/producer of data based on contract requirements of the connected software.
     * 
     * @param maxMsgPerMs
     */
    public PipeRegulator(int maxMsgPerMs, int avgMsgSize) {
        this.divisorSizeNS = MS_TO_NS*(long)avgMsgSize*(long)maxMsgPerMs;
    }    
    
    public static > long computeRateLimitDelay(Pipe pipe, long position, PipeRegulator regulator) {
        long expectedNow = regulator.regulatorTimeBaseNS + ((position-regulator.regulatorPositionBase)/regulator.divisorSizeNS);
        return expectedNow-(System.currentTimeMillis()*MS_TO_NS);
    }
    
}