All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.netflix.eureka2.client.transport.ResolverBasedTransportClient Maven / Gradle / Ivy

package com.netflix.eureka2.client.transport;

import com.netflix.eureka2.client.resolver.ServerResolver;
import com.netflix.eureka2.transport.MessageConnection;
import com.netflix.eureka2.transport.base.BaseMessageConnection;
import com.netflix.eureka2.transport.base.HeartBeatConnection;
import com.netflix.eureka2.transport.base.MessageConnectionMetrics;
import io.reactivex.netty.RxNetty;
import io.reactivex.netty.channel.ObservableConnection;
import io.reactivex.netty.client.RxClient;
import io.reactivex.netty.pipeline.PipelineConfigurator;
import rx.Observable;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

import java.util.concurrent.ConcurrentHashMap;

/**
 * Convenience base implementation for {@link TransportClient} that reads the server list from a {@link ServerResolver}
 *
 * @author Nitesh Kant
 */
public abstract class ResolverBasedTransportClient implements TransportClient {

    private final ServerResolver resolver;
    private final PipelineConfigurator pipelineConfigurator;
    private final MessageConnectionMetrics metrics;
    private ConcurrentHashMap> clients;

    protected ResolverBasedTransportClient(ServerResolver resolver,
                                           PipelineConfigurator pipelineConfigurator,
                                           MessageConnectionMetrics metrics) {
        this.resolver = resolver;
        this.pipelineConfigurator = pipelineConfigurator;
        this.metrics = metrics;
        clients = new ConcurrentHashMap<>();
    }

    public long getHeartbeatIntervalMillis() {
        return HeartBeatConnection.DEFAULT_HEARTBEAT_INTERVAL_MILLIS;
    }

    @Override
    public Observable connect() {
        return resolver.resolve()
                .take(1)
                .map(new Func1>() {
                    @Override
                    public RxClient call(ServerResolver.Server server) {
                        // This should be invoked from a single thread.
                        RxClient client = clients.get(server);
                        if (null == client) {
                            client = RxNetty.createTcpClient(server.getHost(), server.getPort(),
                                    pipelineConfigurator);
                            clients.put(server, client);
                        }
                        return client;
                    }
                })
                .flatMap(new Func1, Observable>() {
                    @Override
                    public Observable call(RxClient client) {
                        return client.connect()
                                .map(new Func1, MessageConnection>() {
                                    @Override
                                    public MessageConnection call(
                                            ObservableConnection conn) {
                                        return new HeartBeatConnection(
                                                new BaseMessageConnection("client", conn, metrics),
                                                getHeartbeatIntervalMillis(), 3, Schedulers.computation()
                                        );
                                    }
                                });
                    }
                })
                .retry(1); // TODO: Better retry strategy?
    }

    @Override
    public void shutdown() {
        for (RxClient client : clients.values()) {
            client.shutdown();
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy