Skip to content

Would it be possible to not use coroutines? #11

Closed

Description

@cies

http4k does not use coroutines. so i put all queries in runBlocking { ... }. it works lovely, but if i can do without the dependency that would be more awesomer.

Activity

  1. cies commented on Jun 15, 2025

    @cies
    Author

    I've added these to my project:

    package lib.blockingterpal
    
    import io.exoquery.sql.Action
    import io.exoquery.sql.ActionReturning
    import io.exoquery.sql.BatchAction
    import io.exoquery.sql.BatchActionReturning
    import io.exoquery.sql.Query
    import io.exoquery.sql.TerpalDriver
    import kotlinx.coroutines.runBlocking
    
    // Copied from io.exoquery.sql.TerpalDriver, added `runBlocking { ... }`.
    
    fun <T> Query<T>.runOn(ctx: TerpalDriver) = runBlocking { ctx.run(this@runOn) }
    fun <T> Query<T>.streamOn(ctx: TerpalDriver) = runBlocking { ctx.stream(this@streamOn) }
    fun <T> Query<T>.runRawOn(ctx: TerpalDriver) = runBlocking { ctx.runRaw(this@runRawOn) }
    fun Action.runOn(ctx: TerpalDriver) = runBlocking { ctx.run(this@runOn) }
    fun <T> ActionReturning<T>.runOn(ctx: TerpalDriver) = runBlocking { ctx.run(this@runOn) }
    fun BatchAction.runOn(ctx: TerpalDriver) = runBlocking { ctx.run(this@runOn) }
    fun <T> BatchActionReturning<T>.runOn(ctx: TerpalDriver) = runBlocking { ctx.run(this@runOn) }
    fun <T> BatchActionReturning<T>.streamOn(ctx: TerpalDriver) = runBlocking { ctx.stream(this@streamOn) }

    Now my application code is not longer littered with runBlocking { ... } calls.

  2. deusaquilus commented on Jun 20, 2025

    @deusaquilus
    Member

    I can't get rid of coroutines. I rely on a custom coroutine context in order to do transactions (i.e. the session a.k.a. java.sql.Connection object is stored inside one). The alternative is to either use thread locals or monads. The former is very difficult to get right in KMP, the latter has a long and storied history that ultimately ends anticlimactically.

    Your approach with runBlocking is the best thing that I can think of. Going to close the issue for now.

  3. cies commented on Jun 23, 2025

    @cies
    Author

    the latter has a long and storied history that ultimately ends anticlimactically.

    Lol.

    Thankfully I can live with the 1.5MB dependency of kotlin-coroutines. Just wanted to make sure it is unavoidable'ish. Thanks for answering.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions