Skip to content

Commit 9000b45

Browse files
committed
Review comments: API docs, comments and other changes
1 parent f100dd0 commit 9000b45

33 files changed

+414
-70
lines changed

grpc/grpc-core/api/grpc-core.api

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
public final class kotlinx/rpc/grpc/GrpcClient : kotlinx/rpc/RpcClient {
2+
public fun call (Lkotlinx/rpc/RpcCall;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
3+
public fun callAsync (Lkotlinx/coroutines/CoroutineScope;Lkotlinx/rpc/RpcCall;)Lkotlinx/coroutines/Deferred;
4+
public fun getCoroutineContext ()Lkotlin/coroutines/CoroutineContext;
5+
public fun provideStubContext (J)Lkotlin/coroutines/CoroutineContext;
6+
}
7+
8+
public final class kotlinx/rpc/grpc/GrpcClientKt {
9+
public static final fun GrpcClient (Ljava/lang/String;ILkotlin/jvm/functions/Function1;)Lkotlinx/rpc/grpc/GrpcClient;
10+
public static final fun GrpcClient (Ljava/lang/String;Lkotlin/jvm/functions/Function1;)Lkotlinx/rpc/grpc/GrpcClient;
11+
public static synthetic fun GrpcClient$default (Ljava/lang/String;ILkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lkotlinx/rpc/grpc/GrpcClient;
12+
public static synthetic fun GrpcClient$default (Ljava/lang/String;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lkotlinx/rpc/grpc/GrpcClient;
13+
}
14+
15+
public final class kotlinx/rpc/grpc/GrpcServer : kotlinx/rpc/RpcServer, kotlinx/rpc/grpc/Server {
16+
public fun awaitTermination-VtjQ1oo (JLkotlin/coroutines/Continuation;)Ljava/lang/Object;
17+
public fun getCoroutineContext ()Lkotlin/coroutines/CoroutineContext;
18+
public fun getPort ()I
19+
public fun isShutdown ()Z
20+
public fun isTerminated ()Z
21+
public fun registerService (Lkotlin/reflect/KClass;Lkotlin/jvm/functions/Function1;)V
22+
public fun shutdown ()Lkotlinx/rpc/grpc/GrpcServer;
23+
public synthetic fun shutdown ()Lkotlinx/rpc/grpc/Server;
24+
public fun shutdownNow ()Lkotlinx/rpc/grpc/GrpcServer;
25+
public synthetic fun shutdownNow ()Lkotlinx/rpc/grpc/Server;
26+
public fun start ()Lkotlinx/rpc/grpc/GrpcServer;
27+
public synthetic fun start ()Lkotlinx/rpc/grpc/Server;
28+
}
29+
30+
public final class kotlinx/rpc/grpc/GrpcServerKt {
31+
public static final fun GrpcServer (ILkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;)Lkotlinx/rpc/grpc/GrpcServer;
32+
public static synthetic fun GrpcServer$default (ILkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lkotlinx/rpc/grpc/GrpcServer;
33+
}
34+
35+
public abstract interface class kotlinx/rpc/grpc/ManagedChannel {
36+
public abstract fun awaitTermination-VtjQ1oo (JLkotlin/coroutines/Continuation;)Ljava/lang/Object;
37+
public abstract fun getPlatformApi ()Lio/grpc/ManagedChannel;
38+
public abstract fun isShutdown ()Z
39+
public abstract fun isTerminated ()Z
40+
public abstract fun shutdown ()Lkotlinx/rpc/grpc/ManagedChannel;
41+
public abstract fun shutdownNow ()Lkotlinx/rpc/grpc/ManagedChannel;
42+
}
43+
44+
public final class kotlinx/rpc/grpc/ManagedChannel_jvmKt {
45+
public static final fun toKotlin (Lio/grpc/ManagedChannel;)Lkotlinx/rpc/grpc/ManagedChannel;
46+
}
47+
48+
public abstract interface class kotlinx/rpc/grpc/Server {
49+
public abstract fun awaitTermination-VtjQ1oo (JLkotlin/coroutines/Continuation;)Ljava/lang/Object;
50+
public abstract fun getPort ()I
51+
public abstract fun isShutdown ()Z
52+
public abstract fun isTerminated ()Z
53+
public abstract fun shutdown ()Lkotlinx/rpc/grpc/Server;
54+
public abstract fun shutdownNow ()Lkotlinx/rpc/grpc/Server;
55+
public abstract fun start ()Lkotlinx/rpc/grpc/Server;
56+
}
57+
58+
public final class kotlinx/rpc/grpc/Server$DefaultImpls {
59+
public static synthetic fun awaitTermination-VtjQ1oo$default (Lkotlinx/rpc/grpc/Server;JLkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
60+
}
61+
62+
public abstract interface class kotlinx/rpc/grpc/Status {
63+
public abstract fun getCause ()Ljava/lang/Throwable;
64+
public abstract fun getCode ()Lkotlinx/rpc/grpc/Status$Code;
65+
public abstract fun getDescription ()Ljava/lang/String;
66+
}
67+
68+
public final class kotlinx/rpc/grpc/Status$Code : java/lang/Enum {
69+
public static final field ABORTED Lkotlinx/rpc/grpc/Status$Code;
70+
public static final field ALREADY_EXISTS Lkotlinx/rpc/grpc/Status$Code;
71+
public static final field CANCELLED Lkotlinx/rpc/grpc/Status$Code;
72+
public static final field DATA_LOSS Lkotlinx/rpc/grpc/Status$Code;
73+
public static final field DEADLINE_EXCEEDED Lkotlinx/rpc/grpc/Status$Code;
74+
public static final field FAILED_PRECONDITION Lkotlinx/rpc/grpc/Status$Code;
75+
public static final field INTERNAL Lkotlinx/rpc/grpc/Status$Code;
76+
public static final field INVALID_ARGUMENT Lkotlinx/rpc/grpc/Status$Code;
77+
public static final field NOT_FOUND Lkotlinx/rpc/grpc/Status$Code;
78+
public static final field OK Lkotlinx/rpc/grpc/Status$Code;
79+
public static final field OUT_OF_RANGE Lkotlinx/rpc/grpc/Status$Code;
80+
public static final field PERMISSION_DENIED Lkotlinx/rpc/grpc/Status$Code;
81+
public static final field RESOURCE_EXHAUSTED Lkotlinx/rpc/grpc/Status$Code;
82+
public static final field UNAUTHENTICATED Lkotlinx/rpc/grpc/Status$Code;
83+
public static final field UNAVAILABLE Lkotlinx/rpc/grpc/Status$Code;
84+
public static final field UNIMPLEMENTED Lkotlinx/rpc/grpc/Status$Code;
85+
public static final field UNKNOWN Lkotlinx/rpc/grpc/Status$Code;
86+
public static fun getEntries ()Lkotlin/enums/EnumEntries;
87+
public final fun getValue ()I
88+
public final fun getValueAscii ()[B
89+
public static fun valueOf (Ljava/lang/String;)Lkotlinx/rpc/grpc/Status$Code;
90+
public static fun values ()[Lkotlinx/rpc/grpc/Status$Code;
91+
}
92+
93+
public abstract interface class kotlinx/rpc/grpc/StatusRuntimeException {
94+
public abstract fun getStatus ()Lkotlinx/rpc/grpc/Status;
95+
}
96+
97+
public final class kotlinx/rpc/grpc/StatusRuntimeException_jvmKt {
98+
public static final fun StatusRuntimeException (Lkotlinx/rpc/grpc/Status;)Lkotlinx/rpc/grpc/StatusRuntimeException;
99+
public static final fun toJvm (Lkotlinx/rpc/grpc/StatusRuntimeException;)Lio/grpc/StatusRuntimeException;
100+
public static final fun toKotlin (Lio/grpc/StatusRuntimeException;)Lkotlinx/rpc/grpc/StatusRuntimeException;
101+
}
102+
103+
public abstract interface class kotlinx/rpc/grpc/descriptor/GrpcClientDelegate {
104+
public abstract fun call (Lkotlinx/rpc/RpcCall;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
105+
public abstract fun callAsync (Lkotlinx/rpc/RpcCall;)Lkotlinx/coroutines/Deferred;
106+
}
107+
108+
public abstract interface class kotlinx/rpc/grpc/descriptor/GrpcDelegate {
109+
public abstract fun clientProvider (Lkotlinx/rpc/grpc/ManagedChannel;)Lkotlinx/rpc/grpc/descriptor/GrpcClientDelegate;
110+
public abstract fun definitionFor (Ljava/lang/Object;)Lio/grpc/ServerServiceDefinition;
111+
}
112+
113+
public abstract interface class kotlinx/rpc/grpc/descriptor/GrpcServiceDescriptor : kotlinx/rpc/descriptor/RpcServiceDescriptor {
114+
public abstract fun getDelegate ()Lkotlinx/rpc/grpc/descriptor/GrpcDelegate;
115+
}
116+

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ import kotlinx.rpc.grpc.descriptor.GrpcServiceDescriptor
1515
import kotlinx.rpc.internal.utils.map.ConcurrentHashMap
1616
import kotlin.coroutines.CoroutineContext
1717

18-
public class GrpcClient(private val channel: ManagedChannel) : RpcClient {
18+
/**
19+
* GrpcClient manages gRPC communication by providing implementation for making asynchronous RPC calls.
20+
*
21+
* @property channel The [ManagedChannel] used to communicate with remote gRPC services.
22+
*/
23+
public class GrpcClient internal constructor(private val channel: ManagedChannel) : RpcClient {
1924
override val coroutineContext: CoroutineContext = SupervisorJob()
2025

2126
private val stubs = ConcurrentHashMap<Long, GrpcClientDelegate>()
@@ -36,11 +41,13 @@ public class GrpcClient(private val channel: ManagedChannel) : RpcClient {
3641
}
3742

3843
override fun provideStubContext(serviceId: Long): CoroutineContext {
39-
// todo create lifetime hierarchy if possible
4044
return SupervisorJob(coroutineContext.job)
4145
}
4246
}
4347

48+
/**
49+
* Constructor function for the [GrpcClient] class.
50+
*/
4451
public fun GrpcClient(
4552
name: String,
4653
port: Int,
@@ -50,6 +57,9 @@ public fun GrpcClient(
5057
return GrpcClient(channel)
5158
}
5259

60+
/**
61+
* Constructor function for the [GrpcClient] class.
62+
*/
5363
public fun GrpcClient(
5464
target: String,
5565
configure: ManagedChannelBuilder<*>.() -> Unit = {},

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ import kotlin.coroutines.CoroutineContext
1313
import kotlin.reflect.KClass
1414
import kotlin.time.Duration
1515

16+
/**
17+
* GrpcServer is an implementation of both [RpcServer] and [Server] interfaces,
18+
* providing the ability to host gRPC services.
19+
*
20+
* @property port Specifies the port used by the server to listen for incoming connections.
21+
* @param builder exposes platform-specific Server builder.
22+
*/
1623
public class GrpcServer internal constructor(
1724
override val port: Int = 8080,
1825
builder: ServerBuilder<*>.() -> Unit,
@@ -26,7 +33,7 @@ public class GrpcServer internal constructor(
2633
}
2734

2835
override val coroutineContext: CoroutineContext
29-
get() = error("coroutineContext is not available for gRPC server builder")
36+
get() = error("coroutineContext is not available for gRPC server")
3037

3138
override fun <@Grpc Service : Any> registerService(
3239
serviceKClass: KClass<Service>,
@@ -87,6 +94,9 @@ public class GrpcServer internal constructor(
8794
}
8895
}
8996

97+
/**
98+
* Constructor function for the [GrpcServer] class.
99+
*/
90100
public fun GrpcServer(
91101
port: Int,
92102
configure: ServerBuilder<*>.() -> Unit = {},

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

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,67 @@ package kotlinx.rpc.grpc
88

99
import kotlin.time.Duration
1010

11+
/**
12+
* Same as [ManagedChannel], but is platform-exposed.
13+
*/
1114
public expect abstract class ManagedChannelPlatform
1215

16+
/**
17+
* A virtual connection to a conceptual endpoint, to perform RPCs.
18+
* A channel is free to have zero or many actual connections to the endpoint based on configuration,
19+
* load, etc. A channel is also free to determine which actual endpoints to use and may change it every RPC,
20+
* permitting client-side load balancing.
21+
*
22+
* Provides lifecycle management.
23+
*/
1324
public interface ManagedChannel {
25+
/**
26+
* Returns whether the channel is shutdown.
27+
* Shutdown channels immediately cancel any new calls but may still have some calls being processed.
28+
*/
1429
public val isShutdown: Boolean
30+
31+
/**
32+
* Returns whether the channel is terminated.
33+
* Terminated channels have no running calls and relevant resources released (like TCP connections).
34+
*/
1535
public val isTerminated: Boolean
1636

37+
/**
38+
* Waits for the channel to become terminated, giving up if the timeout is reached.
39+
*
40+
* @return whether the channel is terminated, as would be done by [isTerminated].
41+
*/
1742
public suspend fun awaitTermination(duration: Duration): Boolean
1843

44+
/**
45+
* Initiates an orderly shutdown in which preexisting calls continue but new calls are immediately canceled.
46+
*
47+
* @return `this`
48+
*/
1949
public fun shutdown(): ManagedChannel
50+
51+
/**
52+
* Initiates a forceful shutdown in which preexisting and new calls are canceled.
53+
* Although forceful, the shutdown process is still not instantaneous; [isTerminated] will likely
54+
* return `false` immediately after this method returns.
55+
*
56+
* @return this
57+
*/
2058
public fun shutdownNow(): ManagedChannel
2159

60+
/**
61+
* Exposes the platform-specific version of this API.
62+
*/
2263
public val platformApi: ManagedChannelPlatform
2364
}
2465

66+
/**
67+
* Builder class for [ManagedChannel].
68+
*/
2569
public expect abstract class ManagedChannelBuilder<T : ManagedChannelBuilder<T>>
2670

27-
public expect fun ManagedChannelBuilder(name: String, port: Int): ManagedChannelBuilder<*>
28-
public expect fun ManagedChannelBuilder(target: String): ManagedChannelBuilder<*>
71+
internal expect fun ManagedChannelBuilder(name: String, port: Int): ManagedChannelBuilder<*>
72+
internal expect fun ManagedChannelBuilder(target: String): ManagedChannelBuilder<*>
2973

30-
public expect fun ManagedChannelBuilder<*>.buildChannel(): ManagedChannel
74+
internal expect fun ManagedChannelBuilder<*>.buildChannel(): ManagedChannel

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66

77
package kotlinx.rpc.grpc
88

9+
/**
10+
* Registry of services and their methods used by servers to dispatching incoming calls.
11+
*/
912
public expect abstract class HandlerRegistry
1013

1114
@Suppress("RedundantConstructorKeyword")
12-
public expect class MutableHandlerRegistry constructor() : HandlerRegistry {
15+
internal expect class MutableHandlerRegistry constructor() : HandlerRegistry {
1316
internal fun addService(@Suppress("unused") service: ServerServiceDefinition): ServerServiceDefinition?
1417
}

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

Lines changed: 87 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,106 @@ package kotlinx.rpc.grpc
88

99
import kotlin.time.Duration
1010

11+
/**
12+
* Platform-specific gRPC server builder.
13+
*/
1114
public expect abstract class ServerBuilder<T : ServerBuilder<T>> {
15+
/**
16+
* Adds a service implementation to the handler registry.
17+
*
18+
* @return `this`
19+
*/
1220
public abstract fun addService(service: ServerServiceDefinition): T
1321

22+
/**
23+
* Sets a fallback handler registry that will be looked up in if a method is not found in the
24+
* primary registry.
25+
* The primary registry (configured via [addService]) is faster but immutable.
26+
* The fallback registry is more flexible and allows implementations to mutate over
27+
* time and load services on-demand.
28+
*
29+
* @return `this`
30+
*/
1431
public abstract fun fallbackHandlerRegistry(registry: HandlerRegistry?): T
1532
}
1633

1734
internal expect fun ServerBuilder(port: Int): ServerBuilder<*>
1835

36+
/**
37+
* Server for listening for and dispatching incoming calls.
38+
* It is not expected to be implemented by application code or interceptors.
39+
*/
1940
public interface Server {
41+
/**
42+
* Returns the port number the server is listening on.
43+
* This can return -1 if there is no actual port or the result otherwise doesn't make sense.
44+
* The result is undefined after the server is terminated.
45+
* If there are multiple possible ports, this will return one arbitrarily.
46+
* Implementations are encouraged to return the same port on each call.
47+
*
48+
* @throws [IllegalStateException] – if the server has not yet been started.
49+
*/
2050
public val port: Int
51+
52+
/**
53+
* Returns whether the server is shutdown.
54+
* Shutdown servers reject any new calls but may still have some calls being processed.
55+
*/
2156
public val isShutdown: Boolean
57+
58+
/**
59+
* Returns whether the server is terminated.
60+
* Terminated servers have no running calls and relevant resources released (like TCP connections).
61+
*/
2262
public val isTerminated: Boolean
2363

24-
public fun start() : Server
25-
public fun shutdown() : Server
26-
public fun shutdownNow() : Server
27-
public suspend fun awaitTermination(duration: Duration = Duration.INFINITE) : Server
64+
/**
65+
* Bind and start the server.
66+
* After this call returns, clients may begin connecting to the listening socket(s).
67+
* @return `this`
68+
* @throws IllegalStateException if already started or shut down
69+
* @throws IOException if unable to bind
70+
*/
71+
// TODO, What is IOException in KMP? KRPC-163
72+
public fun start(): Server
73+
74+
/**
75+
* Initiates an orderly shutdown in which preexisting calls continue but new calls are rejected.
76+
* After this call returns, this server has released the listening socket(s) and may be reused by
77+
* another server.
78+
*
79+
* Note that this method will not wait for preexisting calls to finish before returning.
80+
* [awaitTermination] needs to be called to wait for existing calls to finish.
81+
*
82+
* Calling this method before [start] will shut down and terminate the server like
83+
* normal, but prevents starting the server in the future.
84+
*
85+
* @return `this`
86+
*/
87+
public fun shutdown(): Server
88+
89+
/**
90+
* Initiates a forceful shutdown in which preexisting and new calls are rejected. Although
91+
* forceful, the shutdown process is still not instantaneous; [isTerminated] will likely
92+
* return `false` immediately after this method returns. After this call returns, this
93+
* server has released the listening socket(s) and may be reused by another server.
94+
*
95+
* Calling this method before [start] will shut down and terminate the server like
96+
* normal, but prevents starting the server in the future.
97+
*
98+
* @return `this`
99+
*/
100+
public fun shutdownNow(): Server
101+
102+
/**
103+
* Waits for the server to become terminated, giving up if the timeout is reached.
104+
*
105+
* Calling this method before [start] or [shutdown] is permitted and doesn't
106+
* change its behavior.
107+
*
108+
* @return `this`
109+
*/
110+
public suspend fun awaitTermination(duration: Duration = Duration.INFINITE): Server
28111
}
29112

30113
internal expect fun Server(builder: ServerBuilder<*>): Server

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,7 @@
66

77
package kotlinx.rpc.grpc
88

9+
/**
10+
* Definition of a service to be exposed via a Server.
11+
*/
912
public expect class ServerServiceDefinition

0 commit comments

Comments
 (0)