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

com.yandex.ydb.core.grpc.UnaryStreamToFuture Maven / Gradle / Ivy

There is a newer version: 1.45.6
Show newest version
package com.yandex.ydb.core.grpc;

import java.util.concurrent.CompletableFuture;

import javax.annotation.Nullable;

import com.yandex.ydb.core.Issue;
import com.yandex.ydb.core.Result;
import com.yandex.ydb.core.StatusCode;
import io.grpc.ClientCall;
import io.grpc.Metadata;
import io.grpc.Status;


/**
 * @author Sergey Polovko
 */
public class UnaryStreamToFuture extends ClientCall.Listener {

    private final CompletableFuture> responseFuture;
    private T value;

    public UnaryStreamToFuture(CompletableFuture> responseFuture) {
        this.responseFuture = responseFuture;
    }

    @Override
    public void onMessage(T value) {
        if (this.value != null) {
            Issue issue = Issue.of("More than one value received for gRPC unary call", Issue.Severity.ERROR);
            responseFuture.complete(Result.fail(StatusCode.CLIENT_INTERNAL_ERROR, issue));
        }
        this.value = value;
    }

    @Override
    public void onClose(Status status, @Nullable Metadata trailers) {
        if (status.isOk()) {
            if (value == null) {
                Issue issue = Issue.of("No value received for gRPC unary call", Issue.Severity.ERROR);
                responseFuture.complete(Result.fail(StatusCode.CLIENT_INTERNAL_ERROR, issue));
            } else {
                responseFuture.complete(Result.success(value));
            }
        } else {
            responseFuture.complete(GrpcStatuses.toResult(status));
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy