Skip to content

Commit

Permalink
Made the grpc to use Managed channel directly to use it in blocking m…
Browse files Browse the repository at this point in the history
…ode (#1084)
  • Loading branch information
maheshrajamani authored May 13, 2024
1 parent 803f644 commit 5da46e7
Showing 1 changed file with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,28 +1,41 @@
package io.stargate.sgv2.jsonapi.service.resolver.model.impl;

import io.quarkus.grpc.GrpcClient;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.quarkus.runtime.ShutdownEvent;
import io.stargate.embedding.gateway.EmbeddingGateway;
import io.stargate.embedding.gateway.EmbeddingServiceGrpc;
import io.stargate.sgv2.jsonapi.api.request.DataApiRequestInfo;
import io.stargate.sgv2.jsonapi.exception.ErrorCode;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.event.Observes;
import jakarta.inject.Inject;
import org.eclipse.microprofile.config.inject.ConfigProperty;

@ApplicationScoped
public class ValidateCredentials {
@GrpcClient("embedding")
EmbeddingServiceGrpc.EmbeddingServiceBlockingStub embeddingService;

private @ConfigProperty(name = "quarkus.grpc.clients.embedding.host") String host;

@ConfigProperty(name = "quarkus.grpc.clients.embedding.port")
private int port;

private ManagedChannel channel = null;

@Inject DataApiRequestInfo dataApiRequestInfo;

public boolean validate(String provider, String value) {
if (channel == null) {
channel = ManagedChannelBuilder.forAddress(host, port).usePlaintext().build();
}
EmbeddingGateway.ValidateCredentialRequest.Builder validateCredentialRequest =
EmbeddingGateway.ValidateCredentialRequest.newBuilder();
validateCredentialRequest.setCredential(value);
validateCredentialRequest.setTenantId(dataApiRequestInfo.getTenantId().orElse("default"));
validateCredentialRequest.setProviderName(provider);
validateCredentialRequest.setToken(dataApiRequestInfo.getCassandraToken().orElse(""));

EmbeddingServiceGrpc.EmbeddingServiceBlockingStub embeddingService =
EmbeddingServiceGrpc.newBlockingStub(channel);
final EmbeddingGateway.ValidateCredentialResponse validateCredentialResponse =
embeddingService.validateCredential(validateCredentialRequest.build());
if (validateCredentialResponse.hasError()) {
Expand All @@ -31,4 +44,8 @@ public boolean validate(String provider, String value) {
}
return validateCredentialResponse.getValidity();
}

void onStop(@Observes ShutdownEvent ev) {
channel.shutdown();
}
}

0 comments on commit 5da46e7

Please sign in to comment.