package com.atlassian.bitbucket.mesh.rpc;

import io.grpc.ForwardingServerCallListener.SimpleForwardingServerCallListener;
import io.grpc.ServerCall;

/**
 * Interceptor that ensure {@link ZeroCopyStreamCloser#close()} is called after a message has been processed to ensure
 * that, if the stream was detached during marshalling, the stream is closed after the message has been fully processed.
 */
public class ZeroCopyServerCallListener<ReqT> extends SimpleForwardingServerCallListener<ReqT> {

    public ZeroCopyServerCallListener(ServerCall.Listener<ReqT> delegate) {
        super(delegate);
    }

    @Override
    public void onMessage(ReqT message) {
        try {
            super.onMessage(message);
        } finally {
            ZeroCopyStreamCloser.close();
        }
    }
}
