Skip to content
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
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ subprojects {
test {
useJUnitPlatform()

testLogging {
events "started", "passed", "skipped", "failed"
}

systemProperty "io.netty.leakDetection.level", "ADVANCED"
}

Expand Down
107 changes: 103 additions & 4 deletions rsocket-core/src/main/java/io/rsocket/RSocketFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,13 @@
import io.rsocket.util.MultiSubscriberRSocket;
import java.time.Duration;
import java.util.Objects;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import reactor.core.Disposable;
import reactor.core.publisher.Mono;
import reactor.util.retry.Retry;

/** Factory for creating RSocket clients and servers. */
public class RSocketFactory {
Expand Down Expand Up @@ -95,6 +98,9 @@ default <T extends Closeable> Start<T> transport(ServerTransport<T> transport) {
public static class ClientRSocketFactory implements ClientTransportAcceptor {
private static final String CLIENT_TAG = "client";

private static final BiConsumer<RSocket, Invalidatable> INVALIDATE_FUNCTION =
(r, i) -> r.onClose().subscribe(null, null, i::invalidate);

private SocketAcceptor acceptor = (setup, sendingSocket) -> Mono.just(new AbstractRSocket() {});

private Consumer<Throwable> errorConsumer = Throwable::printStackTrace;
Expand Down Expand Up @@ -125,6 +131,8 @@ public static class ClientRSocketFactory implements ClientTransportAcceptor {
private boolean multiSubscriberRequester = true;
private boolean leaseEnabled;
private Supplier<Leases<?>> leasesSupplier = Leases::new;
private boolean reconnectEnabled;
private Retry retrySpec;

private ByteBufAllocator allocator = ByteBufAllocator.DEFAULT;

Expand Down Expand Up @@ -230,6 +238,86 @@ public ClientRSocketFactory singleSubscriberRequester() {
return this;
}

/**
* Enables a reconnectable, shared instance of {@code Mono<RSocket>} so every subscriber will
* observe the same RSocket instance up on connection establishment. <br>
* For example:
*
* <pre>{@code
* Mono<RSocket> sharedRSocketMono =
* RSocketFactory
* .connect()
* .singleSubscriberRequester()
* .reconnect(Retry.fixedDelay(3, Duration.ofSeconds(1)))
* .transport(transport)
* .start();
*
* RSocket r1 = sharedRSocketMono.block();
* RSocket r2 = sharedRSocketMono.block();
*
* assert r1 == r2;
*
* }</pre>
*
* Apart of the shared behavior, if the connection is lost, the same {@code Mono<RSocket>}
* instance will transparently re-establish the connection for subsequent subscribers.<br>
* For example:
*
* <pre>{@code
* Mono<RSocket> sharedRSocketMono =
* RSocketFactory
* .connect()
* .singleSubscriberRequester()
* .reconnect(Retry.fixedDelay(3, Duration.ofSeconds(1)))
* .transport(transport)
* .start();
*
* RSocket r1 = sharedRSocketMono.block();
* RSocket r2 = sharedRSocketMono.block();
*
* assert r1 == r2;
*
* r1.dispose()
*
* assert r2.isDisposed()
*
* RSocket r3 = sharedRSocketMono.block();
* RSocket r4 = sharedRSocketMono.block();
*
*
* assert r1 != r3;
* assert r4 == r3;
*
* }</pre>
*

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think it would also be worth mentioning or showing that downstream subscribers (i.e. individual requests) are expected to have retry logic, according to their own needs, in order to trigger a reconnect.

@OlegDokuka OlegDokuka Mar 24, 2020 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

* <b>Note,</b> having reconnect() enabled does not eliminate the need to accompany each
* individual request with the corresponding retry logic. <br>
* For example:
*
* <pre>{@code
* Mono<RSocket> sharedRSocketMono =
* RSocketFactory
* .connect()
* .singleSubscriberRequester()
* .reconnect(Retry.fixedDelay(3, Duration.ofSeconds(1)))
* .transport(transport)
* .start();
*
* sharedRSocket.flatMap(rSocket -> rSocket.requestResponse(...))
* .retryWhen(ownRetry)
* .subscribe()
*
* }</pre>
*
* @param retrySpec a retry factory applied for {@link Mono#retryWhen(Retry)}
* @return a shared instance of {@code Mono<RSocket>}.
*/
public ClientRSocketFactory reconnect(Retry retrySpec) {
this.retrySpec = Objects.requireNonNull(retrySpec);
this.reconnectEnabled = true;
return this;
}

public ClientRSocketFactory resume() {
this.resumeEnabled = true;
return this;
Expand Down Expand Up @@ -392,6 +480,15 @@ public Mono<RSocket> start() {
.sendOne(setupFrame)
.thenReturn(wrappedRSocketRequester);
});
})
.as(
source -> {
if (reconnectEnabled) {
return new ReconnectMono<>(
source.retryWhen(retrySpec), Disposable::dispose, INVALIDATE_FUNCTION);
} else {
return source;
}
});
}

Expand Down Expand Up @@ -422,7 +519,7 @@ private ClientSetup clientSetup(DuplexConnection startConnection) {
}

private Mono<DuplexConnection> newConnection() {
return transportClient.get().connect(mtu);
return Mono.fromSupplier(transportClient).flatMap(t -> t.connect(mtu));
}
}
}
Expand Down Expand Up @@ -698,9 +795,11 @@ public Mono<T> start() {

@Override
public Mono<T> get() {
return transportServer
.get()
.start(duplexConnection -> acceptor(serverSetup, duplexConnection), mtu)
return Mono.fromSupplier(transportServer)
.flatMap(
transport ->
transport.start(
duplexConnection -> acceptor(serverSetup, duplexConnection), mtu))
.doOnNext(c -> c.onClose().doFinally(v -> serverSetup.dispose()).subscribe());
}
});
Expand Down
Loading