Skip to content
Leader docs0.4

Ktor integration

Latest stable Based on Leader release 0.4.0

Install an application-scoped suspend elector and bind periodic attempts to Ktor shutdown.

LeaderElectionPlugin exposes a configured SuspendLeaderElector through the Ktor application. The application decides which backend client and elector it owns. Keep one lifecycle owner so plugin shutdown does not race a separately closed client.

Application.leaderScheduled(lockName, period) { ... } launches periodic election attempts. Only the elected instance runs the body, and the job is cancelled on ApplicationStopped. Set lease longer than one normal iteration or use a supported extension strategy for variable work.

The helper does not persist missed schedules, serialize retries, or make side effects idempotent. Record durable work state if a run must be recovered. Test two Ktor applications against one backend and verify both exclusive execution and shutdown cancellation.