Skip to content

Commit 0d26180

Browse files
committed
grpc-native: Code cleanup
Signed-off-by: Johannes Zottele <[email protected]>
1 parent 8302181 commit 0d26180

File tree

12 files changed

+340
-133
lines changed

12 files changed

+340
-133
lines changed

cinterop-c/MODULE.bazel.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

grpc/grpc-core/src/commonMain/kotlin/kotlinx/rpc/grpc/ServerServiceDefinition.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ import kotlinx.rpc.internal.utils.InternalRpcApi
1616
public expect class ServerServiceDefinition {
1717
public fun getServiceDescriptor(): ServiceDescriptor
1818
public fun getMethods(): Collection<ServerMethodDefinition<*, *>>
19+
20+
public fun getMethod(methodName: String): ServerMethodDefinition<*, *>?
1921
}
2022

2123
@InternalRpcApi
2224
public expect fun serverServiceDefinition(
2325
serviceDescriptor: ServiceDescriptor,
24-
methods: Collection<ServerMethodDefinition<*, *>>
26+
methods: Collection<ServerMethodDefinition<*, *>>,
2527
): ServerServiceDefinition

grpc/grpc-core/src/nativeMain/kotlin/kotlinx/rpc/grpc/MutableHandlerRegistry.native.kt

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,66 @@
66

77
package kotlinx.rpc.grpc
88

9+
import kotlinx.rpc.grpc.internal.MethodDescriptor
10+
import kotlinx.rpc.grpc.internal.ServerMethodDefinition
11+
import kotlinx.rpc.internal.utils.map.RpcInternalConcurrentHashMap
12+
913
/**
1014
* Registry of services and their methods used by servers to dispatching incoming calls.
1115
*/
12-
public actual abstract class HandlerRegistry
16+
public actual abstract class HandlerRegistry {
17+
/**
18+
* Returns the [ServerServiceDefinition]s provided by the registry, or an empty list if not
19+
* supported by the implementation.
20+
*/
21+
public abstract fun getServices(): List<ServerServiceDefinition>
22+
23+
/**
24+
* Lookup a [ServerMethodDefinition] by its fully-qualified name.
25+
*
26+
* @param methodName to lookup [ServerMethodDefinition] for.
27+
* @param authority the authority for the desired method (to do virtual hosting). If `null`
28+
* the first matching method will be returned.
29+
* @return the resolved method or `null` if no method for that name exists.
30+
*/
31+
public abstract fun lookupMethod(
32+
methodName: String, authority: String?,
33+
): ServerMethodDefinition<*, *>?
34+
35+
/**
36+
* Lookup a [ServerMethodDefinition] by its fully-qualified name.
37+
*
38+
* @param methodName to lookup [ServerMethodDefinition] for.
39+
* @return the resolved method or `null` if no method for that name exists.
40+
*/
41+
public fun lookupMethod(methodName: String): ServerMethodDefinition<*, *>? {
42+
return lookupMethod(methodName, null)
43+
}
44+
45+
}
1346

1447
internal actual class MutableHandlerRegistry : HandlerRegistry() {
48+
49+
private val services = RpcInternalConcurrentHashMap<String, ServerServiceDefinition>()
50+
1551
actual fun addService(service: ServerServiceDefinition): ServerServiceDefinition? {
16-
error("Native target is not supported in gRPC")
52+
return services.put(service.getServiceDescriptor().getName(), service)
1753
}
1854

1955
actual fun removeService(service: ServerServiceDefinition): Boolean {
20-
error("Native target is not supported in gRPC")
56+
return services.remove(service.getServiceDescriptor().getName()) != null
57+
}
58+
59+
override fun getServices(): List<ServerServiceDefinition> {
60+
return services.values.toList()
61+
}
62+
63+
override fun lookupMethod(
64+
methodName: String,
65+
authority: String?,
66+
): ServerMethodDefinition<*, *>? {
67+
val serviceName = MethodDescriptor.extractFullServiceName(methodName) ?: return null
68+
val service = services[serviceName] ?: return null
69+
return service.getMethod(methodName)
2170
}
2271
}

