Open
Description
This is related to #75.
After hacking a way a bit trying to adapt kotlinx-rpc
to a serverless architecture, I hit an issue.
To illustrate the problem, lets review the architecture of serverless computing.
However, krpc assumes the traditional dedicated server architecture:
If we look at the KrpcTransport
interface:
interface KrpcTransport {
fun send(message: KrpcTransportMessage)
fun receive(): KrpcTransportMessage
}
KrpcTransportMessage
only encodes the messages itself, and not whether this is the last message.
sealed interface KrpcTransportMessage {
class StringMessage(val value: String) : KrpcTransportMessage
class BinaryMessage(val value: ByteArray) : KrpcTransportMessage
}
As such, there is no way to know whether to enter the last step in the first diagram, and shut down the machine, outside of trying to parse the value
, or counting the amount of sent messages, which is very fragile.
Proposed solution
Add a terminateProcedureCall()
method to KrpcTransport
/ KrpcServer
public abstract class KrpcServer {
+ /**
+ * Notifies that a unit of work has been done, and resources associated with this transport may be safely released.
+ * Implementation may safely ignore this method if they wish to reuse the same resources for multiple units of work (multiple procedure calls).
+ */
+ public open suspend fun terminateProcedureCall(): Unit {}
}
Or alternatively:
public interface KrpcTransport : CoroutineScope {
public suspend fun send(message: KrpcTransportMessage)
public suspend fun receive(): KrpcTransportMessage
+ public suspend fun terminateProcedureCall(): Unit {}
public suspend fun receiveCatching(): Result<KrpcTransportMessage> {
return runCatching { receive() }
}
}