Skip to content

Commit cda97dc

Browse files
authored
grpc: Add gzip Compression Support (#527)
1 parent a1f9a1b commit cda97dc

File tree

19 files changed

+541
-41
lines changed

19 files changed

+541
-41
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package kotlinx.rpc.grpc.client
77
import kotlinx.coroutines.flow.Flow
88
import kotlinx.rpc.grpc.GrpcMetadata
99
import kotlinx.rpc.grpc.Status
10-
import kotlinx.rpc.grpc.client.internal.GrpcCallOptions
10+
import kotlinx.rpc.grpc.client.GrpcCallOptions
1111
import kotlinx.rpc.grpc.descriptor.MethodDescriptor
1212

1313
/**
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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.grpc.GrpcCompression
8+
import kotlin.time.Duration
9+
10+
/**
11+
* The collection of runtime options for a new gRPC call.
12+
*
13+
* This class allows configuring per-call behavior such as timeouts.
14+
*/
15+
public class GrpcCallOptions {
16+
/**
17+
* The maximum duration to wait for the RPC to complete.
18+
*
19+
* If set, the RPC will be canceled (with `DEADLINE_EXCEEDED`)
20+
* if it does not complete within the specified duration.
21+
* The timeout is measured from the moment the call is initiated.
22+
* If `null`, no timeout is applied, and the call may run indefinitely.
23+
*
24+
* The default value is `null`.
25+
*
26+
* @see kotlin.time.Duration
27+
*/
28+
public var timeout: Duration? = null
29+
30+
/**
31+
* The compression algorithm to use for encoding outgoing messages in this call.
32+
*
33+
* When set to a value other than [GrpcCompression.None], the client will compress request messages
34+
* using the specified algorithm before sending them to the server. The chosen compression algorithm
35+
* is communicated to the server via the `grpc-encoding` header.
36+
*
37+
* ## Default Behavior
38+
* Defaults to [GrpcCompression.None], meaning no compression is applied to messages.
39+
*
40+
* ## Server Compatibility
41+
* **Important**: It is the caller's responsibility to ensure the server supports the chosen
42+
* compression algorithm. There is no automatic negotiation performed. If the server does not
43+
* support the requested compression, the call will fail.
44+
*
45+
* ## Available Algorithms
46+
* - [GrpcCompression.None]: No compression (identity encoding) - **default**
47+
* - [GrpcCompression.Gzip]: GZIP compression, widely supported
48+
*
49+
* @see GrpcCompression
50+
*/
51+
public var compression: GrpcCompression = GrpcCompression.None
52+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import kotlinx.coroutines.flow.Flow
88
import kotlinx.rpc.RpcCall
99
import kotlinx.rpc.RpcClient
1010
import kotlinx.rpc.grpc.GrpcMetadata
11-
import kotlinx.rpc.grpc.client.internal.GrpcCallOptions
11+
import kotlinx.rpc.grpc.client.GrpcCallOptions
1212
import kotlinx.rpc.grpc.client.internal.ManagedChannel
1313
import kotlinx.rpc.grpc.client.internal.ManagedChannelBuilder
1414
import kotlinx.rpc.grpc.client.internal.bidirectionalStreamingRpc

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

Lines changed: 0 additions & 28 deletions
This file was deleted.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
package kotlinx.rpc.grpc.client.internal
66

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

grpc/grpc-client/src/commonMain/kotlin/kotlinx/rpc/grpc/client/internal/suspendClientCalls.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import kotlinx.rpc.grpc.Status
2222
import kotlinx.rpc.grpc.StatusCode
2323
import kotlinx.rpc.grpc.StatusException
2424
import kotlinx.rpc.grpc.client.ClientCallScope
25+
import kotlinx.rpc.grpc.client.GrpcCallOptions
2526
import kotlinx.rpc.grpc.client.GrpcClient
2627
import kotlinx.rpc.grpc.descriptor.MethodDescriptor
2728
import kotlinx.rpc.grpc.descriptor.MethodType
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,21 @@
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.client.internal
5+
package kotlinx.rpc.grpc.client
66

77
import io.grpc.CallOptions
8+
import kotlinx.rpc.grpc.GrpcCompression
89
import kotlinx.rpc.internal.utils.InternalRpcApi
10+
import java.util.concurrent.TimeUnit
911

1012
@InternalRpcApi
1113
public fun GrpcCallOptions.toJvm(): CallOptions {
1214
var default = CallOptions.DEFAULT
1315
if (timeout != null) {
14-
default = default.withDeadlineAfter(timeout!!.inWholeMilliseconds, java.util.concurrent.TimeUnit.MILLISECONDS)
16+
default = default.withDeadlineAfter(timeout!!.inWholeMilliseconds, TimeUnit.MILLISECONDS)
17+
}
18+
if (compression !is GrpcCompression.None) {
19+
default = default.withCompression(compression.name)
1520
}
1621
return default
1722
}

grpc/grpc-client/src/jvmMain/kotlin/kotlinx/rpc/grpc/client/internal/GrpcChannel.jvm.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
package kotlinx.rpc.grpc.client.internal
66

77
import io.grpc.Channel
8+
import kotlinx.rpc.grpc.client.GrpcCallOptions
9+
import kotlinx.rpc.grpc.client.toJvm
810
import kotlinx.rpc.grpc.descriptor.MethodDescriptor
911
import kotlinx.rpc.internal.utils.InternalRpcApi
1012

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
@file:OptIn(ExperimentalForeignApi::class)
66

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

99
import kotlinx.cinterop.CValue
1010
import kotlinx.cinterop.ExperimentalForeignApi
@@ -25,4 +25,4 @@ public fun GrpcCallOptions.rawDeadline(): CValue<gpr_timespec> {
2525
gpr_time_from_millis(it.inWholeMilliseconds, GPR_TIMESPAN)
2626
)
2727
} ?: gpr_inf_future(GPR_CLOCK_REALTIME)
28-
}
28+
}

grpc/grpc-client/src/nativeMain/kotlin/kotlinx/rpc/grpc/client/internal/GrpcChannel.native.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
package kotlinx.rpc.grpc.client.internal
66

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

0 commit comments

Comments
 (0)