diff --git a/rsocket-core/src/main/java/io/rsocket/RSocketClient.java b/rsocket-core/src/main/java/io/rsocket/RSocketClient.java index 0c97c05de..b4c9dbb20 100644 --- a/rsocket-core/src/main/java/io/rsocket/RSocketClient.java +++ b/rsocket-core/src/main/java/io/rsocket/RSocketClient.java @@ -433,9 +433,6 @@ private boolean contains(int streamId) { protected void terminate() { lifecycle.setTerminationError(new ClosedChannelException()); - if (keepAliveFramesAcceptor != null) { - keepAliveFramesAcceptor.dispose(); - } try { receivers.values().forEach(this::cleanUpSubscriber); senders.values().forEach(this::cleanUpLimitableRequestPublisher); diff --git a/rsocket-core/src/main/java/io/rsocket/RSocketServer.java b/rsocket-core/src/main/java/io/rsocket/RSocketServer.java index 6b1ed4c00..451ed7b42 100644 --- a/rsocket-core/src/main/java/io/rsocket/RSocketServer.java +++ b/rsocket-core/src/main/java/io/rsocket/RSocketServer.java @@ -280,9 +280,6 @@ public Mono onClose() { } private void cleanup() { - if (keepAliveFramesAcceptor != null) { - keepAliveFramesAcceptor.dispose(); - } cleanUpSendingSubscriptions(); cleanUpChannelProcessors(); diff --git a/rsocket-core/src/main/java/io/rsocket/internal/ClientSetup.java b/rsocket-core/src/main/java/io/rsocket/internal/ClientSetup.java index 5f7248cbe..38217bdc2 100644 --- a/rsocket-core/src/main/java/io/rsocket/internal/ClientSetup.java +++ b/rsocket-core/src/main/java/io/rsocket/internal/ClientSetup.java @@ -53,7 +53,7 @@ public DuplexConnection connection() { @Override public KeepAliveHandler keepAliveHandler() { - return new DefaultKeepAliveHandler(); + return new DefaultKeepAliveHandler(connection); } @Override diff --git a/rsocket-core/src/main/java/io/rsocket/internal/ServerSetup.java b/rsocket-core/src/main/java/io/rsocket/internal/ServerSetup.java index 2868fd9f0..dbd8bc173 100644 --- a/rsocket-core/src/main/java/io/rsocket/internal/ServerSetup.java +++ b/rsocket-core/src/main/java/io/rsocket/internal/ServerSetup.java @@ -64,7 +64,7 @@ public Mono acceptRSocketSetup( multiplexer.dispose(); }); } else { - return then.apply(new DefaultKeepAliveHandler(), multiplexer); + return then.apply(new DefaultKeepAliveHandler(multiplexer), multiplexer); } } @@ -132,7 +132,7 @@ public Mono acceptRSocketSetup( new ResumableKeepAliveHandler(connection), new ClientServerInputMultiplexer(connection)); } else { - return then.apply(new DefaultKeepAliveHandler(), multiplexer); + return then.apply(new DefaultKeepAliveHandler(multiplexer), multiplexer); } } diff --git a/rsocket-core/src/main/java/io/rsocket/keepalive/KeepAliveFramesAcceptor.java b/rsocket-core/src/main/java/io/rsocket/keepalive/KeepAliveFramesAcceptor.java index db6314e71..6fc96d6d2 100644 --- a/rsocket-core/src/main/java/io/rsocket/keepalive/KeepAliveFramesAcceptor.java +++ b/rsocket-core/src/main/java/io/rsocket/keepalive/KeepAliveFramesAcceptor.java @@ -5,6 +5,4 @@ public interface KeepAliveFramesAcceptor { void receive(ByteBuf keepAliveFrame); - - void dispose(); } diff --git a/rsocket-core/src/main/java/io/rsocket/keepalive/KeepAliveHandler.java b/rsocket-core/src/main/java/io/rsocket/keepalive/KeepAliveHandler.java index cc1f53c32..b8ba8d5d0 100644 --- a/rsocket-core/src/main/java/io/rsocket/keepalive/KeepAliveHandler.java +++ b/rsocket-core/src/main/java/io/rsocket/keepalive/KeepAliveHandler.java @@ -1,6 +1,7 @@ package io.rsocket.keepalive; import io.netty.buffer.ByteBuf; +import io.rsocket.Closeable; import io.rsocket.keepalive.KeepAliveSupport.KeepAlive; import io.rsocket.resume.ResumableDuplexConnection; import java.util.function.Consumer; @@ -13,12 +14,18 @@ KeepAliveFramesAcceptor start( Consumer onTimeout); class DefaultKeepAliveHandler implements KeepAliveHandler { + private final Closeable duplexConnection; + + public DefaultKeepAliveHandler(Closeable duplexConnection) { + this.duplexConnection = duplexConnection; + } @Override public KeepAliveFramesAcceptor start( KeepAliveSupport keepAliveSupport, Consumer onSendKeepAliveFrame, Consumer onTimeout) { + duplexConnection.onClose().subscribe(v -> keepAliveSupport.stop()); return keepAliveSupport .onSendKeepAliveFrame(onSendKeepAliveFrame) .onTimeout(onTimeout) @@ -38,7 +45,8 @@ public KeepAliveFramesAcceptor start( KeepAliveSupport keepAliveSupport, Consumer onSendKeepAliveFrame, Consumer onTimeout) { - resumableDuplexConnection.onResumed(keepAliveSupport::start); + resumableDuplexConnection.onResume(keepAliveSupport::start); + resumableDuplexConnection.onDisconnect(keepAliveSupport::stop); return keepAliveSupport .resumeState(resumableDuplexConnection) .onSendKeepAliveFrame(onSendKeepAliveFrame) diff --git a/rsocket-core/src/main/java/io/rsocket/keepalive/KeepAliveSupport.java b/rsocket-core/src/main/java/io/rsocket/keepalive/KeepAliveSupport.java index 9573b70e6..ea8a0de22 100644 --- a/rsocket-core/src/main/java/io/rsocket/keepalive/KeepAliveSupport.java +++ b/rsocket-core/src/main/java/io/rsocket/keepalive/KeepAliveSupport.java @@ -27,7 +27,7 @@ import reactor.core.Disposable; import reactor.core.publisher.Flux; -public abstract class KeepAliveSupport implements Disposable, KeepAliveFramesAcceptor { +public abstract class KeepAliveSupport implements KeepAliveFramesAcceptor { final ByteBufAllocator allocator; private final Duration keepAliveInterval; private final Duration keepAliveTimeout; @@ -50,13 +50,16 @@ private KeepAliveSupport( public KeepAliveSupport start() { this.lastReceivedMillis = System.currentTimeMillis(); - startTicks(); + if (started.compareAndSet(false, true)) { + ticksDisposable = Flux.interval(keepAliveInterval).subscribe(v -> onIntervalTick()); + } return this; } - @Override - public void dispose() { - stopTicks(); + public void stop() { + if (started.compareAndSet(true, false)) { + ticksDisposable.dispose(); + } } @Override @@ -106,7 +109,7 @@ void tryTimeout() { if (onTimeout != null) { onTimeout.accept(new KeepAlive(keepAliveInterval, keepAliveTimeout)); } - stopTicks(); + stop(); } } @@ -118,18 +121,6 @@ long remoteLastReceivedPosition(ByteBuf keepAliveFrame) { return KeepAliveFrameFlyweight.lastPosition(keepAliveFrame); } - void startTicks() { - if (started.compareAndSet(false, true)) { - ticksDisposable = Flux.interval(keepAliveInterval).subscribe(v -> onIntervalTick()); - } - } - - void stopTicks() { - if (started.compareAndSet(true, false)) { - ticksDisposable.dispose(); - } - } - public static final class ServerKeepAliveSupport extends KeepAliveSupport { public ServerKeepAliveSupport( diff --git a/rsocket-core/src/main/java/io/rsocket/resume/InMemoryResumableFramesStore.java b/rsocket-core/src/main/java/io/rsocket/resume/InMemoryResumableFramesStore.java index 8a8692cab..1875b7eac 100644 --- a/rsocket-core/src/main/java/io/rsocket/resume/InMemoryResumableFramesStore.java +++ b/rsocket-core/src/main/java/io/rsocket/resume/InMemoryResumableFramesStore.java @@ -84,24 +84,22 @@ public void releaseFrames(long remoteImpliedPos) { @Override public Flux resumeStream() { - return Flux.create( - s -> { - int size = cachedFrames.size(); - int refCnt = upstreamFrameRefCnt; - logger.debug("{} Resuming stream size: {}", tag, size); - /*spsc queue has no iterator - iterating by consuming*/ - for (int i = 0; i < size; i++) { + return Flux.generate( + () -> new ResumeStreamState(cachedFrames.size(), upstreamFrameRefCnt), + (state, sink) -> { + if (state.next()) { + /*spsc queue has no iterator - iterating by consuming*/ ByteBuf frame = cachedFrames.poll(); - /*in the event of connection termination some frames - * are not released on DuplexConnection*/ - if (frame.refCnt() == refCnt) { + if (state.shouldRetain(frame)) { frame.retain(); } cachedFrames.offer(frame); - s.next(frame); + sink.next(frame); + } else { + sink.complete(); + logger.debug("{} Resuming stream completed", tag); } - s.complete(); - logger.debug("{} Resuming stream completed", tag); + return state; }); } @@ -177,11 +175,35 @@ void saveFrame(ByteBuf frame) { } } - private static Queue cachedFramesQueue(int size) { + static class ResumeStreamState { + private final int cacheSize; + private final int expectedRefCnt; + private int cacheCounter; + + public ResumeStreamState(int cacheSize, int expectedRefCnt) { + this.cacheSize = cacheSize; + this.expectedRefCnt = expectedRefCnt; + } + + public boolean next() { + if (cacheCounter < cacheSize) { + cacheCounter++; + return true; + } else { + return false; + } + } + + public boolean shouldRetain(ByteBuf frame) { + return frame.refCnt() == expectedRefCnt; + } + } + + static Queue cachedFramesQueue(int size) { return Queues.get(size).get(); } - private class FramesSubscriber implements Subscriber { + class FramesSubscriber implements Subscriber { private final long firstRequestSize; private final long refillSize; private int received; diff --git a/rsocket-core/src/main/java/io/rsocket/resume/ResumableDuplexConnection.java b/rsocket-core/src/main/java/io/rsocket/resume/ResumableDuplexConnection.java index dd76001e6..49401d560 100644 --- a/rsocket-core/src/main/java/io/rsocket/resume/ResumableDuplexConnection.java +++ b/rsocket-core/src/main/java/io/rsocket/resume/ResumableDuplexConnection.java @@ -36,6 +36,7 @@ public class ResumableDuplexConnection implements DuplexConnection, ResumeStateHolder { private static final Logger logger = LoggerFactory.getLogger(ResumableDuplexConnection.class); + private static final Throwable closedChannelException = new ClosedChannelException(); private final String tag; private final ResumableFramesStore resumableFramesStore; @@ -64,11 +65,12 @@ public class ResumableDuplexConnection implements DuplexConnection, ResumeStateH resumeSaveStreamRequestListener.requests(), this::dispatch); - private volatile Runnable onResumedAction; + private volatile Runnable onResume; + private volatile Runnable onDisconnect; private volatile int state; private volatile Disposable resumedStreamDisposable = Disposables.disposed(); - ResumableDuplexConnection( + public ResumableDuplexConnection( String tag, DuplexConnection duplexConnection, ResumableFramesStore resumableFramesStore, @@ -110,8 +112,12 @@ public void disconnect() { } } - public void onResumed(Runnable onResumedAction) { - this.onResumedAction = onResumedAction; + public void onDisconnect(Runnable onDisconnectAction) { + this.onDisconnect = onDisconnectAction; + } + + public void onResume(Runnable onResumeAction) { + this.onResume = onResumeAction; } /*reconnected by session after error. After this downstream can receive frames, @@ -284,9 +290,9 @@ private void doResume( .apply(impliedPositionOrError) .doOnSuccess( v -> { - Runnable a = this.onResumedAction; - if (a != null) { - a.run(); + Runnable r = this.onResume; + if (r != null) { + r.run(); } }) .then( @@ -336,10 +342,17 @@ private void onNewConnection(DuplexConnection connection) { private void disconnect(DuplexConnection connection) { /*do not report late disconnects on old connection if new one is available*/ if (curConnection == connection && state != State.DISCONNECTED) { - Throwable err = new ClosedChannelException(); + connection.dispose(); state = State.DISCONNECTED; - logger.debug("{} Inner connection disconnected: {}", tag, err.getClass().getSimpleName()); - connectionErrors.onNext(err); + logger.debug( + "{} Inner connection disconnected: {}", + tag, + closedChannelException.getClass().getSimpleName()); + connectionErrors.onNext(closedChannelException); + Runnable r = this.onDisconnect; + if (r != null) { + r.run(); + } } } diff --git a/rsocket-core/src/test/java/io/rsocket/KeepAliveTest.java b/rsocket-core/src/test/java/io/rsocket/KeepAliveTest.java index 30246c10c..aededb804 100644 --- a/rsocket-core/src/test/java/io/rsocket/KeepAliveTest.java +++ b/rsocket-core/src/test/java/io/rsocket/KeepAliveTest.java @@ -17,6 +17,7 @@ package io.rsocket; import static io.rsocket.keepalive.KeepAliveHandler.DefaultKeepAliveHandler; +import static io.rsocket.keepalive.KeepAliveHandler.ResumableKeepAliveHandler; import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBufAllocator; @@ -25,6 +26,8 @@ import io.rsocket.frame.FrameHeaderFlyweight; import io.rsocket.frame.FrameType; import io.rsocket.frame.KeepAliveFrameFlyweight; +import io.rsocket.resume.InMemoryResumableFramesStore; +import io.rsocket.resume.ResumableDuplexConnection; import io.rsocket.test.util.TestDuplexConnection; import io.rsocket.util.DefaultPayload; import java.time.Duration; @@ -44,14 +47,21 @@ public class KeepAliveTest { private static final int KEEP_ALIVE_INTERVAL = 100; private static final int KEEP_ALIVE_TIMEOUT = 1000; + private static final int RESUMABLE_KEEP_ALIVE_TIMEOUT = 200; - static Stream> testData() { + static Stream> rSocketStates() { return Stream.of( requester(KEEP_ALIVE_INTERVAL, KEEP_ALIVE_TIMEOUT), responder(KEEP_ALIVE_INTERVAL, KEEP_ALIVE_TIMEOUT)); } - static Supplier requester(int tickPeriod, int timeout) { + static Stream> resumableRSocketStates() { + return Stream.of( + resumableRequester(KEEP_ALIVE_INTERVAL, RESUMABLE_KEEP_ALIVE_TIMEOUT), + resumableResponder(KEEP_ALIVE_INTERVAL, RESUMABLE_KEEP_ALIVE_TIMEOUT)); + } + + static Supplier requester(int tickPeriod, int timeout) { return () -> { TestDuplexConnection connection = new TestDuplexConnection(); Errors errors = new Errors(); @@ -64,12 +74,12 @@ static Supplier requester(int tickPeriod, int timeout) { StreamIdSupplier.clientSupplier(), tickPeriod, timeout, - new DefaultKeepAliveHandler()); - return new TestData(rSocket, errors, connection); + new DefaultKeepAliveHandler(connection)); + return new RSocketState(rSocket, errors, connection); }; } - static Supplier responder(int tickPeriod, int timeout) { + static Supplier responder(int tickPeriod, int timeout) { return () -> { TestDuplexConnection connection = new TestDuplexConnection(); AbstractRSocket handler = new AbstractRSocket() {}; @@ -83,16 +93,68 @@ static Supplier responder(int tickPeriod, int timeout) { errors, tickPeriod, timeout, - new DefaultKeepAliveHandler()); - return new TestData(rSocket, errors, connection); + new DefaultKeepAliveHandler(connection)); + return new RSocketState(rSocket, errors, connection); + }; + } + + static Supplier resumableRequester(int tickPeriod, int timeout) { + return () -> { + TestDuplexConnection connection = new TestDuplexConnection(); + ResumableDuplexConnection resumableConnection = + new ResumableDuplexConnection( + "test", + connection, + new InMemoryResumableFramesStore("test", 10_000), + Duration.ofSeconds(10), + false); + + Errors errors = new Errors(); + RSocketClient rSocket = + new RSocketClient( + ByteBufAllocator.DEFAULT, + resumableConnection, + DefaultPayload::create, + errors, + StreamIdSupplier.clientSupplier(), + tickPeriod, + timeout, + new ResumableKeepAliveHandler(resumableConnection)); + return new ResumableRSocketState(rSocket, errors, connection, resumableConnection); + }; + } + + static Supplier resumableResponder(int tickPeriod, int timeout) { + return () -> { + AbstractRSocket handler = new AbstractRSocket() {}; + TestDuplexConnection connection = new TestDuplexConnection(); + ResumableDuplexConnection resumableConnection = + new ResumableDuplexConnection( + "test", + connection, + new InMemoryResumableFramesStore("test", 10_000), + Duration.ofSeconds(10), + false); + Errors errors = new Errors(); + RSocketServer rSocket = + new RSocketServer( + ByteBufAllocator.DEFAULT, + resumableConnection, + handler, + DefaultPayload::create, + errors, + tickPeriod, + timeout, + new ResumableKeepAliveHandler(resumableConnection)); + return new ResumableRSocketState(rSocket, errors, connection, resumableConnection); }; } @ParameterizedTest - @MethodSource("testData") - void rSocketNotDisposedOnPresentKeepAlives(Supplier testDataSupplier) { - TestData testData = testDataSupplier.get(); - TestDuplexConnection connection = testData.connection(); + @MethodSource("rSocketStates") + void rSocketNotDisposedOnPresentKeepAlives(Supplier testDataSupplier) { + RSocketState RSocketState = testDataSupplier.get(); + TestDuplexConnection connection = RSocketState.connection(); Flux.interval(Duration.ofMillis(100)) .subscribe( @@ -103,22 +165,33 @@ void rSocketNotDisposedOnPresentKeepAlives(Supplier testDataSupplier) Mono.delay(Duration.ofMillis(1500)).block(); - RSocket rSocket = testData.rSocket(); - List errors = testData.errors().errors(); + RSocket rSocket = RSocketState.rSocket(); + List errors = RSocketState.errors().errors(); Assertions.assertThat(rSocket.isDisposed()).isFalse(); Assertions.assertThat(errors).isEmpty(); } @ParameterizedTest - @MethodSource("testData") - void rSocketDisposedOnMissingKeepAlives(Supplier testDataSupplier) { - TestData testData = testDataSupplier.get(); - RSocket rSocket = testData.rSocket(); + @MethodSource("rSocketStates") + void noKeepAlivesSentAfterRSocketDispose(Supplier testDataSupplier) { + RSocketState RSocketState = testDataSupplier.get(); + RSocketState.rSocket().dispose(); + StepVerifier.create( + Flux.from(RSocketState.connection().getSentAsPublisher()).take(Duration.ofMillis(500))) + .expectComplete() + .verify(Duration.ofSeconds(1)); + } + + @ParameterizedTest + @MethodSource("rSocketStates") + void rSocketDisposedOnMissingKeepAlives(Supplier testDataSupplier) { + RSocketState rSocketState = testDataSupplier.get(); + RSocket rSocket = rSocketState.rSocket(); Mono.delay(Duration.ofMillis(1500)).block(); - List errors = testData.errors().errors(); + List errors = rSocketState.errors().errors(); Assertions.assertThat(rSocket.isDisposed()).isTrue(); Assertions.assertThat(errors).hasSize(1); Throwable throwable = errors.get(0); @@ -127,8 +200,8 @@ void rSocketDisposedOnMissingKeepAlives(Supplier testDataSupplier) { @Test void clientRequesterSendsKeepAlives() { - TestData testData = requester(100, 1000).get(); - TestDuplexConnection connection = testData.connection(); + RSocketState RSocketState = requester(100, 1000).get(); + TestDuplexConnection connection = RSocketState.connection(); StepVerifier.create(Flux.from(connection.getSentAsPublisher()).take(3)) .expectNextMatches(this::keepAliveFrameWithRespondFlag) @@ -140,8 +213,8 @@ void clientRequesterSendsKeepAlives() { @Test void serverResponderSendsKeepAlives() { - TestData testData = responder(100, 1000).get(); - TestDuplexConnection connection = testData.connection(); + RSocketState RSocketState = responder(100, 1000).get(); + TestDuplexConnection connection = RSocketState.connection(); Mono.delay(Duration.ofMillis(100)) .subscribe( l -> @@ -155,31 +228,124 @@ void serverResponderSendsKeepAlives() { .verify(Duration.ofSeconds(5)); } + @Test + void resumableRequesterNoKeepAlivesAfterDisconnect() { + ResumableRSocketState rSocketState = + resumableRequester(KEEP_ALIVE_INTERVAL, KEEP_ALIVE_TIMEOUT).get(); + TestDuplexConnection testConnection = rSocketState.connection(); + ResumableDuplexConnection resumableDuplexConnection = rSocketState.resumableDuplexConnection(); + + resumableDuplexConnection.disconnect(); + + StepVerifier.create(Flux.from(testConnection.getSentAsPublisher()).take(Duration.ofMillis(500))) + .expectComplete() + .verify(Duration.ofSeconds(5)); + } + + @Test + void resumableRequesterKeepAlivesAfterReconnect() { + ResumableRSocketState rSocketState = + resumableRequester(KEEP_ALIVE_INTERVAL, KEEP_ALIVE_TIMEOUT).get(); + ResumableDuplexConnection resumableDuplexConnection = rSocketState.resumableDuplexConnection(); + resumableDuplexConnection.disconnect(); + TestDuplexConnection newTestConnection = new TestDuplexConnection(); + resumableDuplexConnection.reconnect(newTestConnection); + resumableDuplexConnection.resume(0, 0, ignored -> Mono.empty()); + + StepVerifier.create(Flux.from(newTestConnection.getSentAsPublisher()).take(1)) + .expectNextMatches(this::keepAliveFrame) + .expectComplete() + .verify(Duration.ofSeconds(5)); + } + + @Test + void resumableRequesterNoKeepAlivesAfterDispose() { + ResumableRSocketState rSocketState = + resumableRequester(KEEP_ALIVE_INTERVAL, KEEP_ALIVE_TIMEOUT).get(); + rSocketState.rSocket().dispose(); + StepVerifier.create( + Flux.from(rSocketState.connection().getSentAsPublisher()).take(Duration.ofMillis(500))) + .expectComplete() + .verify(Duration.ofSeconds(5)); + } + + @ParameterizedTest + @MethodSource("resumableRSocketStates") + void resumableRSocketsNotDisposedOnMissingKeepAlives( + Supplier testDataSupplier) { + ResumableRSocketState rSocketState = testDataSupplier.get(); + RSocket rSocket = rSocketState.rSocket(); + List errors = rSocketState.errors().errors(); + TestDuplexConnection connection = rSocketState.connection(); + + Mono.delay(Duration.ofMillis(500)).block(); + + Assertions.assertThat(rSocket.isDisposed()).isFalse(); + Assertions.assertThat(errors).hasSize(0); + Assertions.assertThat(connection.isDisposed()).isTrue(); + } + + private boolean keepAliveFrame(ByteBuf frame) { + return FrameHeaderFlyweight.frameType(frame) == FrameType.KEEPALIVE; + } + private boolean keepAliveFrameWithRespondFlag(ByteBuf frame) { - return FrameHeaderFlyweight.frameType(frame) == FrameType.KEEPALIVE - && KeepAliveFrameFlyweight.respondFlag(frame); + return keepAliveFrame(frame) && KeepAliveFrameFlyweight.respondFlag(frame); } private boolean keepAliveFrameWithoutRespondFlag(ByteBuf frame) { - return FrameHeaderFlyweight.frameType(frame) == FrameType.KEEPALIVE - && !KeepAliveFrameFlyweight.respondFlag(frame); + return keepAliveFrame(frame) && !KeepAliveFrameFlyweight.respondFlag(frame); + } + + static class RSocketState { + private final RSocket rSocket; + private final Errors errors; + private final TestDuplexConnection connection; + + public RSocketState(RSocket rSocket, Errors errors, TestDuplexConnection connection) { + this.rSocket = rSocket; + this.errors = errors; + this.connection = connection; + } + + public TestDuplexConnection connection() { + return connection; + } + + public RSocket rSocket() { + return rSocket; + } + + public Errors errors() { + return errors; + } } - static class TestData { + static class ResumableRSocketState { private final RSocket rSocket; private final Errors errors; private final TestDuplexConnection connection; + private final ResumableDuplexConnection resumableDuplexConnection; - public TestData(RSocket rSocket, Errors errors, TestDuplexConnection connection) { + public ResumableRSocketState( + RSocket rSocket, + Errors errors, + TestDuplexConnection connection, + ResumableDuplexConnection resumableDuplexConnection) { this.rSocket = rSocket; this.errors = errors; this.connection = connection; + this.resumableDuplexConnection = resumableDuplexConnection; } public TestDuplexConnection connection() { return connection; } + public ResumableDuplexConnection resumableDuplexConnection() { + return resumableDuplexConnection; + } + public RSocket rSocket() { return rSocket; } diff --git a/rsocket-transport-netty/src/main/java/io/rsocket/transport/netty/SendPublisher.java b/rsocket-transport-netty/src/main/java/io/rsocket/transport/netty/SendPublisher.java index a697524b3..cf21e64bd 100644 --- a/rsocket-transport-netty/src/main/java/io/rsocket/transport/netty/SendPublisher.java +++ b/rsocket-transport-netty/src/main/java/io/rsocket/transport/netty/SendPublisher.java @@ -2,6 +2,7 @@ import io.netty.buffer.ByteBuf; import io.netty.channel.Channel; +import io.netty.channel.ChannelPromise; import io.netty.channel.EventLoop; import io.netty.util.ReferenceCountUtil; import io.netty.util.ReferenceCounted; @@ -84,22 +85,26 @@ class SendPublisher extends Flux { fuse = queue instanceof Fuseable.QueueSubscription; } - @SuppressWarnings("unchecked") - private void writeCleanup(V poll) { - if (requested != Long.MAX_VALUE) { - requested--; - } - requestedUpstream--; - pending--; + private ChannelPromise writeCleanupPromise(V poll) { + return channel + .newPromise() + .addListener( + future -> { + if (requested != Long.MAX_VALUE) { + requested--; + } + requestedUpstream--; + pending--; - InnerSubscriber is = (InnerSubscriber) INNER_SUBSCRIBER.get(SendPublisher.this); - if (is != null) { - is.tryRequestMoreUpstream(); - tryComplete(is); - } - if (poll.refCnt() > 0) { - ReferenceCountUtil.safeRelease(poll); - } + InnerSubscriber is = (InnerSubscriber) INNER_SUBSCRIBER.get(SendPublisher.this); + if (is != null) { + is.tryRequestMoreUpstream(); + tryComplete(is); + } + if (poll.refCnt() > 0) { + ReferenceCountUtil.safeRelease(poll); + } + }); } private void tryComplete(InnerSubscriber is) { @@ -230,29 +235,11 @@ private void drain() { int readableBytes = sizeOf.size(poll); pending++; if (channel.isWritable() && readableBytes <= channel.bytesBeforeUnwritable()) { - channel - .write(poll) - .addListener( - future -> { - if (future.cause() != null) { - onError(future.cause()); - } else { - writeCleanup(poll); - } - }); + channel.write(poll, writeCleanupPromise(poll)); scheduleFlush = true; } else { scheduleFlush = false; - channel - .writeAndFlush(poll) - .addListener( - future -> { - if (future.cause() != null) { - onError(future.cause()); - } else { - writeCleanup(poll); - } - }); + channel.writeAndFlush(poll, writeCleanupPromise(poll)); } tryRequestMoreUpstream();