All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.deephaven.server.session.SessionCloseableObserver Maven / Gradle / Ivy

The newest version!
//
// Copyright (c) 2016-2024 Deephaven Data Labs and Patent Pending
//
package io.deephaven.server.session;

import io.grpc.stub.ServerCallStreamObserver;
import io.grpc.stub.StreamObserver;

import java.io.Closeable;

import static io.deephaven.extensions.barrage.util.GrpcUtil.safelyComplete;

public abstract class SessionCloseableObserver implements Closeable {
    protected final SessionState session;
    protected final StreamObserver responseObserver;
    private boolean isClosed = false;

    public SessionCloseableObserver(
            final SessionState session,
            final StreamObserver responseObserver) {
        this.session = session;
        this.responseObserver = responseObserver;
        session.addOnCloseCallback(this);
        ((ServerCallStreamObserver) responseObserver).setOnCancelHandler(this::close);
    }

    @Override
    public final void close() {
        session.removeOnCloseCallback(this);

        synchronized (this) {
            if (isClosed) {
                return;
            }
            isClosed = true;
        }

        onClose();
        safelyComplete(responseObserver);
    }

    /**
     * Override this to perform any additional specific clean up that must be performed.
     */
    protected void onClose() {

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy