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

netflix.ocelli.rxnetty.HttpClientHolder Maven / Gradle / Ivy

There is a newer version: 0.1.0-rc.2
Show newest version
package netflix.ocelli.rxnetty;

import io.reactivex.netty.metrics.HttpClientMetricEventsListener;
import io.reactivex.netty.protocol.http.client.HttpClient;
import io.reactivex.netty.protocol.http.client.HttpClientRequest;
import io.reactivex.netty.protocol.http.client.HttpClientResponse;

import java.util.concurrent.TimeUnit;

import netflix.ocelli.util.SingleMetric;
import rx.Observable;
import rx.Observable.OnSubscribe;
import rx.Subscriber;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.functions.Func2;

/**
 * An extension of {@link MetricAwareClientHolder} for HTTP.
 *
 * @author Nitesh Kant
 */
public class HttpClientHolder extends MetricAwareClientHolder, HttpClientResponse, HttpClient, HttpMetricListener> {
    
    public HttpClientHolder(HttpClient client, SingleMetric metric) {
        super(client, new HttpMetricListener(metric));
    }

    @Override
    public HttpMetricListener getListener() {
        return super.getListener();
    }
    
    public static  Func1, Observable> failureDetector() {
        return new Func1, Observable>() {
            @Override
            public Observable call(final HttpClientHolder holder) {
                return Observable.create(new OnSubscribe() {
                    @Override
                    public void call(final Subscriber sub) {
                        holder.getClient().subscribe(new HttpClientMetricEventsListener() {
                            @Override
                            protected void onConnectFailed(long duration, TimeUnit timeUnit, Throwable throwable) {
                                sub.onNext(throwable);
                            }
                        });
                    }
                });
            }
        };
    }

    /**
     * Comparison by pending request for load balancing decisions
     * @return
     */
    public static  Func2, HttpClientHolder, HttpClientHolder> byPendingRequest() {
        return new Func2, HttpClientHolder, HttpClientHolder>() {
            @Override
            public HttpClientHolder call(
                    HttpClientHolder t1,
                    HttpClientHolder t2) {
                return t1.getListener().getPendingRequests() > t2.getListener().getPendingRequests() ?
                       t1 : t2;
            }
        };
    }

    /**
     * Comparison by average request latency for load balancing decisions
     * @return
     */
    public static  Func2, HttpClientHolder, HttpClientHolder> byAverageLatency() {
        return new Func2, HttpClientHolder, HttpClientHolder>() {
            @Override
            public HttpClientHolder call(
                    HttpClientHolder t1,
                    HttpClientHolder t2) {
                return t1.getListener().getMetric() > t2.getListener().getMetric() ?
                       t1 : t2;
            }
        };
    }

    public static  Action1> shutdown() {
        return new Action1>() {
            @Override
            public void call(HttpClientHolder t1) {
                t1.getClient().shutdown();
            }
        };
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy