https://kotlinlang.org logo
Join SlackCommunities
Powered by
# grpc
  • c

    Cristian Nicolas Gonzalez

    04/15/2022, 10:25 PM
    Hi! Does someone have a minimal setup to gradle kotlin grpc? I tried this https://github.com/grpc/grpc-kotlin/blob/master/compiler/README.md but it doesn’t work because it miss coroutines depedency and also I have this error
    @io.grpc.stub.annotations.GrpcGenerated
    ^
    symbol:   class GrpcGenerated
    location: package io.grpc.stub.annotations
    d
    k
    d
    • 4
    • 7
  • j

    Júlio Santos

    05/12/2022, 1:12 AM
    Guys, can I use the lib
    grpc-kotlin
    for web projects?
    j
    d
    • 3
    • 2
  • t

    taer

    06/24/2022, 9:36 PM
    Hey y'all. I notice the stubs generated have the headers as an optional parameter to the service method. I really want to send response headers/trailers from the stub code. It seems like the way is via interceptors and threadlocals and coroutineContexts so that mutable things in the threadlocal get updated. That seems really painful to just add a response header. Am I missing something to have my stubbs return a Pair of <RESP, Metadata> ?
  • t

    taer

    06/24/2022, 9:38 PM
    I did the threadlocal version. I added an AtmonicReference<String> as a threadLocal/contextElement. In the stubb, I get the Threadlocal and set the value. In the interceptor, I get the threadLocal then get the value and set it. But.. That's a lot of pain, and I'm not even sure the "post interceptor" is even running on the same thread after the "stub service method" runs.
    m
    • 2
    • 3
  • d

    davec

    06/27/2022, 9:03 PM
    Is there any decent documentation on how to create a gRPC request? It seems like everything uses the DSL stuff which I can't find any documentation for and isn't particularly intuitive:
    Copy code
    val request = rectangle {
                lo = point(lowLat, lowLon)
                hi = point(hiLat, hiLon)
            }
            var i = 1
            stub.listFeatures(request).
    ☝️ that looks easy in theory but try it with a nontrivial request and it becomes near impossible to come up with the right syntax. Seems like sometimes things are a "DSL code block" and other time they're done like constructors? It would be nice if I could just construct the request like a
    data class
    instead of all this stuff
    d
    e
    • 3
    • 6
  • d

    davec

    06/27/2022, 9:05 PM
    as an example how would I build this?
    Copy code
    message DateRange {
      // The beginning of the date range
      google.type.Date from = 1 [(validate.rules).message.required = true];
    
      // The end of the date range
      google.type.Date until = 2 [(validate.rules).message.required = true];
    }
    d
    m
    • 3
    • 11
  • d

    David Reedy

    07/09/2022, 7:27 AM
    Hi @everybody
  • d

    David Reedy

    07/09/2022, 7:27 AM
    Anyone have experience with Kotlin-gRPC?
    • 1
    • 1
  • d

    davec

    07/11/2022, 7:20 PM
    Question: right now my application (which includes gRPC code) builds fine from the command-line (
    gradlew bootJar
    ). Can build, run, everything works fine.
  • d

    davec

    07/11/2022, 7:20 PM
    Yet IntelliJ is showing compile errors for some of the generated gRPC code. Has anyone experienced this?
    d
    • 2
    • 5
  • d

    davec

    07/27/2022, 2:57 PM
    Is it true that if you use gRPC with Kotlin, the request/response classes are still in Java? It kind of seems that way, here's a line from the docs (https://grpc.io/docs/languages/kotlin/basics/#simple-rpc-1):
  • d

    davec

    07/27/2022, 2:59 PM
    If this is true, all the fields in the data model are platform types, which means no null safety unless you explicitly cast each one of them to a nullable type -- which is pretty bad for null safety. If Java is what's generated it would be nice if it at least marked everything nullable with an annotation (https://kotlinlang.org/docs/java-interop.html#nullability-annotations) just so Kotlin has some idea what to do. Ideally it would be nice if it actually generated Kotlin instead of Java; maybe it does but I'm not seeing it?
    m
    j
    j
    • 4
    • 7
  • a

    Akram Bensalem

    09/12/2022, 2:17 PM
    I want to add protobuf to my kotlin multiplatform project that contain "Android and Desktop" so it's JVM only the plugin by default compiles protobuf source from "src/main/proto" rather than "src/commonMain/proto" Do you know how to fix that issue ?
    m
    • 2
    • 1
  • s

    sarvagya agarwal

    09/25/2022, 8:53 PM
    Copy code
    message File {
       bytes file_bytes = 1;
    }
    message Response {
       string some_response = 1;
    }
    Service UploadService {
       rpc uploadFiles(stream File) returns Response;
    }
    How can I define a service that takes List of Files as a parameter ? The above is fine for a single file.
    s
    • 2
    • 10
  • t

    Thomas Cesare-Herriau

    09/29/2022, 10:40 PM
    Hi all! tl;dr: Has anyone here ever needed to create server-side deadlines? _Background_: The
    grpc-kotlin
    wrapper to
    grpc-java
    already supports setting Deadlines on Client Stubs (using the withDeadline function), however there is no "Kotlin coroutine" equivalent to the io.grpc.Context#withDeadline function that allows wrapping any operation into a Cancellable context with a deadline (a deadline that is then propagated to all gRPC client stubs within the operation!). The problem is that the
    io.grpc.Context#withDeadline
    API requires using a
    ScheduledExecutorService
    , which as I understand it, would require bypassing Kotlin coroutine architecture. An alternative is to use `withTimeout`: however this results in 2 separate contextual timeouts (the
    withTimeout
    one and a potential propagated gRPC Deadline in the context) Has anyone here had to set server-side deadlines/timeouts in the past (in the context of gRPC servers)? If so, what approach have you used?
    e
    • 2
    • 2
  • e

    Endre Deak

    09/30/2022, 3:22 PM
    Hi, what's the right way to tell the protobuf Gradle plugin that it should process some of the proto files from the
    build/extracted-include-protos
    folder? I do see the healthcheck proto there, but not sure how to put that next to my own protos to be generated.
  • s

    sarvagya agarwal

    10/13/2022, 9:45 AM
    Hi , has anyone ever worked with streaming files as a byte array in grpc (Sending file as list of chunks from client and reconstructing it on the server) ? This is what i have on the client side
    Copy code
    val chunk = ByteArray(BYTE_ARRAY_SIZE)
    val inputStream = FileInputStream(file)
    val fileChunks = flow {
    	val metadata = MetaData.newBuilder()
    		.setPath(file.path)
    		.setConvertToPdf(BoolValue.of(convertToPdf))
    		.build()
    	emit(FileUploadRequest.newBuilder().setMetadata(metadata).build())
    	// It works if i dont break the file into chunks 
    	// val fileChunk = FileChunk.newBuilder().setData(ByteString.copyFrom(byteArray)).build()
    	// emit(FileUploadRequest.newBuilder().setContent(fileChunk).build())
    	while(true) {
    		val size = inputStream.read(chunk)
    		if(size <= 0) break
    		val fileChunk = FileChunk.newBuilder()
    			.setData(ByteString.copyFrom(chunk, 0, size)).build()
    		emit(FileUploadRequest.newBuilder().setContent(fileChunk).build())
    	}
    }
    On the server side , i reconstruct the file and process it , the file is constructed properly if i send it all at once instead of breaking it down into smaller chunks. Not sure how to debug this either , does anyone have any idea what i am doing wrong ?
    Copy code
    requests.collect {
    	if (it.hasContent()) {
    		file.writeBytes(it.content.toByteArray())
    	} else {
    		metadata = it.metadata
    		file = File(metadata.path)
    	}
    }
  • x

    xuemin.guan

    11/07/2022, 7:48 PM
    hello everyone 👋😀, a complete newbie to gRPC. What do people use for gRPC testing? What do people use to record and re-play request and response?
    a
    • 2
    • 1
  • a

    Agustin Bonilla

    12/16/2022, 3:43 PM
    Hi everyone! I’m using a library that, as I can see, has a dependency on
    grpc-kotlin-stub
    . As a limitation on my side, I can only add those dependencies on AAR or JAR files. I have found and added the
    grpc-kotlin-stub
    JAR, but when I run my app I’m having the following error: “Java.Lang.NoClassDefFoundError: Failed resolution of: Lio/grpc/kotlin/AbstractCoroutineStub” It looks like haven’t found the class
    AbstractCoroutineStub
    that if I’m right, it should be in the
    grpc-kotlin-stub
    JAR. Does anyone know why it isn’t finding the class
    AbstractCoroutineStub
    ? Is there a
    grpc-kotlin-stub
    AAR version to try?
  • a

    Andrés Romero

    01/25/2023, 6:41 PM
    Hi everyone! I'm looking to implement a long-living server stream with gRPC that clients can subscribe to. Context: there is a group of users with users and a lead user, when the lead starts and action
    performAction()
    which is a unary RPC. I want to notify the other users with information about that action. I would expect those members to be subscribed to another long-living RPC that facilitates sending that information. Do you have any examples of how can achieve that in Kotlin? I was looking into something like: https://dev.bitolog.com/grpc-long-lived-streaming/ https://sultanov.dev/blog/grpc-long-lived-streaming-using-observer-pattern/ But those implementations are written in Go and Java, and I'm struggling to find examples in Kotlin
    🆘 1
    m
    • 2
    • 3
  • f

    Francis Reynders

    03/25/2023, 10:38 AM
    Hello all. I'm implementing a grpc service which needs a request/response rpc call from server to client. I did some research and understand there is no support for this communication pattern. A common way to shoehorn this pattern in the current infrastructure would be to add a correlation id to the request payload (server->client stream) and for the client to respond with an rpc call to the server, repeating that correlation id. My questions: 1) is there a better way? Should I maybe consider a different protocol? 2) Is there any library support for this? I could not find any so far. 3) I consider implementing this myself. I'm thinking about a bidirectionnal grpc pattern where the server->client stream would be a
    ChannelFlow
    . A request/response request from the server -> client could then be initiated by an async coroutine launch that sends the payload with correlation id to the channel and listens with timeout for a respone on a
    SharedChannel
    flow (client->server stream), listening for the first response with that correlation id. Would that work? Thank you
    m
    • 2
    • 1
  • m

    Minsoo Cheong

    04/02/2023, 4:20 PM
    I've just published a new library that makes working with Protocol Buffers and gRPC in Kotlin projects much easier by generating idiomatic Kotlin data classes and gRPC service/stub based on your protobuf definitions, while providing interoperability to classic protobuf java classes. Check out my blog post on Medium for an introduction and guide on how to use krotoDC: https://medium.com/@icycle0409/introducing-krotodc-use-protobuf-and-grpc-with-kotlin-dataclasses-3144d0b20032 Feel free to give it a try and share your feedback! I'd love to hear your thoughts and suggestions for improvements. 🎉 Slack Conversation
    🙌 1
  • j

    Jacob K

    05/05/2023, 11:05 AM
    I'm looking for some advice/insight... I'm having trouble understanding how/if https://github.com/grpc/grpc-java/blob/master/api/src/main/java/io/grpc/Contexts.java#L44 works well together with the kotlin
    GrpcContextElement
    https://github.com/grpc/grpc-kotlin/blob/master/stub/src/main/java/io/grpc/kotlin/GrpcContextElement.kt#L27 It's seems as the initial
    GrpcContextElement
    is created when the server-call is created, and that it is supposed to rely on kotlins coroutine system to ensure the context is available on the current thread (used by the coroutine). But once I create a new context with
    Context.current().withValue(...)
    and pass it on with
    Contexts.interceptCall(...)
    I worry that this newly created context doesn't "inherit" that coroutine specific logic. The current interceptor looks something like this:
    Copy code
    data class DebugInfo(val enabled: Boolean, var debugCtx: DebugContext?)
    
    val DEBUG_KEY = Context.key<DebugInfo>("debug-info")
    
    val DEBUG_INFO_CTX =
        Metadata.Key.of(
            "x-debug-info-ctx${Metadata.BINARY_HEADER_SUFFIX}",
            ProtoUtils.metadataMarshaller(DebugContext.getDefaultInstance()))
    
    class DebugInfoInterceptor : ServerInterceptor {
      override fun <ReqT : Any?, RespT : Any?> interceptCall(
          call: ServerCall<ReqT, RespT>,
          headers: Metadata,
          next: ServerCallHandler<ReqT, RespT>
      ): ServerCall.Listener<ReqT> {
        val debug = headers.get(DEBUG_HEADER)?.let { it == "true" } ?: false
        val debugInfo = DebugInfo(debug, null)
        val debugInfoContext = Context.current().withValue(DEBUG_KEY, debugInfo)
        val debugInfoCall =
            object : ForwardingServerCall.SimpleForwardingServerCall<ReqT, RespT>(call) {
              override fun close(status: Status, trailers: Metadata) {
                debugInfo.debugCtx?.let { trailers.put(DEBUG_INFO_CTX, it) }
                super.close(status, trailers)
              }
            }
    
        return Contexts.interceptCall(debugInfoContext, debugInfoCall, headers, next)
      }
    }
    and the corresponding server stub:
    Copy code
    class SomeService() : SomeServiceGrpcKt.SomeServiceCoroutineImplBase() {
      override suspend fun someRpc(request: SomeRequest): SomeResponse {
        //
        // do suspending stuff
        //
        val debugInfo = DEBUG_KEY.get()
        if (debugInfo.enabled) {
          debugInfo.debugCtx = DebugContext.newBuilder().setSomething().build()
        }
        return SomeResponse().build()
      }
    }
    I'v been digging through the source but I can't convince myself that this is safe 😉
    m
    w
    • 3
    • 3
  • j

    Júlio Santos

    06/05/2023, 5:45 PM
    Hi everyone! Do you recommend using Ktor with gRPC?
    w
    i
    • 3
    • 10
  • j

    Jorge Viana

    07/28/2023, 2:14 PM
    Hello all! I’m working with grpc and kotlin in a spring-boot project. Do you know if there is a way of putting a deadline (per flow element and not entire request, if this makes sense) on a grpc client when calling a stub that returns a kotlin.flow?
    w
    • 2
    • 5
  • a

    Ali

    09/22/2023, 10:30 AM
    Anyone here has experience with gRPC-Protobuf. I was trying to integrate it into my project but having this issue. If anyone has any idea would be helpful 🙏
    Copy code
    e: com.squareup.anvil.compiler.api.AnvilCompilationException: Back-end (JVM) Internal error: Couldn't resolve FqName com.google.protobuf.kotlin.ProtoDslMarker for Psi element: com.google.protobuf.kotlin.ProtoDslMarker
  • a

    Alseddnm

    02/08/2024, 3:08 PM
    Hello QQ regarding kotlin Grpc , this should be very stable with all JDK versions above 1.8 https://github.com/GoogleCloudPlatform/kotlin-samples/blob/main/run/grpc-hello-world-mvn/pom.xml#L102 ? latest release is 1.4.1
    w
    • 2
    • 6
  • c

    Colton Idle

    02/27/2024, 7:27 PM
    This might be a question out of left field... but I'll ask it anyway. So I've never used grpc before, but I feel like I sorta get the point of it. I'm using googles encryption library called tink which apprently saves stuff into a protobuf format (kinda cool), but now all I want to do is grab a "deserialized" version of the public key generated by tink which is "KeyData value is still a binary serialized proto." Can I just put a string value of
    EgYIARABGAIaILk8CE7/WHIslvUnsNk+ZYNVF0XTTwCQzAD8xqk7K81G
    through a protobuf deserializer? I guess i have the proto file so it should be possible? https://github.com/tink-crypto/tink-java/blob/main/proto/tink.proto
    j
    a
    • 3
    • 52
  • m

    Minsoo Cheong

    03/07/2024, 8:54 AM
    krotoDC 1.1.1 is out with json conversion support K 🦜 https://github.com/mscheong01/krotoDC For those who are tired of Java/Kotlin's protobuf's cumbersome null checks - Try krotoDC which supports data class Protobufs, nullable fields, implicit conversions to Kotlin standard library types, and more!
    👍 1
  • d

    David

    08/02/2024, 2:19 PM
    I'm running grpc-java & grpc-kotlin with UDS on android, every now and then we see our channel entering
    TransientFailure
    on Connecting. After setting up
    maxTraceEvent
    we see the following:
    Copy code
    ManagementService connection state: Connecting
    [{0}] Failed to resolve name. status={1}
    ManagementService connection state: TransientFailure
    [{0}] Failed to resolve name. status={1}
    ManagementService connection state: Connecting
    ManagementService connection state: Ready
    Any ideas why we would get
    Failed to resolve name
    ?
    • 1
    • 3