io.grpc.ManagedChannelBuilder Scala Examples
The following examples show how to use io.grpc.ManagedChannelBuilder.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
Example 1
Source File: ClientSpec.scala From gemini with GNU General Public License v3.0 | 5 votes |
package tech.sourced.featurext; import io.grpc.{ManagedChannel, ManagedChannelBuilder} import org.scalatest.{BeforeAndAfterAll, FlatSpec, Matchers} import tech.sourced.featurext.generated.service._ import gopkg.in.bblfsh.sdk.v1.uast.generated.Node import java.nio.file.{Files, Paths} @tags.FEIntegration class ClientSpec extends FlatSpec with Matchers with BeforeAndAfterAll { val serverHost = "localhost" val serverPort = 9001 val fixturePath = "src/test/resources/protomsgs/server.py.proto" var channel: ManagedChannel = _ var blockingStub: FeatureExtractorGrpc.FeatureExtractorBlockingStub = _ var uast: Node = _ override def beforeAll(): Unit = { super.beforeAll() val byteArray: Array[Byte] = Files.readAllBytes(Paths.get(fixturePath)) uast = Node.parseFrom(byteArray) channel = ManagedChannelBuilder.forAddress(serverHost, serverPort).usePlaintext(true).build() blockingStub = FeatureExtractorGrpc.blockingStub(channel) } override def afterAll(): Unit = { channel.shutdownNow() } "identifiers call" should "return correct response" in { val request = IdentifiersRequest(uast=Some(uast), options=Some(IdentifiersOptions(docfreqThreshold=5))) val reply = blockingStub.identifiers(request) var features = reply.features.sortBy(_.name) // check correct shape of response features.size should be(49) features(0).name should be("i.ArgumentParser") features(0).weight should be(1) } "literals call" should "return correct response" in { val request = LiteralsRequest(uast=Some(uast), options=Some(LiteralsOptions(docfreqThreshold=5))) val reply = blockingStub.literals(request) var features = reply.features.sortBy(_.name) // check correct shape of response features.size should be(16) features(0).name should be("l.149420d2b7f04801") features(0).weight should be(1) } "uast2seq call" should "return correct response" in { val request = Uast2seqRequest(uast=Some(uast), options=Some(Uast2seqOptions(docfreqThreshold=5))) val reply = blockingStub.uast2Seq(request) var features = reply.features.sortBy(_.name) // check correct shape of response features.size should be(207) features(0).name should be("s.Assign>Name>Attribute>Call>Expr") features(0).weight should be(1) } "graphlet call" should "return correct response" in { val request = GraphletRequest(uast=Some(uast), options=Some(GraphletOptions(docfreqThreshold=5))) val reply = blockingStub.graphlet(request) var features = reply.features.sortBy(_.name) // check correct shape of response features.size should be(106) features(1).name should be("g.Assign_Call_Attribute") features(0).weight should be(1) } }
Example 2
Source File: ManagedChannelBuilderSyntax.scala From fs2-grpc with MIT License | 5 votes |
package org.lyranthe.fs2_grpc package java_runtime package syntax import cats.effect._ import fs2.Stream import io.grpc.{ManagedChannel, ManagedChannelBuilder} import java.util.concurrent.TimeUnit import scala.concurrent._ trait ManagedChannelBuilderSyntax { implicit final def fs2GrpcSyntaxManagedChannelBuilder(builder: ManagedChannelBuilder[_]): ManagedChannelBuilderOps = new ManagedChannelBuilderOps(builder) } final class ManagedChannelBuilderOps(val builder: ManagedChannelBuilder[_]) extends AnyVal { def streamWithShutdown[F[_]](shutdown: ManagedChannel => F[Unit])(implicit F: Sync[F]): Stream[F, ManagedChannel] = Stream.resource(resourceWithShutdown(shutdown)) }
Example 3
Source File: GrpcTraceReaders.scala From haystack-traces with Apache License 2.0 | 5 votes |
package com.expedia.www.haystack.trace.reader.stores.readers.grpc import com.expedia.open.tracing.api.Trace import com.expedia.open.tracing.backend.{ReadSpansRequest, StorageBackendGrpc} import com.expedia.www.haystack.commons.metrics.MetricsSupport import com.expedia.www.haystack.trace.commons.config.entities.TraceStoreBackends import com.expedia.www.haystack.trace.reader.exceptions.TraceNotFoundException import com.expedia.www.haystack.trace.reader.metrics.AppMetricNames import com.expedia.www.haystack.trace.reader.readers.utils.TraceMerger import io.grpc.{ManagedChannel, ManagedChannelBuilder} import org.slf4j.LoggerFactory import scala.collection.JavaConverters._ import scala.concurrent.{ExecutionContextExecutor, Future, Promise} class GrpcTraceReaders(config: TraceStoreBackends) (implicit val dispatcher: ExecutionContextExecutor) extends MetricsSupport with AutoCloseable { private val LOGGER = LoggerFactory.getLogger(classOf[GrpcTraceReaders]) private val readTimer = metricRegistry.timer(AppMetricNames.BACKEND_READ_TIME) private val readFailures = metricRegistry.meter(AppMetricNames.BACKEND_READ_FAILURES) private val tracesFailures = metricRegistry.meter(AppMetricNames.BACKEND_TRACES_FAILURE) private val clients: Seq[GrpcChannelClient] = config.backends.map { backend => { val channel = ManagedChannelBuilder .forAddress(backend.host, backend.port) .usePlaintext(true) .build() val client = StorageBackendGrpc.newFutureStub(channel) GrpcChannelClient(channel, client) } } def readTraces(traceIds: List[String]): Future[Seq[Trace]] = { val allFutures = clients.map { client => readTraces(traceIds, client.stub) recoverWith { case _: Exception => Future.successful(Seq.empty[Trace]) } } Future.sequence(allFutures) .map(traceSeq => traceSeq.flatten) .map { traces => if (traces.isEmpty) throw new TraceNotFoundException() else TraceMerger.merge(traces) } } private def readTraces(traceIds: List[String], client: StorageBackendGrpc.StorageBackendFutureStub): Future[Seq[Trace]] = { val timer = readTimer.time() val promise = Promise[Seq[Trace]] try { val readSpansRequest = ReadSpansRequest.newBuilder().addAllTraceIds(traceIds.asJavaCollection).build() val futureResponse = client.readSpans(readSpansRequest) futureResponse.addListener(new ReadSpansResponseListener( futureResponse, promise, timer, readFailures, tracesFailures, traceIds.size), dispatcher) // return the future with the results for the given client promise.future } catch { case ex: Exception => readFailures.mark() timer.stop() LOGGER.error("Failed to read raw traces with exception", ex) Future.failed(ex) } } override def close(): Unit = { clients.foreach(_.channel.shutdown()) } case class GrpcChannelClient(channel: ManagedChannel, stub: StorageBackendGrpc.StorageBackendFutureStub) }
Example 4
Source File: BaseIntegrationTestSpec.scala From haystack-traces with Apache License 2.0 | 5 votes |
package com.expedia.www.haystack.trace.storage.backends.memory.integration import java.util.UUID import java.util.concurrent.Executors import com.expedia.open.tracing.Span import com.expedia.open.tracing.backend.StorageBackendGrpc import com.expedia.open.tracing.buffer.SpanBuffer import com.expedia.www.haystack.trace.storage.backends.memory.Service import io.grpc.ManagedChannelBuilder import org.scalatest._ import scala.collection.JavaConverters._ trait BaseIntegrationTestSpec extends FunSpec with GivenWhenThen with Matchers with BeforeAndAfterAll with BeforeAndAfterEach { protected var client: StorageBackendGrpc.StorageBackendBlockingStub = _ private val executors = Executors.newSingleThreadExecutor() override def beforeAll() { executors.submit(new Runnable { override def run(): Unit = Service.main(null) }) Thread.sleep(5000) client = StorageBackendGrpc.newBlockingStub(ManagedChannelBuilder.forAddress("localhost", 8090) .usePlaintext(true) .build()) } protected def createSerializedSpanBuffer(traceId: String = UUID.randomUUID().toString, spanId: String = UUID.randomUUID().toString, serviceName: String = "test-service", operationName: String = "test-operation", tags: Map[String, String] = Map.empty, startTime: Long = System.currentTimeMillis() * 1000, sleep: Boolean = true): Array[Byte] = { val spanBuffer = createSpanBufferWithSingleSpan(traceId, spanId, serviceName, operationName, tags, startTime) spanBuffer.toByteArray } private def createSpanBufferWithSingleSpan(traceId: String, spanId: String, serviceName: String, operationName: String, tags: Map[String, String], startTime: Long) = { val spanTags = tags.map(tag => com.expedia.open.tracing.Tag.newBuilder().setKey(tag._1).setVStr(tag._2).build()) SpanBuffer .newBuilder() .setTraceId(traceId) .addChildSpans(Span .newBuilder() .setTraceId(traceId) .setSpanId(spanId) .setOperationName(operationName) .setServiceName(serviceName) .setStartTime(startTime) .addAllTags(spanTags.asJava) .build()) .build() } }
Example 5
Source File: GRPCClient.scala From NSDb with Apache License 2.0 | 5 votes |
package io.radicalbit.nsdb.client.rpc import java.util.concurrent.TimeUnit import io.grpc.{ManagedChannel, ManagedChannelBuilder} import io.radicalbit.nsdb.rpc.health.{HealthCheckRequest, HealthCheckResponse, HealthGrpc} import io.radicalbit.nsdb.rpc.init._ import io.radicalbit.nsdb.rpc.request.RPCInsert import io.radicalbit.nsdb.rpc.requestCommand.{DescribeMetric, ShowMetrics, ShowNamespaces} import io.radicalbit.nsdb.rpc.requestSQL.SQLRequestStatement import io.radicalbit.nsdb.rpc.response.RPCInsertResult import io.radicalbit.nsdb.rpc.responseCommand.{DescribeMetricResponse, MetricsGot, Namespaces} import io.radicalbit.nsdb.rpc.responseSQL.SQLStatementResponse import io.radicalbit.nsdb.rpc.restore.{RestoreGrpc, RestoreRequest, RestoreResponse} import io.radicalbit.nsdb.rpc.service.{NSDBServiceCommandGrpc, NSDBServiceSQLGrpc} import org.slf4j.LoggerFactory import scala.concurrent.Future class GRPCClient(host: String, port: Int) { private val log = LoggerFactory.getLogger(classOf[GRPCClient]) private val channel: ManagedChannel = ManagedChannelBuilder.forAddress(host, port).usePlaintext().build private val stubHealth = HealthGrpc.stub(channel) private val stubRestore = RestoreGrpc.stub(channel) private val stubSql = NSDBServiceSQLGrpc.stub(channel) private val stubCommand = NSDBServiceCommandGrpc.stub(channel) private val stubInit = InitMetricGrpc.stub(channel) def checkConnection(): Future[HealthCheckResponse] = { log.debug("checking connection") stubHealth.check(HealthCheckRequest("whatever")) } def restore(request: RestoreRequest): Future[RestoreResponse] = { log.debug("creating dump") stubRestore.restore(request) } def initMetric(request: InitMetricRequest): Future[InitMetricResponse] = { log.debug("Preparing a init request for {}", request) stubInit.initMetric(request) } def write(request: RPCInsert): Future[RPCInsertResult] = { log.debug("Preparing a write request for {}...", request) stubSql.insertBit(request) } def executeSQLStatement(request: SQLRequestStatement): Future[SQLStatementResponse] = { log.debug("Preparing execution of SQL request: {} ", request.statement) stubSql.executeSQLStatement(request) } def showNamespaces(request: ShowNamespaces): Future[Namespaces] = { log.debug("Preparing of command show namespaces") stubCommand.showNamespaces(request) } def showMetrics(request: ShowMetrics): Future[MetricsGot] = { log.debug("Preparing of command show metrics for namespace: {} ", request.namespace) stubCommand.showMetrics(request) } def describeMetric(request: DescribeMetric): Future[DescribeMetricResponse] = { log.debug("Preparing of command describe metric for namespace: {} ", request.namespace) stubCommand.describeMetric(request) } def close(): Unit = channel.shutdownNow().awaitTermination(10, TimeUnit.SECONDS) }
Example 6
Source File: KsmGrpcServer.scala From kafka-security-manager with MIT License | 5 votes |
package com.github.simplesteph.ksm.grpc import com.github.simplesteph.ksm.AclSynchronizer import com.security.kafka.pb.ksm.{KsmServiceGrpc, KsmServiceHandler} import grpcgateway.server.{GrpcGatewayServer, GrpcGatewayServerBuilder} import io.grpc.protobuf.services.ProtoReflectionService import io.grpc.{ManagedChannelBuilder, Server, ServerBuilder} import org.slf4j.LoggerFactory import scala.concurrent.ExecutionContext class KsmGrpcServer( aclSynchronizer: AclSynchronizer, port: Int, gatewayPort: Int, enabled: Boolean ) { val log = LoggerFactory.getLogger(KsmServiceGrpc.getClass) private[this] var server: Server = _ private[this] var gateway: GrpcGatewayServer = _ private val ec = ExecutionContext.global def start(): Unit = { if (enabled) { log.info("Starting gRPC Server") server = ServerBuilder .forPort(port) .addService(ProtoReflectionService.newInstance()) .addService( KsmServiceGrpc.bindService(new KsmServiceImpl(aclSynchronizer), ec) ) .build() server.start() log.info(s"gRPC Server started on port $port") log.info("Starting gRPC Gateway") // internal client for gateway val channel = ManagedChannelBuilder .forAddress("localhost", port) .usePlaintext() .build() // gateway (REST) gateway = GrpcGatewayServerBuilder .forPort(gatewayPort) .addService(new KsmServiceHandler(channel)(ec)) .build() gateway.start() log.info(s"gRPC Server started on port $gatewayPort") } } def stop(): Unit = { if (enabled) { server.shutdown() gateway.shutdown() } } }
Example 7
Source File: OnlineActionProxy.scala From marvin-engine-executor with Apache License 2.0 | 5 votes |
package org.marvin.executor.proxies import actions.OnlineActionHandlerGrpc.{OnlineActionHandler, OnlineActionHandlerBlockingClient, OnlineActionHandlerBlockingStub, OnlineActionHandlerStub} import actions._ import akka.pattern.pipe import io.grpc.ManagedChannelBuilder import org.marvin.executor.proxies.EngineProxy.{ExecuteOnline, HealthCheck, Reload} import org.marvin.model.EngineActionMetadata //Reload messages final case class Reloaded(protocol: String) final case class FailedToReload(protocol: String = "") class OnlineActionProxy(metadata: EngineActionMetadata) extends EngineProxy (metadata) { var engineAsyncClient:OnlineActionHandler = _ var engineClient:OnlineActionHandlerBlockingClient = _ implicit val ec = context.dispatcher override def preStart() = { log.info(s"${this.getClass().getCanonicalName} actor initialized...") val channel = ManagedChannelBuilder.forAddress(metadata.host, metadata.port).usePlaintext(true).build artifacts = metadata.artifactsToLoad.mkString(",") engineAsyncClient = OnlineActionHandlerGrpc.stub(channel) engineClient = OnlineActionHandlerGrpc.blockingStub(channel) } override def receive = { case ExecuteOnline(requestMessage, params) => log.info(s"Start the execute remote procedure to ${metadata.name}.") val responseFuture = engineAsyncClient.RemoteExecute(OnlineActionRequest(message=requestMessage, params=params)) responseFuture.collect{case response => response.message} pipeTo sender case HealthCheck => log.info(s"Start the health check remote procedure to ${metadata.name}.") val statusFuture = engineAsyncClient.HealthCheck(HealthCheckRequest(artifacts=artifacts)) statusFuture.collect{case response => response.status} pipeTo sender case Reload(protocol) => log.info(s"Start the reload remote procedure to ${metadata.name}. Protocol [$protocol]") try{ val message = engineClient.RemoteReload(ReloadRequest(artifacts=artifacts, protocol=protocol)).message log.info(s"Reload remote procedure to ${metadata.name} Done with [${message}]. Protocol [$protocol]") sender ! Reloaded(protocol) } catch { case _ : Exception => sender ! FailedToReload(protocol) } case _ => log.warning(s"Not valid message !!") } }
Example 8
Source File: BatchActionProxy.scala From marvin-engine-executor with Apache License 2.0 | 5 votes |
package org.marvin.executor.proxies import actions.BatchActionHandlerGrpc.BatchActionHandlerBlockingClient import actions.{BatchActionHandlerGrpc, BatchActionRequest, HealthCheckRequest, ReloadRequest} import akka.Done import io.grpc.ManagedChannelBuilder import org.marvin.executor.proxies.EngineProxy.{ExecuteBatch, HealthCheck, Reload} import org.marvin.model.EngineActionMetadata class BatchActionProxy(metadata: EngineActionMetadata) extends EngineProxy (metadata) { var engineClient:BatchActionHandlerBlockingClient = _ override def preStart() = { log.info(s"${this.getClass().getCanonicalName} actor initialized...") val channel = ManagedChannelBuilder.forAddress(metadata.host, metadata.port).usePlaintext(true).build artifacts = metadata.artifactsToLoad.mkString(",") engineClient = BatchActionHandlerGrpc.blockingStub(channel) } override def receive = { case ExecuteBatch(protocol, params) => log.info(s"Start the execute remote procedure to ${metadata.name}. Protocol [$protocol]") val message = engineClient.RemoteExecute(BatchActionRequest(params=params)).message log.info(s"Execute remote procedure to ${metadata.name} Done with [${message}]. Protocol [$protocol]") sender ! Done case HealthCheck => log.info(s"Start the health check remote procedure to ${metadata.name}.") val status = engineClient.HealthCheck(HealthCheckRequest(artifacts=artifacts)).status log.info(s"Health check remote procedure to ${metadata.name} Done with [${status}].") sender ! status case Reload(protocol) => log.info(s"Start the reload remote procedure to ${metadata.name}. Protocol [$protocol].") val message = engineClient.RemoteReload(ReloadRequest(artifacts=artifacts, protocol=protocol)).message log.info(s"Reload remote procedure to ${metadata.name} Done with [${message}]. Protocol [$protocol].") sender ! Done case _ => log.warning(s"Not valid message !!") } }
Example 9
Source File: RouteGuideRestGateway.scala From grpcexample with MIT License | 5 votes |
package io.grpc.routeguide import java.util.logging.Logger import grpcgateway.server.GrpcGatewayServerBuilder import io.grpc.ManagedChannelBuilder import scala.concurrent.ExecutionContext import scala.sys.ShutdownHookThread class RouteGuideRestGateway(port: Int, grpcHost: String, grpcPort: Int)(implicit ec: ExecutionContext) { private val logger: Logger = Logger.getLogger(classOf[RouteGuideServer].getName) private val channel = ManagedChannelBuilder .forAddress(grpcHost, grpcPort) .usePlaintext(true) .build() private val gateway = GrpcGatewayServerBuilder .forPort(port) .addService(new RouteGuideHandler(channel)) .build() private var shutdownHook: Option[ShutdownHookThread] = None def start(): Unit = { gateway.start() logger.info(s"GRPC Gateway started, listening on $port") shutdownHook = Option( sys.addShutdownHook { // Use stderr here since the logger may has been reset by its JVM shutdown hook. System.err.println("*** shutting down gRPC gateway since JVM is shutting down") stop() System.err.println("*** gRPC Gateway shut down") } ) } def stop(): Unit = gateway.shutdown() def blockUntilShutdown(): Unit = shutdownHook.foreach(_.join()) } object RouteGuideRestGateway extends App { import scala.concurrent.ExecutionContext.Implicits.global // Expects GRPC server to be running on localhost:8980 val gateway = new RouteGuideRestGateway(8981, "localhost", 8980) gateway.start() gateway.blockUntilShutdown() }
Example 10
Source File: Node.scala From Waves with MIT License | 5 votes |
package com.wavesplatform.it import java.net.{InetSocketAddress, URL} import java.util.concurrent.TimeUnit import com.typesafe.config.Config import com.wavesplatform.account.{KeyPair, PublicKey} import com.wavesplatform.common.utils.EitherExt2 import com.wavesplatform.it.util.GlobalTimer import com.wavesplatform.settings.WavesSettings import com.wavesplatform.state.diffs.FeeValidation import com.wavesplatform.utils.LoggerFacade import io.grpc.{ManagedChannel, ManagedChannelBuilder} import org.asynchttpclient.Dsl.{config => clientConfig, _} import org.asynchttpclient._ import org.slf4j.LoggerFactory import scala.concurrent.duration.FiniteDuration abstract class Node(val config: Config) extends AutoCloseable { lazy val log: LoggerFacade = LoggerFacade(LoggerFactory.getLogger(s"${getClass.getCanonicalName}.${this.name}")) val settings: WavesSettings = WavesSettings.fromRootConfig(config) val client: AsyncHttpClient = asyncHttpClient( clientConfig() .setKeepAlive(false) .setNettyTimer(GlobalTimer.instance)) lazy val grpcChannel: ManagedChannel = ManagedChannelBuilder.forAddress(networkAddress.getHostString, nodeExternalPort(6870)) .usePlaintext() .keepAliveWithoutCalls(true) .keepAliveTime(30, TimeUnit.SECONDS) .build() val keyPair: KeyPair = KeyPair.fromSeed(config.getString("account-seed")).explicitGet() val publicKey: PublicKey = PublicKey.fromBase58String(config.getString("public-key")).explicitGet() val address: String = config.getString("address") def nodeExternalPort(internalPort: Int): Int def nodeApiEndpoint: URL def apiKey: String def networkAddress: InetSocketAddress override def close(): Unit = client.close() } object Node { implicit class NodeExt(val n: Node) extends AnyVal { def name: String = n.settings.networkSettings.nodeName def publicKeyStr: String = n.publicKey.toString def fee(txTypeId: Byte): Long = FeeValidation.FeeConstants(txTypeId) * FeeValidation.FeeUnit def blockDelay: FiniteDuration = n.settings.blockchainSettings.genesisSettings.averageBlockDelay } }
Example 11
Source File: JaegerTracer.scala From zio-telemetry with Apache License 2.0 | 5 votes |
package zio.telemetry.opentelemetry.example import io.grpc.ManagedChannelBuilder import io.opentelemetry.exporters.jaeger.JaegerGrpcSpanExporter import io.opentelemetry.sdk.OpenTelemetrySdk import io.opentelemetry.trace.Tracer import zio._ import zio.telemetry.opentelemetry.example.config.{ Config, Configuration } object JaegerTracer { def live(serviceName: String): RLayer[Configuration, Has[Tracer]] = ZLayer.fromServiceM((conf: Config) => for { tracer <- UIO(OpenTelemetrySdk.getTracerProvider.get("zio.telemetry.opentelemetry.example.JaegerTracer")) managedChannel <- Task(ManagedChannelBuilder.forTarget(conf.tracer.host).usePlaintext().build()) _ <- UIO( JaegerGrpcSpanExporter .newBuilder() .setServiceName(serviceName) .setChannel(managedChannel) .install(OpenTelemetrySdk.getTracerProvider) ) } yield tracer ) }
Example 12
Source File: HelloWorldClient.scala From grpc-scala-sample with BSD 3-Clause "New" or "Revised" License | 5 votes |
package io.grpc.examples.helloworld import java.util.concurrent.TimeUnit import java.util.logging.{Level, Logger} import io.grpc.examples.helloworld.helloworld.{HelloRequest, GreeterGrpc} import io.grpc.examples.helloworld.helloworld.GreeterGrpc.GreeterBlockingStub import io.grpc.{StatusRuntimeException, ManagedChannelBuilder, ManagedChannel} def greet(name: String): Unit = { logger.info("Will try to greet " + name + " ...") val request = HelloRequest(name = name) try { val response = blockingStub.sayHello(request) logger.info("Greeting: " + response.message) } catch { case e: StatusRuntimeException => logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus) } } }