forked from KNEMC/KLALB
KLALB2.3
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
package org.kne.cloud.network;
|
||||
|
||||
import java.util.concurrent.locks.LockSupport;
|
||||
|
||||
public class SpeedLimiter {
|
||||
public boolean isHitlimit() {
|
||||
return hitlimit;
|
||||
}
|
||||
public SpeedLimiter(long limitspeed, long timedeltans) {
|
||||
super();
|
||||
this.limitspeed = limitspeed;
|
||||
this.timedeltans = timedeltans;
|
||||
}
|
||||
public SpeedLimiter() {
|
||||
super();
|
||||
}
|
||||
private volatile long limitspeed=1024*1024;
|
||||
private volatile long timedeltans=5000000;
|
||||
private volatile boolean hitlimit=false;
|
||||
public long getLimitspeed() {
|
||||
return limitspeed;
|
||||
}
|
||||
|
||||
public SpeedLimiter(long limitspeed) {
|
||||
super();
|
||||
this.limitspeed = limitspeed;
|
||||
}
|
||||
public void setLimitspeed(long limitspeed) {
|
||||
this.limitspeed = limitspeed;
|
||||
//System.out.println("更新限速:"+limitspeed);
|
||||
}
|
||||
|
||||
public long getTimedeltans() {
|
||||
return timedeltans;
|
||||
}
|
||||
|
||||
public void setTimedeltans(long timedeltans) {
|
||||
this.timedeltans = timedeltans;
|
||||
}
|
||||
private long ltime=System.nanoTime();
|
||||
private long counter=0;
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SpeedLimiter [limitspeed=" + limitspeed + "]";
|
||||
}
|
||||
public boolean checkTransmit(long datasize) {
|
||||
long curtime=System.nanoTime();
|
||||
if(curtime-ltime>timedeltans) {
|
||||
ltime=curtime;
|
||||
if((counter*1000000000)/timedeltans<=limitspeed) {
|
||||
hitlimit=false;
|
||||
}
|
||||
counter=0;
|
||||
}
|
||||
if((counter*1000000000)/timedeltans<=limitspeed) {
|
||||
counter+=datasize;
|
||||
return true;
|
||||
}
|
||||
hitlimit=true;
|
||||
return false;
|
||||
}
|
||||
public void transmit(long datasize) {
|
||||
while(true) {
|
||||
long curtime=System.nanoTime();
|
||||
if(curtime-ltime>timedeltans) {
|
||||
ltime=curtime;
|
||||
if((counter*1000000000)/timedeltans<=limitspeed) {
|
||||
hitlimit=false;
|
||||
}
|
||||
counter=0;
|
||||
}
|
||||
if((counter*1000000000)/timedeltans<=limitspeed) {
|
||||
counter+=datasize;
|
||||
return;
|
||||
}
|
||||
hitlimit=true;
|
||||
//System.out.println("hit limit");
|
||||
LockSupport.parkNanos(100000);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user