drainAndClose

suspend fun drainAndClose()

close, after every local write's frame has been handed to the Seam. The teardown to use when the last write matters, such as a departure notice: close alone would cancel it.

Suspends until no apply send is in flight, then closes. Bound it with your own timeout, because a seam can hold a send for as long as it likes:

quilter.apply(departingPresence)
withTimeoutOrNull(2.seconds) { quilter.drainAndClose() }

It always ends closed. If the caller is cancelled or its timeout fires, the replica closes anyway, which cancels whatever is still in flight, and the cancellation goes on to the caller. A send stuck in the seam therefore cannot leave an unclosed replica behind.

What a normal return promises: every apply that returned normally has had its Seam.broadcast call finish. It does not promise delivery, for three reasons, and this method reports none of them:

  • A returning broadcast means the seam accepted the frame. Whether the seam then delivers it before its own close is the seam's business, not this replica's.

  • A broadcast that throws is logged and counted as finished, exactly as it is on the live path.

  • A send cancelled by something else counts too: a plain close from elsewhere, the parent scope being cancelled, or the Seam tearing, which closes this replica by itself. Each of those ends the wait instead of hanging it.

A write that races the drain is drained too. apply, mutate and mutateOrSkip keep working until the moment of closing, and the check that nothing is in flight is atomic with the close, so every write either has its send waited for or throws IllegalStateException because the replica has closed. No write is accepted and then silently lost. This was chosen over refusing writes from the moment the drain starts, because that would turn an in-flight send into a window where a RgaGcCoordinator compaction, or anything else reacting to state, throws. The cost is that writers who never leave a gap between sends keep the drain waiting, which is one more reason to bound it. Stop your own writers first if you can.

Only local writes hold it open. Acks, digests, gossip, retries and a QuiltMessage.Resend's retransmission are all cancelled at the close, so a peer's traffic cannot keep a departing replica alive. Inbound frames are still applied until then.

Idempotent and safe to call concurrently, or after close. Do not call it from inside the Seam's own broadcast: it would be waiting for itself.

Samples

runTest(
    StandardTestDispatcher(),
    timeout = TEST_WEDGE_BACKSTOP,
) {
    val loom = InMemoryLoom()
    val seamAlice = loom.host(Pattern("table"))
    val seamBob = loom.join(InMemoryTag("bob"))

    val serializer = LWWMap.serializer(serializer<String>(), serializer<String>())
    val cfg = QuilterConfig(expectVirtualTime = true)
    val alice = Quilter(seamAlice, LWWMap.empty<String, String>(), serializer, backgroundScope, config = cfg)
    val bob = Quilter(seamBob, LWWMap.empty<String, String>(), serializer, backgroundScope, config = cfg)
    kotlinx.coroutines.delay(1)

    // Publish the goodbye, then wait for it to reach the seam before closing.
    alice.mutate { it.set(alice.replica, 1L, "alice", "left") }
    kotlinx.coroutines.withTimeout(5.seconds) { alice.drainAndClose() }

    kotlinx.coroutines.delay(1)
    assertEquals("left", bob.state.value["alice"])
}