Skip to content

feat: update inactivity handling #18

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

Merged
merged 1 commit into from
Oct 1, 2023
Merged
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
17 changes: 13 additions & 4 deletions src/main/java/com/topcoder/dal/DBAccessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,15 @@ public StreamObserver<QueryRequest> streamQuery(StreamObserver<QueryResponse> re
AtomicLong lastTimerReset = new AtomicLong(System.nanoTime() - DEBOUNCE_INTERVAL.toNanos() - 1);
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
AtomicReference<ScheduledFuture<?>> streamTimeoutFuture = new AtomicReference<>(scheduleStreamTimeout());
private Boolean isStreamAlive = true;

@Override
public void onNext(QueryRequest request) {
if (!isStreamAlive) {
responseObserver.onError(Status.DEADLINE_EXCEEDED.withDescription("Stream closed due to inactivity")
.asRuntimeException());
return;
}
cancelStreamTimeout();
try {
QueryResponse response = executeQuery(request.getQuery(), con);
Expand All @@ -350,9 +356,11 @@ public void onError(Throwable throwable) {

@Override
public void onCompleted() {
cancelStreamTimeout();
commit();
responseObserver.onCompleted();
if (isStreamAlive) {
cancelStreamTimeout();
commit();
responseObserver.onCompleted();
}
}

private void commit() {
Expand All @@ -362,6 +370,7 @@ private void commit() {

private void rollback() {
logger.info("Rolling back transaction");
isStreamAlive = false;
jdbcTemplate.rollback(con);
}

Expand Down Expand Up @@ -390,7 +399,7 @@ private ScheduledFuture<?> scheduleStreamTimeout() {
logger.error(message);
rollback();
cancelStreamTimeout();
responseObserver.onError(Status.DEADLINE_EXCEEDED.withDescription(message).asRuntimeException());
responseObserver.onCompleted();
}, streamTimeout.plus(DEBOUNCE_INTERVAL).toNanos(), TimeUnit.NANOSECONDS);
}
};
Expand Down