Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove RxJava - Part 5 #564

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,20 @@
*/
package com.hotels.styx.routing.handlers;

import static java.util.Collections.emptyList;
import static java.util.Objects.requireNonNull;
import static rx.Observable.create;
import static rx.RxReactiveStreams.toObservable;
import static rx.RxReactiveStreams.toPublisher;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import com.hotels.styx.api.Eventual;
import com.hotels.styx.api.HttpHandler;
import com.hotels.styx.api.HttpInterceptor;
import com.hotels.styx.api.LiveHttpRequest;
import com.hotels.styx.api.LiveHttpResponse;

import com.hotels.styx.server.track.RequestTracker;
import rx.Observable;
import org.reactivestreams.Publisher;
import org.reactivestreams.Subscriber;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import static java.util.Collections.emptyList;
import static java.util.Objects.requireNonNull;

/**
* The pipeline consists of a chain of interceptors followed by a handler.
Expand Down Expand Up @@ -99,21 +96,27 @@ public Eventual<LiveHttpResponse> proceed(LiveHttpRequest request) {

requestTracker.markRequestAsSent(request);

return new Eventual<>(toPublisher(toObservable(client.handle(request, this.context))
.compose(StandardHttpPipeline::sendErrorOnDoubleSubscription)));
return new Eventual<>(new SingleSubscriptionPublisher(client.handle(request, this.context)));
}
}

private static Observable<LiveHttpResponse> sendErrorOnDoubleSubscription(Observable<LiveHttpResponse> original) {
AtomicInteger subscriptionCounter = new AtomicInteger();
private static final class SingleSubscriptionPublisher implements Publisher<LiveHttpResponse> {

private AtomicInteger subscriptionCounter = new AtomicInteger();
private Publisher<LiveHttpResponse> original;

return create(subscriber -> {
public SingleSubscriptionPublisher(Publisher<LiveHttpResponse> original) {
this.original = original;
}

@Override
public void subscribe(Subscriber<? super LiveHttpResponse> subscriber) {
if (subscriptionCounter.incrementAndGet() > 1) {
subscriber.onError(new IllegalStateException("Response already subscribed. Additional subscriptions forbidden."));
} else {
original.subscribe(subscriber);
}
});
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static rx.RxReactiveStreams.toObservable;

public class StandardHttpPipelineTest {
@Test
Expand Down Expand Up @@ -167,7 +165,7 @@ public void sendsExceptionUponMultipleSubscription() {
assertThat(response.status(), is(OK));

assertThrows(IllegalStateException.class,
() -> toObservable(responseObservable).toBlocking().first());
() -> Mono.from(responseObservable).block());
}

@ParameterizedTest
Expand All @@ -180,7 +178,7 @@ public void sendsExceptionUponExtraSubscriptionInsideInterceptor(HttpInterceptor

Eventual<LiveHttpResponse> responseObservable = pipeline.handle(get("/").build(), HttpInterceptorContext.create());
assertThrows(IllegalStateException.class,
() -> toObservable(responseObservable).toBlocking().first());
() -> Mono.from(responseObservable).block());
}

private static Stream<Arguments> multipleSubscriptionInterceptors() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.hotels.styx.api.Buffer;
import com.hotels.styx.api.ByteStream;
import com.hotels.styx.api.ContentOverflowException;
import com.hotels.styx.api.Eventual;
import com.hotels.styx.api.HttpHandler;
import com.hotels.styx.api.HttpInterceptor;
import com.hotels.styx.api.HttpResponseStatus;
Expand Down Expand Up @@ -51,11 +52,10 @@
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.codec.DecoderException;
import io.netty.handler.codec.TooLongFrameException;
import org.reactivestreams.Subscription;
import org.slf4j.Logger;
import reactor.core.publisher.BaseSubscriber;
import reactor.core.publisher.Flux;
import rx.Observable;
import rx.Subscriber;
import rx.Subscription;

import javax.net.ssl.SSLHandshakeException;
import java.io.IOException;
Expand Down Expand Up @@ -87,7 +87,6 @@
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Objects.requireNonNull;
import static org.slf4j.LoggerFactory.getLogger;
import static rx.RxReactiveStreams.toObservable;

/**
* Passes request to HTTP Pipeline.
Expand Down Expand Up @@ -265,25 +264,29 @@ private State onLegitimateRequest(LiveHttpRequest request, ChannelHandlerContext
// the same call stack as "onLegitimateRequest" handler. This happens when a plugin
// generates a response.
try {
Observable<LiveHttpResponse> responseObservable = toObservable(httpPipeline.handle(v11Request, newInterceptorContext(ctx)));
subscription = responseObservable
.subscribe(new Subscriber<LiveHttpResponse>() {
@Override
public void onCompleted() {
eventProcessor.submit(new ResponseObservableCompletedEvent(ctx, request.id()));
}

@Override
public void onError(Throwable cause) {
eventProcessor.submit(new ResponseObservableErrorEvent(ctx, cause, request.id()));
}

@Override
public void onNext(LiveHttpResponse response) {
eventProcessor.submit(new ResponseReceivedEvent(response, ctx));
}
}
);
Eventual<LiveHttpResponse> responseEventual = httpPipeline.handle(v11Request, newInterceptorContext(ctx));
responseEventual.subscribe(new BaseSubscriber<LiveHttpResponse>() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would consider inlining responseEventual.

@Override
public void hookOnSubscribe(Subscription s) {
subscription = s;
s.request(1);
}

@Override
public void hookOnComplete() {
eventProcessor.submit(new ResponseObservableCompletedEvent(ctx, request.id()));
}

@Override
public void hookOnError(Throwable cause) {
eventProcessor.submit(new ResponseObservableErrorEvent(ctx, cause, request.id()));
}

@Override
public void hookOnNext(LiveHttpResponse response) {
eventProcessor.submit(new ResponseReceivedEvent(response, ctx));
}
});

return WAITING_FOR_RESPONSE;
} catch (Throwable cause) {
Expand Down Expand Up @@ -628,7 +631,7 @@ private static class ResponseObservableCompletedEvent {

private void cancelSubscription() {
if (subscription != null) {
subscription.unsubscribe();
subscription.cancel();
}
}

Expand Down
Loading