Skip to content

Commit 9318ec7

Browse files
committed
grpc-native: Refactor channel credentials
Signed-off-by: Johannes Zottele <[email protected]>
1 parent 178c5b3 commit 9318ec7

File tree

8 files changed

+95
-50
lines changed

8 files changed

+95
-50
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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/*
6+
* Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
7+
*/
8+
9+
public expect abstract class ChannelCredentials
10+
11+
public expect class InsecureChannelCredentials : ChannelCredentials
12+
13+
public expect class TlsChannelCredentials : ChannelCredentials
14+
15+
16+
public expect fun InsecureChannelCredentials(): ChannelCredentials
17+
public expect fun TlsChannelCredentials(): ChannelCredentials

grpc/grpc-core/src/commonTest/kotlin/kotlinx/rpc/grpc/test/proto/GrpcbInTlsTest.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import hello.HelloService
99
import hello.invoke
1010
import kotlinx.coroutines.test.runTest
1111
import kotlinx.rpc.grpc.GrpcClient
12+
import kotlinx.rpc.grpc.TlsChannelCredentials
1213
import kotlinx.rpc.withService
1314
import kotlin.test.Test
1415

@@ -23,6 +24,8 @@ class GrpcbInTlsTest {
2324
fun testTlsCall() = runTest {
2425
val service = grpcClient.withService<HelloService>()
2526

27+
TlsChannelCredentials.create()
28+
2629
val request = HelloRequest {
2730
greeting = "Postman"
2831
}

grpc/grpc-core/src/jvmMain/kotlin/kotlinx/rpc/grpc/ManagedChannel.jvm.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ internal actual fun ManagedChannelBuilder(hostname: String, port: Int): ManagedC
2828
return io.grpc.ManagedChannelBuilder.forAddress(hostname, port)
2929
}
3030

31-
internal actual fun ManagedChannelBuilder(target: String): ManagedChannelBuilder<*> {
31+
internal actual fun ManagedChannelBuilder(
32+
target: String,
33+
): ManagedChannelBuilder<*> {
3234
return io.grpc.ManagedChannelBuilder.forTarget(target)
3335
}
3436

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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
6+
7+
public actual typealias ChannelCredentials = io.grpc.ChannelCredentials
8+
9+
public actual typealias TlsChannelCredentials = io.grpc.TlsChannelCredentials
10+
11+
public actual typealias InsecureChannelCredentials = io.grpc.InsecureChannelCredentials
12+
13+
public actual fun InsecureChannelCredentials.create(): ChannelCredentials {
14+
return io.grpc.InsecureChannelCredentials.create()
15+
}
16+
17+
public actual fun TlsChannelCredentials.create(): ChannelCredentials {
18+
return io.grpc.TlsChannelCredentials.create()
19+
}

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
package kotlinx.rpc.grpc
88

99
import kotlinx.rpc.grpc.internal.GrpcChannel
10-
import kotlinx.rpc.grpc.internal.GrpcChannelCredentials
11-
import kotlinx.rpc.grpc.internal.GrpcInsecureChannelCredentials
12-
import kotlinx.rpc.grpc.internal.GrpcSslChannelCredentials
1310
import kotlinx.rpc.grpc.internal.NativeManagedChannel
1411
import kotlinx.rpc.grpc.internal.internalError
1512

@@ -30,10 +27,10 @@ public actual abstract class ManagedChannelBuilder<T : ManagedChannelBuilder<T>>
3027
internal class NativeManagedChannelBuilder(
3128
private val target: String,
3229
) : ManagedChannelBuilder<NativeManagedChannelBuilder>() {
33-
private var credentials: Lazy<GrpcChannelCredentials> = lazy { GrpcSslChannelCredentials() }
30+
private var credentials: Lazy<ChannelCredentials> = lazy { TlsChannelCredentials.create() }
3431

3532
override fun usePlaintext(): NativeManagedChannelBuilder {
36-
credentials = lazy { GrpcInsecureChannelCredentials() }
33+
credentials = lazy { InsecureChannelCredentials.create() }
3734
return this
3835
}
3936

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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(ExperimentalForeignApi::class, ExperimentalNativeApi::class)
6+
7+
package kotlinx.rpc.grpc
8+
9+
import cnames.structs.grpc_channel_credentials
10+
import kotlinx.cinterop.CPointer
11+
import kotlinx.cinterop.ExperimentalForeignApi
12+
import libkgrpc.grpc_channel_credentials_release
13+
import libkgrpc.grpc_insecure_credentials_create
14+
import libkgrpc.grpc_tls_credentials_create
15+
import libkgrpc.grpc_tls_credentials_options_create
16+
import kotlin.experimental.ExperimentalNativeApi
17+
import kotlin.native.ref.createCleaner
18+
19+
public actual abstract class ChannelCredentials internal constructor(
20+
internal val raw: CPointer<grpc_channel_credentials>,
21+
) {
22+
internal val rawCleaner = createCleaner(raw) {
23+
grpc_channel_credentials_release(it)
24+
}
25+
}
26+
27+
public actual class InsecureChannelCredentials internal constructor(
28+
raw: CPointer<grpc_channel_credentials>,
29+
) : ChannelCredentials(raw)
30+
31+
public actual class TlsChannelCredentials internal constructor(
32+
raw: CPointer<grpc_channel_credentials>,
33+
) : ChannelCredentials(raw)
34+
35+
36+
public actual fun InsecureChannelCredentials.Companion.create(): ChannelCredentials {
37+
return InsecureChannelCredentials(
38+
grpc_insecure_credentials_create() ?: error("grpc_insecure_credentials_create() returned null")
39+
)
40+
}
41+
42+
public actual fun TlsChannelCredentials.Companion.create(): ChannelCredentials {
43+
val raw = grpc_tls_credentials_options_create()?.let {
44+
grpc_tls_credentials_create(it)
45+
} ?: error("Failed to create TLS credentials")
46+
47+
return TlsChannelCredentials(raw)
48+
}

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

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,70 +7,29 @@
77
package kotlinx.rpc.grpc.internal
88

99
import cnames.structs.grpc_channel
10-
import cnames.structs.grpc_channel_credentials
1110
import kotlinx.atomicfu.atomic
1211
import kotlinx.cinterop.CPointer
1312
import kotlinx.cinterop.ExperimentalForeignApi
14-
import kotlinx.cinterop.memScoped
1513
import kotlinx.coroutines.CompletableDeferred
1614
import kotlinx.coroutines.Job
1715
import kotlinx.coroutines.SupervisorJob
1816
import kotlinx.coroutines.cancelChildren
1917
import kotlinx.coroutines.withTimeoutOrNull
18+
import kotlinx.rpc.grpc.ChannelCredentials
2019
import kotlinx.rpc.grpc.ManagedChannel
2120
import kotlinx.rpc.grpc.ManagedChannelPlatform
2221
import libkgrpc.GPR_CLOCK_REALTIME
2322
import libkgrpc.GRPC_PROPAGATE_DEFAULTS
2423
import libkgrpc.gpr_inf_future
2524
import libkgrpc.grpc_channel_create
2625
import libkgrpc.grpc_channel_create_call
27-
import libkgrpc.grpc_channel_credentials_release
2826
import libkgrpc.grpc_channel_destroy
29-
import libkgrpc.grpc_insecure_credentials_create
3027
import libkgrpc.grpc_slice_unref
31-
import libkgrpc.grpc_ssl_credentials_create_ex
3228
import kotlin.coroutines.cancellation.CancellationException
3329
import kotlin.experimental.ExperimentalNativeApi
3430
import kotlin.native.ref.createCleaner
3531
import kotlin.time.Duration
3632

37-
/**
38-
* Wrapper for [grpc_channel_credentials].
39-
*/
40-
internal sealed class GrpcChannelCredentials(
41-
internal val raw: CPointer<grpc_channel_credentials>,
42-
) {
43-
val rawCleaner = createCleaner(raw) {
44-
grpc_channel_credentials_release(it)
45-
}
46-
}
47-
48-
/**
49-
* Insecure credentials.
50-
*/
51-
internal class GrpcInsecureChannelCredentials() :
52-
GrpcChannelCredentials(
53-
grpc_insecure_credentials_create() ?: error("grpc_insecure_credentials_create() returned null")
54-
)
55-
56-
/**
57-
* SSL credentials.
58-
*/
59-
internal class GrpcSslChannelCredentials(
60-
raw: CPointer<grpc_channel_credentials>,
61-
) : GrpcChannelCredentials(raw) {
62-
63-
constructor() : this(
64-
memScoped {
65-
grpc_ssl_credentials_create_ex(
66-
null,
67-
null,
68-
null,
69-
null
70-
) ?: error("grpc_ssl_credentials_create() returned null")
71-
}
72-
)
73-
}
7433

7534
/**
7635
* Native implementation of [ManagedChannel].
@@ -81,7 +40,7 @@ internal class GrpcSslChannelCredentials(
8140
internal class NativeManagedChannel(
8241
target: String,
8342
// we must store them, otherwise the credentials are getting released
84-
credentials: GrpcChannelCredentials,
43+
credentials: ChannelCredentials,
8544
) : ManagedChannel, ManagedChannelPlatform() {
8645

8746
// a reference to make sure the grpc_init() was called. (it is released after shutdown)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import libkgrpc.grpc_slice_unref
4343
import libkgrpc.grpc_status_code
4444
import platform.posix.memcpy
4545

46-
internal fun internalError(message: String) {
46+
internal fun internalError(message: String): Nothing {
4747
error("Unexpected internal error: $message. Please, report the issue here: https://github.com/Kotlin/kotlinx-rpc/issues/new?template=bug_report.md")
4848
}
4949

0 commit comments

Comments
 (0)