package com.atlassian.bitbucket.mesh.rpc;

import io.grpc.ClientCall;
import io.grpc.ForwardingClientCallListener.SimpleForwardingClientCallListener;

/**
 * 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 ZeroCopyClientCallListener<T> extends SimpleForwardingClientCallListener<T> {

    public ZeroCopyClientCallListener(ClientCall.Listener<T> delegate) {
        super(delegate);
    }

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