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

io.reactivex.netty.protocol.http.client.RequestProcessingOperator Maven / Gradle / Ivy

There is a newer version: 0.3.18
Show newest version
/*
 * Copyright 2014 Netflix, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.reactivex.netty.protocol.http.client;

import io.reactivex.netty.channel.ObservableConnection;
import io.reactivex.netty.client.ClientMetricsEvent;
import io.reactivex.netty.metrics.Clock;
import io.reactivex.netty.metrics.MetricEventsSubject;
import rx.Observable;
import rx.Observer;
import rx.Subscriber;
import rx.functions.Action0;
import rx.functions.Action1;
import rx.subscriptions.CompositeSubscription;

import java.util.concurrent.TimeUnit;

/**
 * @author Nitesh Kant
 */
class RequestProcessingOperator implements Observable.Operator,
        ObservableConnection, HttpClientRequest>>{

    private final HttpClientRequest request;
    private final MetricEventsSubject> eventsSubject;
    private final long responseSubscriptionTimeoutMs;

    RequestProcessingOperator(HttpClientRequest request, MetricEventsSubject> eventsSubject,
                              long responseSubscriptionTimeoutMs) {
        this.request = request;
        this.eventsSubject = eventsSubject;
        this.responseSubscriptionTimeoutMs = responseSubscriptionTimeoutMs;
    }

    @Override
    public Subscriber, HttpClientRequest>> call(
            final Subscriber> child) {
        final long startTimeMillis = Clock.newStartTimeMillis();
        eventsSubject.onEvent(HttpClientMetricsEvent.REQUEST_SUBMITTED);
        final CompositeSubscription cs = new CompositeSubscription();
        child.add(cs);// Unsubscribe when the child unsubscribes.

        Subscriber, HttpClientRequest>> toReturn =
                new Subscriber, HttpClientRequest>>() {

                    @Override
                    public void onCompleted() {
                        // Ignore onComplete of connection.
                    }

                    @Override
                    public void onError(Throwable e) {
                        child.onError(e);
                    }

                    @Override
                    public void onNext(final ObservableConnection, HttpClientRequest> connection) {

                        // Why don't we close the connection on unsubscribe?
                        // See issue: https://github.com/ReactiveX/RxNetty/issues/225
                        cs.add(connection.getInput()
                                         .doOnNext(new Action1>() {
                                             @Override
                                             public void call(final HttpClientResponse response) {
                                                 response.updateNoContentSubscriptionTimeoutIfNotScheduled(
                                                         responseSubscriptionTimeoutMs,
                                                         TimeUnit.MILLISECONDS);
                                             }
                                         })
                                         .doOnError(new Action1() {
                                             @Override
                                             public void call(Throwable throwable) {
                                                 eventsSubject.onEvent(HttpClientMetricsEvent.RESPONSE_FAILED,
                                                                       Clock.onEndMillis(startTimeMillis), throwable);
                                             }
                                         })
                                         .subscribe(child)); //subscribe the child for response.
                        request.doOnWriteComplete(new Action0() {
                            @Override
                            public void call() {
                                connection.flush().subscribe(new Observer() {
                                    @Override
                                    public void onCompleted() {
                                        // Failure in writes gets reported during write, this is for the entire request write
                                        // over event.
                                        eventsSubject.onEvent(HttpClientMetricsEvent.REQUEST_WRITE_COMPLETE,
                                                              Clock.onEndMillis(startTimeMillis));
                                    }

                                    @Override
                                    public void onError(Throwable e) {
                                        eventsSubject.onEvent(HttpClientMetricsEvent.REQUEST_WRITE_FAILED,
                                                              Clock.onEndMillis(startTimeMillis), e);
                                        child.onError(e);
                                    }

                                    @Override
                                    public void onNext(Void aVoid) {
                                        // No op as nothing to do for a write onNext().
                                    }
                                });
                            }
                        });
                        connection.write(request);
                    }
                };
        return toReturn;
    }
}