Skip to content

Allow suspend calls inside ktor rpc builder #433

@lsafer-meemer

Description

@lsafer-meemer

Is your feature request related to a problem? Please describe.
When registering rpc services, I can't make suspend calls.

Describe the solution you'd like
The code for registering rpc services is executing per connection thus KrpcRoute implements WebSocketSession. Why not just the block suspendable?

I traced the code and it appears that there is no issues with making it suspend other than changing the signature:

// ...
@KtorDsl
public fun Route.rpc(builder: KrpcRoute.() -> Unit) {
    createRpcServer(builder)
}

private fun Route.createRpcServer(rpcRouteBuilder: KrpcRoute.() -> Unit) {
    // ...

    webSocket {
        val rpcRoute = KrpcRoute(this).apply(rpcRouteBuilder)

        // ...
    }
}

full code here

Describe alternatives you've considered

  1. Copy-paste with signature change -> not effective
  2. Using call attributes -> too confusing and error prune

Additional context
I need it to be suspend to decode the access token which needs the keyset of the service which might not be in the cache.

Metadata

Metadata

Assignees

Labels

featureNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions