Skip to content

Commit e5b327f

Browse files
Jozott00Mr3zee
authored andcommitted
grpc: Splitting core module into client and server (#504)
* grpc: Split in core, client, server modules * grpc: Add sub packages * grpc: Move internal classes * grpc: Update kmp example * grpc: Reduce dependencies * grpc: Fix Ktor server test * grpc: Remove todo
1 parent f1609b0 commit e5b327f

File tree

88 files changed

+941
-583
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+941
-583
lines changed

compiler-plugin/compiler-plugin-backend/src/main/kotlin/kotlinx/rpc/codegen/extension/RpcIrContext.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,12 @@ internal class RpcIrContext(
8484
if (isJvmTarget()) {
8585
getIrClassSymbol("io.grpc", "MethodDescriptor")
8686
} else {
87-
getIrClassSymbol("kotlinx.rpc.grpc.internal", "MethodDescriptor")
87+
getIrClassSymbol("kotlinx.rpc.grpc.descriptor", "MethodDescriptor")
8888
}
8989
}
9090

9191
val grpcPlatformMethodType by lazy {
92-
getIrClassSymbol("kotlinx.rpc.grpc.internal", "MethodType")
92+
getIrClassSymbol("kotlinx.rpc.grpc.descriptor", "MethodType")
9393
}
9494

9595
val grpcPlatformMethodTypeUnary by lazy {
@@ -248,7 +248,7 @@ internal class RpcIrContext(
248248
}
249249

250250
val methodDescriptor by lazy {
251-
namedFunction("kotlinx.rpc.grpc.internal", "methodDescriptor")
251+
namedFunction("kotlinx.rpc.grpc.descriptor", "methodDescriptor")
252252
}
253253

254254
val grpcServiceDescriptorDelegate by lazy {

grpc/grpc-client/build.gradle.kts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
@file:OptIn(InternalRpcApi::class)
6+
7+
import kotlinx.rpc.internal.InternalRpcApi
8+
9+
plugins {
10+
alias(libs.plugins.conventions.kmp)
11+
alias(libs.plugins.kotlinx.rpc)
12+
}
13+
14+
kotlin {
15+
compilerOptions {
16+
freeCompilerArgs.add("-Xexpect-actual-classes")
17+
}
18+
19+
sourceSets {
20+
commonMain {
21+
dependencies {
22+
api(projects.grpc.grpcCore)
23+
}
24+
}
25+
26+
nativeMain {
27+
dependencies {
28+
implementation(libs.atomicfu)
29+
}
30+
}
31+
}
32+
}

grpc/grpc-client/gradle.properties

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#
2+
# Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
3+
#
4+
kotlinx.rpc.exclude.wasmWasi=true
5+
kotlinx.rpc.exclude.js=true
6+
kotlinx.rpc.exclude.wasmJs=true
7+
kotlinx.rpc.exclude.mingwX64=true
8+
kotlinx.rpc.exclude.tvosArm64=true
9+
kotlinx.rpc.exclude.tvosSimulatorArm64=true
10+
kotlinx.rpc.exclude.tvosX64=true
11+
kotlinx.rpc.exclude.watchosArm32=true
12+
kotlinx.rpc.exclude.watchosDeviceArm64=true
13+
kotlinx.rpc.exclude.watchosX64=true
14+
# TODO: Remove once we ant to activate WatchOS (these two targets are already prepared for activation)
15+
kotlinx.rpc.exclude.watchosArm64=true
16+
kotlinx.rpc.exclude.watchosSimulatorArm64=true

grpc/grpc-core/src/commonMain/kotlin/kotlinx/rpc/grpc/ClientInterceptor.kt renamed to grpc/grpc-client/src/commonMain/kotlin/kotlinx/rpc/grpc/client/ClientInterceptor.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
* Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

5-
package kotlinx.rpc.grpc
5+
package kotlinx.rpc.grpc.client
66

77
import kotlinx.coroutines.flow.Flow
8-
import kotlinx.rpc.grpc.internal.GrpcCallOptions
9-
import kotlinx.rpc.grpc.internal.MethodDescriptor
8+
import kotlinx.rpc.grpc.GrpcMetadata
9+
import kotlinx.rpc.grpc.Status
10+
import kotlinx.rpc.grpc.client.internal.GrpcCallOptions
11+
import kotlinx.rpc.grpc.descriptor.MethodDescriptor
1012

1113
/**
1214
* The scope of a single outgoing gRPC client call observed by a [ClientInterceptor].
@@ -78,7 +80,7 @@ public interface ClientCallScope<Request, Response> {
7880
* Cancel the call locally, providing a human-readable [message] and an optional [cause].
7981
* This method won't return and abort all further processing.
8082
*
81-
* We made cancel throw a [StatusException] instead of returning, so control flow is explicit and
83+
* We made cancel throw a [kotlinx.rpc.grpc.StatusException] instead of returning, so control flow is explicit and
8284
* race conditions between interceptors and the transport layer are avoided.
8385
*/
8486
public fun cancel(message: String, cause: Throwable? = null): Nothing

grpc/grpc-core/src/commonMain/kotlin/kotlinx/rpc/grpc/GrpcClient.kt renamed to grpc/grpc-client/src/commonMain/kotlin/kotlinx/rpc/grpc/client/GrpcClient.kt

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,29 @@
22
* Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

5-
package kotlinx.rpc.grpc
5+
package kotlinx.rpc.grpc.client
66

77
import kotlinx.coroutines.flow.Flow
88
import kotlinx.rpc.RpcCall
99
import kotlinx.rpc.RpcClient
10+
import kotlinx.rpc.grpc.GrpcMetadata
11+
import kotlinx.rpc.grpc.client.internal.GrpcDefaultCallOptions
12+
import kotlinx.rpc.grpc.client.internal.ManagedChannel
13+
import kotlinx.rpc.grpc.client.internal.ManagedChannelBuilder
14+
import kotlinx.rpc.grpc.client.internal.bidirectionalStreamingRpc
15+
import kotlinx.rpc.grpc.client.internal.buildChannel
16+
import kotlinx.rpc.grpc.client.internal.clientStreamingRpc
17+
import kotlinx.rpc.grpc.client.internal.serverStreamingRpc
18+
import kotlinx.rpc.grpc.client.internal.unaryRpc
1019
import kotlinx.rpc.grpc.codec.EmptyMessageCodecResolver
1120
import kotlinx.rpc.grpc.codec.MessageCodecResolver
1221
import kotlinx.rpc.grpc.codec.ThrowingMessageCodecResolver
1322
import kotlinx.rpc.grpc.codec.plus
1423
import kotlinx.rpc.grpc.descriptor.GrpcServiceDelegate
1524
import kotlinx.rpc.grpc.descriptor.GrpcServiceDescriptor
16-
import kotlinx.rpc.grpc.internal.GrpcDefaultCallOptions
17-
import kotlinx.rpc.grpc.internal.MethodDescriptor
18-
import kotlinx.rpc.grpc.internal.MethodType
19-
import kotlinx.rpc.grpc.internal.bidirectionalStreamingRpc
20-
import kotlinx.rpc.grpc.internal.clientStreamingRpc
21-
import kotlinx.rpc.grpc.internal.serverStreamingRpc
22-
import kotlinx.rpc.grpc.internal.type
23-
import kotlinx.rpc.grpc.internal.unaryRpc
25+
import kotlinx.rpc.grpc.descriptor.MethodDescriptor
26+
import kotlinx.rpc.grpc.descriptor.MethodType
27+
import kotlinx.rpc.grpc.descriptor.methodType
2428
import kotlinx.rpc.internal.utils.map.RpcInternalConcurrentHashMap
2529
import kotlin.time.Duration
2630

@@ -29,7 +33,7 @@ private typealias RequestClient = Any
2933
/**
3034
* GrpcClient manages gRPC communication by providing implementation for making asynchronous RPC calls.
3135
*
32-
* @field channel The [ManagedChannel] used to communicate with remote gRPC services.
36+
* @field channel The [kotlinx.rpc.grpc.client.internal.ManagedChannel] used to communicate with remote gRPC services.
3337
*/
3438
public class GrpcClient internal constructor(
3539
internal val channel: ManagedChannel,
@@ -57,7 +61,7 @@ public class GrpcClient internal constructor(
5761
val callOptions = GrpcDefaultCallOptions
5862
val trailers = GrpcMetadata()
5963

60-
return when (methodDescriptor.type) {
64+
return when (methodDescriptor.methodType) {
6165
MethodType.UNARY -> unaryRpc(
6266
descriptor = methodDescriptor,
6367
request = request,
@@ -72,15 +76,15 @@ public class GrpcClient internal constructor(
7276
trailers = trailers,
7377
)
7478

75-
else -> error("Wrong method type ${methodDescriptor.type}")
79+
else -> error("Wrong method type ${methodDescriptor.methodType}")
7680
}
7781
}
7882

7983
override fun <T> callServerStreaming(call: RpcCall): Flow<T> = withGrpcCall(call) { methodDescriptor, request ->
8084
val callOptions = GrpcDefaultCallOptions
8185
val trailers = GrpcMetadata()
8286

83-
when (methodDescriptor.type) {
87+
when (methodDescriptor.methodType) {
8488
MethodType.SERVER_STREAMING -> serverStreamingRpc(
8589
descriptor = methodDescriptor,
8690
request = request,
@@ -95,7 +99,7 @@ public class GrpcClient internal constructor(
9599
trailers = trailers,
96100
)
97101

98-
else -> error("Wrong method type ${methodDescriptor.type}")
102+
else -> error("Wrong method type ${methodDescriptor.methodType}")
99103
}
100104
}
101105

@@ -187,6 +191,10 @@ private fun GrpcClient(
187191
* Configuration class for a gRPC client, providing customization options
188192
* for client behavior, including interceptors, credentials, codec resolution,
189193
* and authority overrides.
194+
*
195+
* @see credentials
196+
* @see overrideAuthority
197+
* @see intercept
190198
*/
191199
public class GrpcClientConfiguration internal constructor() {
192200
internal val interceptors: MutableList<ClientInterceptor> = mutableListOf()
@@ -222,6 +230,9 @@ public class GrpcClientConfiguration internal constructor() {
222230
* credentials = plaintext() // for testing purposes only!
223231
* }
224232
* ```
233+
*
234+
* @see tls
235+
* @see plaintext
225236
*/
226237
public var credentials: ClientCredentials? = null
227238

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package kotlinx.rpc.grpc.client
6+
7+
import kotlinx.rpc.internal.utils.InternalRpcApi
8+
9+
public expect abstract class ClientCredentials
10+
11+
public expect class InsecureClientCredentials : ClientCredentials
12+
13+
// we need a wrapper for InsecureChannelCredentials as our constructor would conflict with the private
14+
// java constructor.
15+
@InternalRpcApi
16+
public expect fun createInsecureClientCredentials(): ClientCredentials
17+
18+
public expect class TlsClientCredentials : ClientCredentials
19+
20+
public fun TlsClientCredentials(configure: TlsClientCredentialsBuilder.() -> Unit = {}): ClientCredentials {
21+
val builder = TlsClientCredentialsBuilder()
22+
builder.configure()
23+
return builder.build()
24+
}
25+
26+
public interface TlsClientCredentialsBuilder {
27+
public fun trustManager(rootCertsPem: String): TlsClientCredentialsBuilder
28+
public fun keyManager(certChainPem: String, privateKeyPem: String): TlsClientCredentialsBuilder
29+
}
30+
31+
internal expect fun TlsClientCredentialsBuilder(): TlsClientCredentialsBuilder
32+
33+
internal expect fun TlsClientCredentialsBuilder.build(): ClientCredentials

grpc/grpc-core/src/commonMain/kotlin/kotlinx/rpc/grpc/internal/ClientCall.kt renamed to grpc/grpc-client/src/commonMain/kotlin/kotlinx/rpc/grpc/client/internal/ClientCall.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

5-
package kotlinx.rpc.grpc.internal
5+
package kotlinx.rpc.grpc.client.internal
66

77
import kotlinx.rpc.grpc.GrpcMetadata
88
import kotlinx.rpc.grpc.Status

grpc/grpc-core/src/commonMain/kotlin/kotlinx/rpc/grpc/internal/GrpcCallOptions.kt renamed to grpc/grpc-client/src/commonMain/kotlin/kotlinx/rpc/grpc/client/internal/GrpcCallOptions.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

5-
package kotlinx.rpc.grpc.internal
5+
package kotlinx.rpc.grpc.client.internal
66

77
import kotlinx.rpc.internal.utils.InternalRpcApi
88

grpc/grpc-core/src/commonMain/kotlin/kotlinx/rpc/grpc/internal/GrpcChannel.kt renamed to grpc/grpc-client/src/commonMain/kotlin/kotlinx/rpc/grpc/client/internal/GrpcChannel.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
* Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

5-
package kotlinx.rpc.grpc.internal
5+
package kotlinx.rpc.grpc.client.internal
66

7+
import kotlinx.rpc.grpc.descriptor.MethodDescriptor
78
import kotlinx.rpc.internal.utils.InternalRpcApi
89

910
@InternalRpcApi

grpc/grpc-core/src/commonMain/kotlin/kotlinx/rpc/grpc/ManagedChannel.kt renamed to grpc/grpc-client/src/commonMain/kotlin/kotlinx/rpc/grpc/client/internal/ManagedChannel.kt

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44

55
@file:Suppress("EXPECT_ACTUAL_CLASSIFIERS_ARE_IN_BETA_WARNING")
66

7-
package kotlinx.rpc.grpc
7+
package kotlinx.rpc.grpc.client.internal
88

9-
import kotlinx.rpc.grpc.internal.GrpcChannel
9+
import kotlinx.rpc.grpc.client.ClientCredentials
10+
import kotlinx.rpc.internal.utils.InternalRpcApi
1011
import kotlin.time.Duration
1112

1213
/**
1314
* Same as [ManagedChannel], but is platform-exposed.
1415
*/
16+
@InternalRpcApi
1517
public expect abstract class ManagedChannelPlatform : GrpcChannel
1618

1719
/**
@@ -22,6 +24,7 @@ public expect abstract class ManagedChannelPlatform : GrpcChannel
2224
*
2325
* Provides lifecycle management.
2426
*/
27+
@InternalRpcApi
2528
public interface ManagedChannel {
2629
/**
2730
* Returns whether the channel is shutdown.
@@ -67,19 +70,23 @@ public interface ManagedChannel {
6770
/**
6871
* Builder class for [ManagedChannel].
6972
*/
73+
@InternalRpcApi
7074
public expect abstract class ManagedChannelBuilder<T : ManagedChannelBuilder<T>> {
7175
public abstract fun overrideAuthority(authority: String): T
7276
}
7377

74-
internal expect fun ManagedChannelBuilder(
78+
@InternalRpcApi
79+
public expect fun ManagedChannelBuilder(
7580
hostname: String,
7681
port: Int,
7782
credentials: ClientCredentials? = null,
7883
): ManagedChannelBuilder<*>
7984

80-
internal expect fun ManagedChannelBuilder(
85+
@InternalRpcApi
86+
public expect fun ManagedChannelBuilder(
8187
target: String,
8288
credentials: ClientCredentials? = null,
8389
): ManagedChannelBuilder<*>
8490

85-
internal expect fun ManagedChannelBuilder<*>.buildChannel(): ManagedChannel
91+
@InternalRpcApi
92+
public expect fun ManagedChannelBuilder<*>.buildChannel(): ManagedChannel

0 commit comments

Comments
 (0)