Skip to content

Commit 0f86094

Browse files
committed
grpc-native: Resolve review requests
Signed-off-by: Johannes Zottele <[email protected]>
1 parent c60a41d commit 0f86094

File tree

6 files changed

+34
-32
lines changed

6 files changed

+34
-32
lines changed

grpc/grpc-core/build.gradle.kts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,13 @@ kotlin {
4141

4242
val grpcppCLib = projectDir.resolve("../grpcpp-c")
4343

44+
// TODO: Replace function implementation, so it does not use an internal API
4445
fun findProgram(name: String) = org.gradle.internal.os.OperatingSystem.current().findInPath(name)
4546
val checkBazel by tasks.registering {
4647
doLast {
4748
val bazelPath = findProgram("bazel")
4849
if (bazelPath != null) {
49-
println("bazel: $bazelPath")
50+
logger.debug("bazel: {}", bazelPath)
5051
} else {
5152
throw GradleException("'bazel' not found on PATH. Please install Bazel (https://bazel.build/).")
5253
}
@@ -73,7 +74,7 @@ kotlin {
7374
grpcppCLib.resolve("bazel-grpcpp-c/external/grpc+/include")
7475
)
7576
extraOpts(
76-
"-libraryPath", "${grpcppCLib.resolve("bazel-out/darwin_arm64-opt/bin")}",
77+
"-libraryPath", "${grpcppCLib.resolve("bazel-out/darwin_arm64-opt/bin")}",
7778
)
7879
}
7980

grpc/grpc-core/gradle.properties

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#
22
# Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
33
#
4-
54
kotlinx.rpc.exclude.wasmWasi=true
65
kotlinx.rpc.exclude.iosArm64=true
76
kotlinx.rpc.exclude.iosX64=true

grpc/grpc-core/src/nativeMain/kotlin/kotlinx/rpc/grpc/bridge/GrpcByteBuffer.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,22 @@ import libgrpcpp_c.*
1010
@OptIn(ExperimentalForeignApi::class)
1111
internal class GrpcByteBuffer internal constructor(
1212
internal val cByteBuffer: CPointer<grpc_byte_buffer>
13-
): AutoCloseable {
13+
) : AutoCloseable {
1414

15-
constructor(slice: GrpcSlice): this(memScoped {
15+
constructor(slice: GrpcSlice) : this(memScoped {
1616
grpc_raw_byte_buffer_create(slice.cSlice, 1u) ?: error("Failed to create byte buffer")
1717
})
1818

1919
fun intoSlice(): GrpcSlice {
2020
memScoped {
21-
val resp_slice = alloc<grpc_slice>()
22-
grpc_byte_buffer_dump_to_single_slice(cByteBuffer, resp_slice.ptr)
23-
return GrpcSlice(resp_slice.readValue())
21+
val respSlice = alloc<grpc_slice>()
22+
grpc_byte_buffer_dump_to_single_slice(cByteBuffer, respSlice.ptr)
23+
return GrpcSlice(respSlice.readValue())
2424
}
2525
}
2626

2727
override fun close() {
2828
grpc_byte_buffer_destroy(cByteBuffer)
2929
}
3030

31-
}
31+
}

grpc/grpc-core/src/nativeMain/kotlin/kotlinx/rpc/grpc/bridge/GrpcClient.kt

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ package kotlinx.rpc.grpc.bridge
66

77
import kotlinx.cinterop.*
88
import kotlinx.coroutines.suspendCancellableCoroutine
9+
import libgrpcpp_c.*
910
import kotlin.coroutines.resume
1011
import kotlin.coroutines.resumeWithException
11-
import libgrpcpp_c.*
12-
1312

1413
@OptIn(ExperimentalForeignApi::class)
15-
internal class GrpcClient(target: String): AutoCloseable {
16-
private var clientPtr: CPointer<grpc_client_t> = grpc_client_create_insecure(target) ?: error("Failed to create client")
14+
internal class GrpcClient(target: String) : AutoCloseable {
15+
private var clientPtr: CPointer<grpc_client_t> =
16+
grpc_client_create_insecure(target) ?: error("Failed to create client")
1717

1818
fun callUnaryBlocking(method: String, req: GrpcSlice): GrpcSlice {
1919
memScoped {
@@ -23,45 +23,48 @@ internal class GrpcClient(target: String): AutoCloseable {
2323
}
2424
}
2525

26-
suspend fun callUnary(method: String, req: GrpcByteBuffer): GrpcByteBuffer = suspendCancellableCoroutine { continuation ->
26+
suspend fun callUnary(method: String, req: GrpcByteBuffer): GrpcByteBuffer =
27+
suspendCancellableCoroutine { continuation ->
2728
val context = grpc_context_create()
2829
val method = grpc_method_create(method)
2930

30-
val req_raw_buf = nativeHeap.alloc<CPointerVar<grpc_byte_buffer>>()
31-
req_raw_buf.value = req.cByteBuffer
31+
val reqRawBuf = nativeHeap.alloc<CPointerVar<grpc_byte_buffer>>()
32+
reqRawBuf.value = req.cByteBuffer
3233

33-
val resp_raw_buf: CPointerVar<grpc_byte_buffer> = nativeHeap.alloc()
34+
val respRawBuf: CPointerVar<grpc_byte_buffer> = nativeHeap.alloc()
3435

3536
val continueCb = { st: grpc_status_code_t ->
3637
// cleanup allocations owned by this method (this runs always)
3738
grpc_method_delete(method)
3839
grpc_context_delete(context)
39-
nativeHeap.free(req_raw_buf)
40+
nativeHeap.free(reqRawBuf)
4041

4142
if (st != GRPC_C_STATUS_OK) {
4243
continuation.resumeWithException(RuntimeException("Call failed with code: $st"))
4344
} else {
44-
val result = resp_raw_buf.value
45+
val result = respRawBuf.value
4546
if (result == null) {
4647
continuation.resumeWithException(RuntimeException("No response received"))
4748
} else {
4849
continuation.resume(GrpcByteBuffer(result))
4950
}
5051
}
5152

52-
nativeHeap.free(resp_raw_buf)
53+
nativeHeap.free(respRawBuf)
5354
}
5455
val cbCtxStable = StableRef.create(continueCb)
5556

56-
grpc_client_call_unary_callback(clientPtr, method, context, req_raw_buf.ptr, resp_raw_buf.ptr, cbCtxStable.asCPointer(), staticCFunction { st, ctx ->
57-
val cbCtxStable = ctx!!.asStableRef<(grpc_status_code_t) -> Unit>()
58-
cbCtxStable.get()(st)
59-
cbCtxStable.dispose()
60-
})
61-
}
57+
grpc_client_call_unary_callback(
58+
clientPtr, method, context, reqRawBuf.ptr, respRawBuf.ptr,
59+
cbCtxStable.asCPointer(), staticCFunction { st, ctx ->
60+
val cbCtxStable = ctx!!.asStableRef<(grpc_status_code_t) -> Unit>()
61+
cbCtxStable.get()(st)
62+
cbCtxStable.dispose()
63+
})
64+
}
6265

6366
override fun close() {
6467
grpc_client_delete(clientPtr)
6568
}
6669

67-
}
70+
}

grpc/grpc-core/src/nativeMain/kotlin/kotlinx/rpc/grpc/bridge/GrpcSlice.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import libgrpcpp_c.grpc_slice
1212
import libgrpcpp_c.grpc_slice_from_copied_buffer
1313
import libgrpcpp_c.grpc_slice_unref
1414

15-
1615
@OptIn(ExperimentalForeignApi::class)
1716
internal class GrpcSlice internal constructor(internal val cSlice: CValue<grpc_slice>) : AutoCloseable {
1817

@@ -25,4 +24,4 @@ internal class GrpcSlice internal constructor(internal val cSlice: CValue<grpc_s
2524
override fun close() {
2625
grpc_slice_unref(cSlice)
2726
}
28-
}
27+
}

grpc/grpc-core/src/nativeTest/kotlin/kotlinx/rpc/grpc/BridgeTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ import kotlinx.coroutines.runBlocking
88
import kotlinx.rpc.grpc.bridge.GrpcByteBuffer
99
import kotlinx.rpc.grpc.bridge.GrpcClient
1010
import kotlinx.rpc.grpc.bridge.GrpcSlice
11+
import libgrpcpp_c.pb_decode_greeter_sayhello_response
1112
import kotlin.test.Test
12-
import libgrpcpp_c.*
1313

1414
@OptIn(kotlinx.cinterop.ExperimentalForeignApi::class)
1515
class BridgeTest {
1616

1717
@Test
18-
fun `test basic unary async call`() {
18+
fun testBasicUnaryAsyncCall() {
1919
runBlocking {
2020
GrpcClient("localhost:50051").use { client ->
2121
GrpcSlice(byteArrayOf(8, 4)).use { request ->
@@ -30,7 +30,7 @@ class BridgeTest {
3030
}
3131
}
3232
}
33-
}
33+
}
3434
}
3535
}
3636
}

0 commit comments

Comments
 (0)