Skip to content

Commit

Permalink
feat(stream): fail pending request if server close stream without res…
Browse files Browse the repository at this point in the history
…ponse (#191)

* feat(stream): fail pending request if server close stream without
exception

* fix spotless
  • Loading branch information
mattisonchao authored Nov 16, 2024
1 parent b071de7 commit 7ffdddd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.streamnative.oxia.proto.ReadResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CancellationException;
import lombok.NonNull;

final class ReadBatch extends BatchBase implements Batch, StreamObserver<ReadResponse> {
Expand Down Expand Up @@ -83,6 +84,13 @@ public void onError(Throwable batchError) {

@Override
public void onCompleted() {
// complete pending request if the server close stream without any response
gets.forEach(
g -> {
if (!g.callback().isDone()) {
g.fail(new CancellationException());
}
});
factory.getReadRequestLatencyHistogram().recordSuccess(System.nanoTime() - startSendTimeNanos);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.streamnative.oxia.proto.WriteResponse;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CompletableFuture;
import lombok.extern.slf4j.Slf4j;

Expand Down Expand Up @@ -63,7 +64,17 @@ public void onError(Throwable t) {
}

@Override
public void onCompleted() {}
public void onCompleted() {
synchronized (WriteStreamWrapper.this) {
// complete pending request if the server close stream without any response
pendingWrites.forEach(
f -> {
if (!f.isDone()) {
f.completeExceptionally(new CancellationException());
}
});
}
}

public CompletableFuture<WriteResponse> send(WriteRequest request) {
synchronized (WriteStreamWrapper.this) {
Expand Down

0 comments on commit 7ffdddd

Please sign in to comment.