Search in sources :

Example 1 with Sockets

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();
            }
        }
    });
}
Also used : App(org.rx.core.App) Extends(org.rx.core.Extends) org.rx.core(org.rx.core) Constants(org.rx.core.Constants) Getter(lombok.Getter) RandomList(org.rx.bean.RandomList) NonNull(lombok.NonNull) java.util.concurrent(java.util.concurrent) BiTuple(org.rx.bean.BiTuple) InvalidException(org.rx.exception.InvalidException) Set(java.util.Set) InetSocketAddress(java.net.InetSocketAddress) Action(org.rx.util.function.Action) InetAddress(java.net.InetAddress) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) RpcClientConfig(org.rx.net.rpc.RpcClientConfig) Map(java.util.Map) Sockets(org.rx.net.Sockets) Remoting(org.rx.net.rpc.Remoting) Action(org.rx.util.function.Action) Set(java.util.Set) Sockets(org.rx.net.Sockets) InetSocketAddress(java.net.InetSocketAddress) InvalidException(org.rx.exception.InvalidException)

Aggregations

InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 java.util.concurrent (java.util.concurrent)1 Getter (lombok.Getter)1 NonNull (lombok.NonNull)1 Slf4j (lombok.extern.slf4j.Slf4j)1 BiTuple (org.rx.bean.BiTuple)1 RandomList (org.rx.bean.RandomList)1 org.rx.core (org.rx.core)1 App (org.rx.core.App)1 Constants (org.rx.core.Constants)1 Extends (org.rx.core.Extends)1 InvalidException (org.rx.exception.InvalidException)1 Sockets (org.rx.net.Sockets)1 Remoting (org.rx.net.rpc.Remoting)1 RpcClientConfig (org.rx.net.rpc.RpcClientConfig)1 Action (org.rx.util.function.Action)1