withConnection

suspend fun <T> RadioClient.withConnection(teardownTimeout: Duration = 10.seconds, block: suspend RadioClient.() -> T): T(source)

Run block against a connected session, guaranteeing teardown.

Connects, executes block, and always disconnects afterwards — on success, on exception, and on cancellation (the disconnect runs under NonCancellable so a cancelled caller still tears the session down cleanly). This is the structured-concurrency replacement for a blocking use { } idiom, which RadioClient deliberately does not offer:

val owner = client.withConnection {
admin.getOwner().getOrThrow()
}

The client is single-use: after withConnection returns, build a new RadioClient for the next session.

Return

whatever block returns

Since

0.2.0

Parameters

teardownTimeout

upper bound on the guaranteed disconnect (default 10s) — protects the caller from a wedged transport teardown while still covering the polite-goodbye flush and storage close on every healthy path

Throws