use of org.rx.net.Sockets in project rxlib by RockyLOMO.
the class NameserverClient method registerAsync.
public CompletableFuture<?> registerAsync(@NonNull Set<InetSocketAddress> registerEndpoints) {
if (registerEndpoints.isEmpty()) {
throw new InvalidException("At least one server that required");
}
svrEps.addAll(NQuery.of(registerEndpoints).selectMany(Sockets::allEndpoints).toSet());
return Tasks.run(() -> {
for (InetSocketAddress regEp : svrEps) {
synchronized (hold) {
if (NQuery.of(hold).any(p -> eq(p.left, regEp))) {
continue;
}
BiTuple<InetSocketAddress, Nameserver, Integer> tuple = BiTuple.of(regEp, null, null);
hold.add(tuple);
Action doReg = () -> {
try {
tuple.right = tuple.middle.register(appName, svrEps);
reInject();
} catch (Throwable e) {
delayTasks.computeIfAbsent(appName, k -> Tasks.setTimeout(() -> {
tuple.right = tuple.middle.register(appName, svrEps);
// 优先
delayTasks.remove(appName);
reInject();
return false;
}, DEFAULT_RETRY_PERIOD, null, TimeoutFlag.PERIOD));
}
};
tuple.middle = Remoting.create(Nameserver.class, RpcClientConfig.statefulMode(regEp, 0), (ns, rc) -> {
rc.onConnected.combine((s, e) -> {
hold.setWeight(tuple, RandomList.DEFAULT_WEIGHT);
reInject();
});
rc.onDisconnected.combine((s, e) -> {
hold.setWeight(tuple, 0);
reInject();
});
rc.onReconnecting.combine((s, e) -> {
if (svrEps.addAll(NQuery.of(registerEndpoints).selectMany(Sockets::allEndpoints).toSet())) {
registerAsync(svrEps);
}
});
rc.onReconnected.combine((s, e) -> {
tuple.right = null;
doReg.invoke();
});
ns.<NEventArgs<Set<InetSocketAddress>>>attachEvent(Nameserver.EVENT_CLIENT_SYNC, (s, e) -> {
log.info("sync server endpoints: {}", toJsonString(e.getValue()));
if (e.getValue().isEmpty()) {
return;
}
registerAsync(e.getValue());
}, false);
});
doReg.invoke();
}
}
});
}
Aggregations