Skip to content

Commit 39f229d

Browse files
committed
Replace hand-rolled sleep/doWork with node:timers/promises in examples
setTimeout from node:timers/promises accepts { signal } natively — no need to reimplement abort handling manually in every example.
1 parent 5026a6a commit 39f229d

File tree

4 files changed

+18
-79
lines changed

4 files changed

+18
-79
lines changed

examples/coordination/election.js

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { setTimeout as sleep } from 'node:timers/promises'
2+
13
import { CoordinationClient } from '@ydbjs/coordination'
24
import { Driver } from '@ydbjs/core'
35

@@ -30,14 +32,14 @@ async function runLeader(signal) {
3032
console.log('[leader] elected — publishing endpoint')
3133

3234
// Simulate startup time before the real endpoint is known.
33-
await sleep(300, session.signal)
35+
await sleep(300, undefined, { signal: session.signal })
3436

3537
// Update leader data without re-election — all observers see it immediately.
3638
await leadership.proclaim(utf8.encode('worker-a:8080'))
3739
console.log('[leader] proclaimed endpoint: worker-a:8080')
3840

3941
// Hold leadership until the session dies or external signal fires.
40-
await sleep(2_000, leadership.signal)
42+
await sleep(2_000, undefined, { signal: leadership.signal })
4143

4244
console.log('[leader] resigning')
4345
// await using → resign() called automatically here
@@ -148,24 +150,6 @@ async function main() {
148150
}
149151
}
150152

151-
function sleep(ms, signal) {
152-
return new Promise((resolve, reject) => {
153-
if (signal?.aborted) {
154-
return reject(signal.reason)
155-
}
156-
157-
let timer = setTimeout(resolve, ms)
158-
signal?.addEventListener(
159-
'abort',
160-
() => {
161-
clearTimeout(timer)
162-
reject(signal.reason)
163-
},
164-
{ once: true }
165-
)
166-
})
167-
}
168-
169153
main().catch((error) => {
170154
if (error?.message === 'example timeout') return
171155
console.error(error)

examples/coordination/mutex.js

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { setTimeout as sleep } from 'node:timers/promises'
2+
13
import { CoordinationClient } from '@ydbjs/coordination'
24
import { Driver } from '@ydbjs/core'
35

@@ -26,7 +28,7 @@ async function runWorker(id, signal) {
2628
await using lock = await mutex.lock()
2729

2830
console.log(`[worker-${id}] lock acquired — doing exclusive work`)
29-
await sleep(500, lock.signal)
31+
await sleep(500, undefined, { signal: lock.signal })
3032
console.log(`[worker-${id}] work done, releasing`)
3133

3234
// await using → lock.release() called automatically here
@@ -61,7 +63,7 @@ async function tryWork(signal) {
6163

6264
try {
6365
console.log('[tryLock] lock acquired — doing optional work')
64-
await sleep(200, lock.signal)
66+
await sleep(200, undefined, { signal: lock.signal })
6567
console.log('[tryLock] optional work done')
6668
} finally {
6769
await lock.release()
@@ -97,24 +99,6 @@ async function main() {
9799
}
98100
}
99101

100-
function sleep(ms, signal) {
101-
return new Promise((resolve, reject) => {
102-
if (signal?.aborted) {
103-
return reject(signal.reason)
104-
}
105-
106-
let timer = setTimeout(resolve, ms)
107-
signal?.addEventListener(
108-
'abort',
109-
() => {
110-
clearTimeout(timer)
111-
reject(signal.reason)
112-
},
113-
{ once: true }
114-
)
115-
})
116-
}
117-
118102
main().catch((error) => {
119103
console.error(error)
120104
process.exit(1)

examples/coordination/resource-management.js

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
* `await using` equivalent so the difference is unmistakable.
1818
*/
1919

20+
import { setTimeout as sleep } from 'node:timers/promises'
21+
2022
import { CoordinationClient } from '@ydbjs/coordination'
2123
import { Driver } from '@ydbjs/core'
2224

@@ -65,7 +67,7 @@ async function oldLockUsage(signal) {
6567
let lock = await mutex.lock()
6668
try {
6769
console.log('[old] lock acquired, doing work')
68-
await doWork(signal)
70+
await sleep(200, undefined, { signal })
6971
} finally {
7072
await lock.release()
7173
}
@@ -81,7 +83,7 @@ async function newLockUsage(signal) {
8183
await using _lock = await session.mutex('job').lock()
8284

8385
console.log('[new] lock acquired, doing work')
84-
await doWork(signal)
86+
await sleep(200, undefined, { signal })
8587
// lock.release() ← called automatically
8688
// session.close() ← called automatically, after lock is released
8789
}
@@ -129,7 +131,7 @@ async function oldMultiResource(signal) {
129131
let lease = await session.semaphore('quota').acquire({ count: 1 })
130132
try {
131133
console.log('[old] all resources acquired')
132-
await doWork(signal)
134+
await sleep(200, undefined, { signal })
133135
} finally {
134136
await lease.release()
135137
}
@@ -150,7 +152,7 @@ async function newMultiResource(signal) {
150152
await using _lease = await session.semaphore('quota').acquire({ count: 1 })
151153

152154
console.log('[new] all resources acquired')
153-
await doWork(signal)
155+
await sleep(200, undefined, { signal })
154156
// lease.release() ← first
155157
// lock.release() ← second
156158
// session.close() ← last
@@ -171,7 +173,7 @@ async function campaignExample(signal) {
171173
await using leadership = await election.campaign(utf8.encode('worker-a'))
172174

173175
console.log('[leader] elected — doing leader work')
174-
await doWork(leadership.signal)
176+
await sleep(200, undefined, { signal: leadership.signal })
175177

176178
// leadership.resign() called automatically when the block exits —
177179
// whether by normal return, exception, or session expiry.
@@ -239,21 +241,6 @@ async function main() {
239241
}
240242
}
241243

242-
function doWork(signal) {
243-
return new Promise((resolve, reject) => {
244-
if (signal?.aborted) return reject(signal.reason)
245-
let timer = setTimeout(resolve, 200)
246-
signal?.addEventListener(
247-
'abort',
248-
() => {
249-
clearTimeout(timer)
250-
reject(signal.reason)
251-
},
252-
{ once: true }
253-
)
254-
})
255-
}
256-
257244
main().catch((error) => {
258245
console.error(error)
259246
process.exit(1)

examples/coordination/shared-config.js

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { setTimeout as sleep } from 'node:timers/promises'
2+
13
import { CoordinationClient } from '@ydbjs/coordination'
24
import { Driver } from '@ydbjs/core'
35

@@ -105,30 +107,12 @@ async function publishUpdates(signal) {
105107

106108
for (let config of configs) {
107109
// oxlint-disable-next-line no-await-in-loop
108-
await sleep(500, signal)
110+
await sleep(500, undefined, { signal })
109111
// oxlint-disable-next-line no-await-in-loop
110112
await publishConfig(config, signal)
111113
}
112114
}
113115

114-
function sleep(ms, signal) {
115-
return new Promise((resolve, reject) => {
116-
if (signal?.aborted) {
117-
return reject(signal.reason)
118-
}
119-
120-
let timer = setTimeout(resolve, ms)
121-
signal?.addEventListener(
122-
'abort',
123-
() => {
124-
clearTimeout(timer)
125-
reject(signal.reason)
126-
},
127-
{ once: true }
128-
)
129-
})
130-
}
131-
132116
main().catch((error) => {
133117
console.error(error)
134118
process.exit(1)

0 commit comments

Comments
 (0)