Skip to content

Instantly share code, notes, and snippets.

@polRk
Created March 3, 2026 12:34
Show Gist options
  • Select an option

  • Save polRk/8fdc3e28c18dd9aeec593d71aed3e25f to your computer and use it in GitHub Desktop.

Select an option

Save polRk/8fdc3e28c18dd9aeec593d71aed3e25f to your computer and use it in GitHub Desktop.
YDB Coordination Service Design
// Уровень 2: Session
await using session = await client.openSession(path, options?)
// [asyncDispose] → session.close()
// Semaphore — создаётся на сессии, не disposable сам по себе
let semaphore = session.semaphore(name, { limit })
semaphore.update(data, signal?)
semaphore.delete(options?, signal?)
semaphore.describe(options?, signal?)
// Lease — результат acquire, disposable
await using lease = await semaphore.acquire({ count, data }, signal?)
await using lease = await semaphore.tryAcquire({ count, data }, signal?) // null если не удалось
lease.signal // абортится при потере сессии
// [asyncDispose] → lease.release()
// watch — async iterable, прерывается через signal
for await (let desc of semaphore.watch({ owners, data }, signal?)) {
desc.owners
desc.data
}
// Уровень 3: Mutex
let mutex = session.mutex(name) // ephemeral, limit=MAX, count=MAX
await using lock = await mutex.lock(signal?)
await using lock = await mutex.tryLock(signal?) // null если занят
lock.signal
// [asyncDispose] → lock.release()
// Уровень 3: Election
let election = session.election(name) // semaphore limit=1
// campaign блокирует до победы → Leadership, disposable
await using leadership = await election.campaign(data, signal?)
leadership.signal // абортится при потере лидерства
leadership.proclaim(data, signal?) // update данных
// [asyncDispose] → resign()
// observe — async iterable для всех участников
for await (let state of election.observe(signal?)) {
state.data // данные текущего лидера
state.isMe // я ли лидер
}
// leader — разовый запрос
let leader = await election.leader(signal?) // { data } | null
// Уровень 3: ServiceRegistry
let registry = session.registry(name) // semaphore limit=MAX, ephemeral
// Регистрация себя — disposable
await using entry = await registry.register(data, signal?)
// [asyncDispose] → release
// Наблюдение — async iterable
for await (let endpoints of registry.watch(signal?)) {
endpoints // { data: Uint8Array }[]
}
// Уровень 3: ConfigWatch
let config = session.config(name) // semaphore, watchData
config.publish(data, signal?) // update
for await (let value of config.watch(signal?)) {
value // Uint8Array
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment