KLALB大更新
新负载均衡算法
This commit is contained in:
@@ -1,20 +1,33 @@
|
||||
package org.kne.cloud.network.klalb;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.Inet6Address;
|
||||
import java.net.NoRouteToHostException;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class SendTask extends TimerTask {
|
||||
public class SendTask {
|
||||
private int maxcount;
|
||||
private int count;
|
||||
private AtomicInteger count=new AtomicInteger();
|
||||
private int priority;
|
||||
private Inet6Address address;
|
||||
private volatile long starttime=System.nanoTime();
|
||||
private void resetTime() {
|
||||
starttime=System.nanoTime();
|
||||
}
|
||||
private boolean checkTime(long limit) {
|
||||
long curr=System.nanoTime();
|
||||
boolean b=curr-starttime>limit;
|
||||
if(b)
|
||||
starttime=curr;
|
||||
return b;
|
||||
}
|
||||
public SendTask( KLALBController controllker,Inet6Address address,DATATPacket kp,int priority,int maxcount) {
|
||||
super();
|
||||
this.maxcount = maxcount;
|
||||
this.controllker = controllker;
|
||||
this.kp = kp;
|
||||
this.packet = kp;
|
||||
this.address=address;
|
||||
this.priority=priority;
|
||||
}
|
||||
@@ -28,13 +41,13 @@ public class SendTask extends TimerTask {
|
||||
}
|
||||
|
||||
|
||||
public DATATPacket getKp() {
|
||||
return kp;
|
||||
public DATATPacket getPacket() {
|
||||
return packet;
|
||||
}
|
||||
|
||||
|
||||
private KLALBController controllker;
|
||||
private DATATPacket kp;
|
||||
private DATATPacket packet;
|
||||
|
||||
|
||||
public int getMaxcount() {
|
||||
@@ -43,25 +56,34 @@ public class SendTask extends TimerTask {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
public void check(int number) throws IOException {
|
||||
long limit= (1<<Math.min(4,count.get()-1))*(number<2?100:1000*number)*1000000L;
|
||||
if(checkTime(limit))
|
||||
run();
|
||||
}
|
||||
|
||||
|
||||
public void run() throws IOException {
|
||||
if(count.get()>=maxcount) {
|
||||
throw new IOException("send error!");
|
||||
}
|
||||
try {
|
||||
if(count.get()==0) {
|
||||
controllker.sendPacketToAddress((Inet6Address) address,
|
||||
kp, priority+count);
|
||||
packet, priority+count.get(),1);
|
||||
}else {
|
||||
controllker.sendPacketToAddress((Inet6Address) address,
|
||||
packet, priority+count.get(),2);
|
||||
}
|
||||
} catch (NoRouteToHostException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
count++;
|
||||
if(count>1) {
|
||||
System.out.println("重传:"+kp);
|
||||
}
|
||||
if(count>=maxcount) {
|
||||
cancel();
|
||||
resetTime();
|
||||
count.incrementAndGet();
|
||||
if(count.get()>1) {
|
||||
System.out.println("第"+(count.get()-1)+"次重传:"+packet);
|
||||
}
|
||||
|
||||
}
|
||||
public void addToTimer(int timeout) {
|
||||
controllker.getTimer().scheduleAtFixedRate(this, 0, timeout);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user