Skip to content

Provide some way for a KrpcTransport/KrpcServer implementation to know if a procedure call is done #289

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
natanfudge opened this issue Feb 17, 2025 · 4 comments
Assignees
Labels
feature New feature or request

Comments

@natanfudge
Copy link

natanfudge commented Feb 17, 2025

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.

Image

However, krpc assumes the traditional dedicated server architecture:

Image

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() }
    }
}
@natanfudge natanfudge added the feature New feature or request label Feb 17, 2025
@natanfudge natanfudge changed the title Provide some way for a KrpcTransport implementation to know if a procedure call is done Provide some way for a KrpcTransport/KrpcServer implementation to know if a procedure call is done Feb 17, 2025
@Mr3zee
Copy link
Collaborator

Mr3zee commented Feb 18, 2025

Hey @natanfudge ! Thanks for the interest in the lib!
I have a question: I'm not sure I understand, when terminateProcedureCall is intended to be called?

@natanfudge
Copy link
Author

natanfudge commented Feb 18, 2025

@Mr3zee terminateProcedureCall must be called when handling the procedure call is done.
The workflow should be like this:

  • Call KrpcTransport.receive() to get a request.
  • Parse the request and send it to an RPC service implementation.
  • Send the response(s) with KrpcTransport.send() (this includes dispatching events made by emitting to streams)
  • Release resource with terminateProcedureCall(). By calling this, krpc acknowledges the hosting computer may shut down immediately.

@natanfudge
Copy link
Author

@Mr3zee Any thoughts?

@Mr3zee
Copy link
Collaborator

Mr3zee commented Mar 12, 2025

Hi, @natanfudge haven't had a chance to think it though yet, when will do - I'll let you know. Thank you for the issue, though!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants