ServerClusterReconnect

class ServerClusterReconnect(endpoints: List<Tag>, selector: EndpointSelector = RoundRobinEndpointSelector(startIndex = 0))

Reconnect helper for a joiner that faces a static cluster of server endpoints.

Connects to one endpoint at a time. On transport tear, advances to the next endpoint per the selector (default: deterministic round-robin). On reconnect, the caller presents the pendingToken — which survives endpoint changes because it is keyed on RoomId, not on any specific server identity.

Usage

val reconnect = ServerClusterReconnect(
endpoints = listOf(tag0, tag1, tag2),
)

// Initial connection:
val seam = reconnect.connect(loom)
// ... admit handshake; on success, register the token:
reconnect.setToken(room.resumeToken!!)

// On transport tear:
reconnect.onTransportTear()
val newSeam = reconnect.connect(loom)
// Present the token for resume on the new endpoint:
room.resume(reconnect.pendingToken()!!)
// On success, clear the token (it has been consumed):
reconnect.clearToken()

Thread safety

All mutable state is guarded by an atomicfu reentrantLock. Suspend calls (connect) are issued outside the locked section to comply with the lock-discipline rule: no suspending code inside a locked region.

This helper is caller-orchestrated: it tracks the current endpoint and the pending ResumeToken, and connect delegates straight to Loom.join on the calling coroutine. It launches no background work, so it owns no kotlinx.coroutines.CoroutineScope. If a future iteration makes it manage the connect → tear → reconnect loop itself, a required scope parameter is reintroduced then (and actually used).

Parameters

endpoints

Ordered list of server endpoint Tags. Must be non-empty.

selector

Strategy that determines which index to activate on each advanceEndpoint call. Default: deterministic round-robin starting at index 0.

Constructors

Link copied to clipboard
constructor(endpoints: List<Tag>, selector: EndpointSelector = RoundRobinEndpointSelector(startIndex = 0))

Functions

Link copied to clipboard

Advance to the next endpoint as determined by selector.

Link copied to clipboard

Clear the pending token after a successful resume.

Link copied to clipboard
suspend fun connect(loom: Loom): Seam

Weave a new Seam against the currentEndpoint using loom.

Link copied to clipboard

The endpoint this helper is currently targeting.

Link copied to clipboard

React to a transport tear by advancing to the next endpoint.

Link copied to clipboard

Returns the ResumeToken set by setToken, or null if none is registered.

Link copied to clipboard
fun setToken(resumeToken: ResumeToken)

Register a ResumeToken acquired after a successful admit handshake.