grpc/grpc-core/src/nativeMain/kotlin/kotlinx/rpc/grpc/Server.native.kt

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
package kotlinx.rpc.grpc
66

7-
import kotlinx.rpc.grpc.internal.GrpcServerCredentials
7+
import kotlinx.rpc.grpc.internal.GrpcInsecureServerCredentials
88
import kotlinx.rpc.grpc.internal.NativeServer
9+
import kotlinx.rpc.grpc.internal.ServerMethodDefinition
910

1011
/**
1112
* Platform-specific gRPC server builder.
@@ -23,26 +24,22 @@ private class NativeServerBuilder(
2324
) : ServerBuilder<NativeServerBuilder>() {
2425

2526
// TODO: Add actual credentials
26-
private val credentials = GrpcServerCredentials.createInsecure()
27+
private val credentials = GrpcInsecureServerCredentials()
2728
private val services = mutableListOf<ServerServiceDefinition>()
29+
private var fallbackRegistry: HandlerRegistry = DefaultFallbackRegistry
2830

2931
override fun addService(service: ServerServiceDefinition): NativeServerBuilder {
3032
services.add(service)
3133
return this
3234
}
3335

3436
override fun fallbackHandlerRegistry(registry: HandlerRegistry?): NativeServerBuilder {
35-
TODO("Not yet implemented")
37+
fallbackRegistry = registry ?: DefaultFallbackRegistry
38+
return this
3639
}
3740

3841
override fun build(): Server {
39-
val server = NativeServer(port, credentials)
40-
41-
for (service in services) {
42-
server.addService(service)
43-
}
44-
45-
return server
42+
return NativeServer(port, credentials, services, fallbackRegistry)
4643
}
4744

4845
}
@@ -54,3 +51,17 @@ internal actual fun ServerBuilder(port: Int): ServerBuilder<*> {
5451
internal actual fun Server(builder: ServerBuilder<*>): Server {
5552
return builder.build()
5653
}
54+
55+
private object DefaultFallbackRegistry : HandlerRegistry() {
56+
override fun getServices(): List<ServerServiceDefinition> {
57+
return listOf()
58+
}
59+
60+
override fun lookupMethod(
61+
methodName: String,
62+
authority: String?,
63+
): ServerMethodDefinition<*, *>? {
64+
return null
65+
}
66+
67+
}

grpc/grpc-core/src/nativeMain/kotlin/kotlinx/rpc/grpc/ServerServiceDefinition.native.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,18 @@ import kotlinx.rpc.internal.utils.InternalRpcApi
1010

1111
public actual class ServerServiceDefinition internal constructor(
1212
private val serviceDescriptor: ServiceDescriptor,
13-
private val methods: Collection<ServerMethodDefinition<*, *>>,
13+
private val methods: Map<String, ServerMethodDefinition<*, *>>,
1414
) {
15+
16+
internal constructor(serviceDescriptor: ServiceDescriptor, methods: Collection<ServerMethodDefinition<*, *>>) :
17+
this(serviceDescriptor, methods.associateBy { it.getMethodDescriptor().getFullMethodName() })
18+
1519
public actual fun getServiceDescriptor(): ServiceDescriptor = serviceDescriptor
1620

17-
public actual fun getMethods(): Collection<ServerMethodDefinition<*, *>> = methods
21+
public actual fun getMethods(): Collection<ServerMethodDefinition<*, *>> = methods.values
22+
23+
public actual fun getMethod(methodName: String): ServerMethodDefinition<*, *>? = methods[methodName]
24+
1825
}
1926

2027
@InternalRpcApi

grpc/grpc-core/src/nativeMain/kotlin/kotlinx/rpc/grpc/internal/CompletionQueue.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,19 +219,42 @@ private fun deleteCbTag(tag: CPointer<kgrpc_cb_tag>) {
219219
nativeHeap.free(tag)
220220
}
221221

222+
/**
223+
* Represents a callback tag for completion queues constructed with
224+
* [grpc_completion_queue_create_for_callback].
225+
*
226+
* The [run] method will be invoked onces the completion queue signals the completion of the operation.
227+
* It is guaranteed that the [run] method will be only invoked once.
228+
*
229+
* `this` object is guaranteed to be not garbage collected until the [run] method was executed.
230+
*/
222231
internal interface CallbackTag {
223232
fun run(ok: Boolean)
224233

234+
/**
235+
* Creates a pointer to a gRPC callback tag that encapsulates the given `run` function.
236+
* It can be passed to callback-based grpc_completion_queue.
237+
*/
225238
fun toCbTag(): CPointer<kgrpc_cb_tag> {
226239
return newCbTag(this, staticCFunction { functor, ok ->
227240
val tag = functor!!.reinterpret<kgrpc_cb_tag>()
228241
val callbackTag = tag.pointed.user_data!!.asStableRef<CallbackTag>().get()
242+
// free the tag memory and release the stable reference to `this`
229243
deleteCbTag(tag)
230244
callbackTag.run(ok != 0)
231245
})
232246
}
233247

234248
companion object {
249+
/**
250+
* Creates a pointer to a gRPC callback tag that encapsulates the given `run` function.
251+
*
252+
* The `run` function will be invoked when the callback is triggered with a boolean
253+
* value that indicates the success or failure of the operation.
254+
*
255+
* @return A pointer to the newly created gRPC callback tag.
256+
* It can be passed to callback-based grpc_completion_queue.
257+
*/
235258
fun anonymous(run: (ok: Boolean) -> Unit): CPointer<kgrpc_cb_tag> {
236259
return object : CallbackTag {
237260
override fun run(ok: Boolean) {

grpc/grpc-core/src/nativeMain/kotlin/kotlinx/rpc/grpc/internal/MethodDescriptor.native.kt

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,7 @@ public actual class MethodDescriptor<Request, Response> internal constructor(
2222
public actual fun getFullMethodName(): String = fullMethodName
2323

2424
private val serviceName: String? by lazy {
25-
val index = fullMethodName.lastIndexOf('/')
26-
if (index == -1) {
27-
null
28-
} else {
29-
fullMethodName.substring(0, index)
30-
}
25+
extractFullServiceName(fullMethodName)
3126
}
3227

3328
public actual fun getServiceName(): String? = serviceName
@@ -48,6 +43,17 @@ public actual class MethodDescriptor<Request, Response> internal constructor(
4843
public actual fun stream(value: T): InputStream
4944
public actual fun parse(stream: InputStream): T
5045
}
46+
47+
public companion object {
48+
public fun extractFullServiceName(fullMethodName: String): String? {
49+
val index = fullMethodName.lastIndexOf('/')
50+
return if (index == -1) {
51+
null
52+
} else {
53+
fullMethodName.take(index)
54+
}
55+
}
56+
}
5157
}
5258

5359
@InternalRpcApi

grpc/grpc-core/src/nativeMain/kotlin/kotlinx/rpc/grpc/internal/NativeManagedChannel.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import libkgrpc.grpc_channel_create_call
2626
import libkgrpc.grpc_channel_credentials_release
2727
import libkgrpc.grpc_channel_destroy
2828
import libkgrpc.grpc_insecure_credentials_create
29+
import libkgrpc.grpc_slice_unref
2930
import kotlin.coroutines.cancellation.CancellationException
3031
import kotlin.experimental.ExperimentalNativeApi
3132
import kotlin.native.ref.createCleaner
@@ -151,6 +152,8 @@ internal class NativeManagedChannel(
151152
reserved = null
152153
) ?: error("Failed to create call")
153154

155+
grpc_slice_unref(methodNameSlice)
156+
154157
return NativeClientCall(
155158
cq, rawCall, methodDescriptor, callJob
156159
)

0 commit comments

Comments
 (0)