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
3 changes: 0 additions & 3 deletions rsocket-core/src/main/java/io/rsocket/RSocketClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 0 additions & 3 deletions rsocket-core/src/main/java/io/rsocket/RSocketServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,6 @@ public Mono<Void> onClose() {
}

private void cleanup() {
if (keepAliveFramesAcceptor != null) {
keepAliveFramesAcceptor.dispose();
}
cleanUpSendingSubscriptions();
cleanUpChannelProcessors();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public DuplexConnection connection() {

@Override
public KeepAliveHandler keepAliveHandler() {
return new DefaultKeepAliveHandler();
return new DefaultKeepAliveHandler(connection);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public Mono<Void> acceptRSocketSetup(
multiplexer.dispose();
});
} else {
return then.apply(new DefaultKeepAliveHandler(), multiplexer);
return then.apply(new DefaultKeepAliveHandler(multiplexer), multiplexer);
}
}

Expand Down Expand Up @@ -132,7 +132,7 @@ public Mono<Void> acceptRSocketSetup(
new ResumableKeepAliveHandler(connection),
new ClientServerInputMultiplexer(connection));
} else {
return then.apply(new DefaultKeepAliveHandler(), multiplexer);
return then.apply(new DefaultKeepAliveHandler(multiplexer), multiplexer);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,4 @@
public interface KeepAliveFramesAcceptor {

void receive(ByteBuf keepAliveFrame);

void dispose();
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -13,12 +14,18 @@ KeepAliveFramesAcceptor start(
Consumer<KeepAlive> onTimeout);

class DefaultKeepAliveHandler implements KeepAliveHandler {
private final Closeable duplexConnection;

public DefaultKeepAliveHandler(Closeable duplexConnection) {
this.duplexConnection = duplexConnection;
}

@Override
public KeepAliveFramesAcceptor start(
KeepAliveSupport keepAliveSupport,
Consumer<ByteBuf> onSendKeepAliveFrame,
Consumer<KeepAlive> onTimeout) {
duplexConnection.onClose().subscribe(v -> keepAliveSupport.stop());
return keepAliveSupport
.onSendKeepAliveFrame(onSendKeepAliveFrame)
.onTimeout(onTimeout)
Expand All @@ -38,7 +45,8 @@ public KeepAliveFramesAcceptor start(
KeepAliveSupport keepAliveSupport,
Consumer<ByteBuf> onSendKeepAliveFrame,
Consumer<KeepAlive> onTimeout) {
resumableDuplexConnection.onResumed(keepAliveSupport::start);
resumableDuplexConnection.onResume(keepAliveSupport::start);
resumableDuplexConnection.onDisconnect(keepAliveSupport::stop);
return keepAliveSupport
.resumeState(resumableDuplexConnection)
.onSendKeepAliveFrame(onSendKeepAliveFrame)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -106,7 +109,7 @@ void tryTimeout() {
if (onTimeout != null) {
onTimeout.accept(new KeepAlive(keepAliveInterval, keepAliveTimeout));
}
stopTicks();
stop();
}
}

Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,24 +84,22 @@ public void releaseFrames(long remoteImpliedPos) {

@Override
public Flux<ByteBuf> 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;
});
}

Expand Down Expand Up @@ -177,11 +175,35 @@ void saveFrame(ByteBuf frame) {
}
}

private static Queue<ByteBuf> 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<ByteBuf> cachedFramesQueue(int size) {
return Queues.<ByteBuf>get(size).get();
}

private class FramesSubscriber implements Subscriber<ByteBuf> {
class FramesSubscriber implements Subscriber<ByteBuf> {
private final long firstRequestSize;
private final long refillSize;
private int received;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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();
}
}
}

Expand Down
Loading