Skip to content

Commit

Permalink
Rate limit
Browse files Browse the repository at this point in the history
  • Loading branch information
fireduck64 committed Sep 24, 2020
1 parent 3872100 commit 538ced2
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/RateLimit.java
@@ -0,0 +1,57 @@
package duckutil;

public class RateLimit
{

private double max_bytes_per_ns;
private double last_allocated;
private double buffer_seconds;

public RateLimit(double maxBytesPerSecond, double buffer_seconds)
{
max_bytes_per_ns = maxBytesPerSecond / 1e9;
this.buffer_seconds = buffer_seconds;

resetLast();
}

private void resetLast()
{
last_allocated = Math.max(
System.nanoTime() - (buffer_seconds * 1e9),
last_allocated);
}

/**
* Returns true if a rate limit was applied
*/
public synchronized boolean waitForRate(double bytes)
{
resetLast();


double end_time = last_allocated + (bytes / max_bytes_per_ns);
double tm = System.nanoTime();

last_allocated = end_time;
if (end_time > tm)
{
long wait_tm = (long)((end_time - tm) / 1e6);
try
{
Thread.sleep(wait_tm);
}
catch(InterruptedException e)
{
throw new RuntimeException(e);
}
return true;
}
return false;

}




}

0 comments on commit 538ced2

Please sign in to comment.