ClientAddress优化

This commit is contained in:
redkale
2023-12-01 08:40:30 +08:00
parent 061fecf7b4
commit c234663f87
3 changed files with 67 additions and 36 deletions

View File

@@ -170,6 +170,29 @@ public abstract class Client<C extends ClientConnection<R, P>, R extends ClientR
return null; return null;
} }
//更新地址列表
protected void updateClientAddress(List<SocketAddress> addrs) {
Set<SocketAddress> newAddrs = new HashSet<>(addrs);
Set<SocketAddress> delAddrs = new HashSet<>();
for (SocketAddress addr : this.address.getAddresses()) {
if (!newAddrs.contains(addr)) {
delAddrs.add(addr);
}
}
this.address.updateAddress(addrs);
for (SocketAddress addr : delAddrs) {
AddressConnEntry<C>[] entrys = this.connAddrEntrys.remove(addr);
if (entrys != null) {
for (AddressConnEntry<C> entry : entrys) {
if (entry != null && entry.connection != null) {
entry.connection.dispose(null);
}
}
}
}
}
protected int pingIntervalSeconds() { protected int pingIntervalSeconds() {
return 30; return 30;
} }

View File

@@ -38,14 +38,17 @@ public class ClientAddress implements java.io.Serializable {
if (addrs == null || addrs.isEmpty()) { if (addrs == null || addrs.isEmpty()) {
throw new NullPointerException("addresses is empty"); throw new NullPointerException("addresses is empty");
} }
this.addresses = createAddressArray(addrs); this.addresses = WeightAddress.createAddressArray(addrs);
} }
public void updateAddress(List<WeightAddress> addrs) { void updateAddress(List<SocketAddress> addrs) {
if (addrs == null || addrs.isEmpty()) { if (addrs == null || addrs.isEmpty()) {
throw new NullPointerException("addresses is empty"); throw new NullPointerException("addresses is empty");
} }
this.addresses = createAddressArray(addrs); for (SocketAddress addr : addrs) {
Objects.requireNonNull(addr);
}
this.addresses = addrs.toArray(new SocketAddress[addrs.size()]);
} }
public SocketAddress randomAddress() { public SocketAddress randomAddress() {
@@ -56,39 +59,8 @@ public class ClientAddress implements java.io.Serializable {
return addrs[ThreadLocalRandom.current().nextInt(addrs.length)]; return addrs[ThreadLocalRandom.current().nextInt(addrs.length)];
} }
private static SocketAddress[] createAddressArray(List<WeightAddress> ws) { public Set<SocketAddress> getAddresses() {
int min = 0; return Set.of(addresses);
int size = 0; //20,35,45去掉最大公约数数组长度为:4+7+9=20
for (WeightAddress w : ws) {
size += w.getWeight();
if (min == 0 || w.getWeight() < min) {
min = w.getWeight();
}
}
int divisor = 1; //最大公约数
for (int i = 2; i <= min; i++) {
boolean all = true;
for (WeightAddress w : ws) {
if (w.getWeight() % i > 0) {
all = false;
break;
}
}
if (all) {
divisor = i;
}
}
size /= divisor;
SocketAddress[] newAddrs = new SocketAddress[size];
int index = -1;
for (int i = 0; i < ws.size(); i++) {
WeightAddress w = ws.get(i);
int z = w.getWeight() / divisor;
for (int j = 0; j < z; j++) {
newAddrs[++index] = w.getAddress();
}
}
return newAddrs;
} }
@Override @Override

View File

@@ -4,6 +4,7 @@
package org.redkale.net.client; package org.redkale.net.client;
import java.net.SocketAddress; import java.net.SocketAddress;
import java.util.List;
import java.util.Objects; import java.util.Objects;
import org.redkale.annotation.ConstructorParameters; import org.redkale.annotation.ConstructorParameters;
import org.redkale.convert.ConvertColumn; import org.redkale.convert.ConvertColumn;
@@ -38,6 +39,41 @@ public class WeightAddress implements Comparable<WeightAddress>, java.io.Seriali
this.weight = weight; this.weight = weight;
} }
public static SocketAddress[] createAddressArray(List<WeightAddress> ws) {
int min = 0;
int size = 0; //20,35,45去掉最大公约数数组长度为:4+7+9=20
for (WeightAddress w : ws) {
size += w.getWeight();
if (min == 0 || w.getWeight() < min) {
min = w.getWeight();
}
}
int divisor = 1; //最大公约数
for (int i = 2; i <= min; i++) {
boolean all = true;
for (WeightAddress w : ws) {
if (w.getWeight() % i > 0) {
all = false;
break;
}
}
if (all) {
divisor = i;
}
}
size /= divisor;
SocketAddress[] newAddrs = new SocketAddress[size];
int index = -1;
for (int i = 0; i < ws.size(); i++) {
WeightAddress w = ws.get(i);
int z = w.getWeight() / divisor;
for (int j = 0; j < z; j++) {
newAddrs[++index] = w.getAddress();
}
}
return newAddrs;
}
@Override @Override
public int compareTo(WeightAddress o) { public int compareTo(WeightAddress o) {
return this.weight - (o == null ? 0 : o.weight); return this.weight - (o == null ? 0 : o.weight);