-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimeout.js
More file actions
296 lines (273 loc) · 8.89 KB
/
timeout.js
File metadata and controls
296 lines (273 loc) · 8.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
const noop = () => {}
const terr = new Error('timeout')
// use less timers by group 100ms
const timeout = (ms) => {
let timer = null
const timedout = new Promise((res, rej) => {
const now = Date.now()
let next = now + ms
next = next - (next % 100)
next = (100 + next) - now
timer = setTimeout(rej, next, terr)
})
return [timer, timedout]
}
// timeouts in ms
const defaults = {
default: 1_500,
open: undefined,
close: undefined,
txn: undefined,
commit: undefined,
abort: undefined,
append: undefined,
appendBatch: undefined,
lock: undefined,
trim: undefined,
iter: undefined,
del: undefined,
}
// wrap a log with timeouts
class TimeoutLog {
constructor(log, opts={}) {
opts = { ...defaults, ...opts }
this._openMs = opts.open ?? opts.default
this._closeMs = opts.close ?? opts.default
this._txnMs = opts.txn ?? opts.default
this._commitMs = opts.commit ?? this._txnMs
this._abortMs = opts.abort ?? this._txnMs
this._appendMs = opts.append ?? opts.default
this._appendBatchMs = opts.appendBatch ?? opts.default
this._lockMs = opts.lock ?? this._txnMs
this._trimMs = opts.trim ?? opts.default
this._iterMs = opts.iter ?? opts.default
this._delMs = opts.del ?? opts.default
this.log = log
this.name = log.name
this.path = log.path
this.seq = null
this.head = null
this.iterators = []
}
get isOpen() {
return this.log.isOpen
}
_sync(ok) {
this.seq = this.log.seq
this.head = this.log.head
return ok
}
open() {
const { path } = this
const [timer, timedout] = timeout(this._openMs)
const work = new Promise((res, rej) => {
timedout.catch((err) => rej(new Error(`${path} open timeout`)))
this.log.open().then(res).catch(rej)
})
work.catch(noop).finally(() => clearTimeout(timer))
return work.then(() => this._sync()).catch((err) => {
this._sync()
return Promise.reject(err)
})
}
close() {
const { path } = this
this.iterators.forEach((iter) => iter.close())
this.iterators = []
const [timer, timedout] = timeout(this._closeMs)
const work = new Promise((res, rej) => {
timedout.catch((err) => rej(new Error(`${path} close timeout`)))
this.log.close().then(res).catch(rej)
})
work.catch(noop).finally(() => clearTimeout(timer))
return work.then(() => this._sync()).catch((err) => {
this._sync()
return Promise.reject(err)
})
}
_wrapTxn(txn) {
const { path } = this
const append = (data, seq) => {
let [timer, timedout] = timeout(this._appendMs)
timedout = timedout.catch(() => Promise.reject(new Error(`${path} txn append timeout`)))
let work = txn.append(data, seq)
work = Promise.race([timedout, work])
work.catch(noop).finally(() => clearTimeout(timer))
return work.then((ok) => this._sync(ok)).catch((err) => {
this._sync()
return Promise.reject(err)
})
}
const appendBatch = (data, seq) => {
let [timer, timedout] = timeout(this._appendBatchMs)
timedout = timedout.catch(() => Promise.reject(new Error(`${path} txn appendBatch timeout`)))
let work = txn.appendBatch(data, seq)
work = Promise.race([timedout, work])
work.catch(noop).finally(() => clearTimeout(timer))
return work.then((ok) => this._sync(ok)).catch((err) => {
this._sync()
return Promise.reject(err)
})
}
const lock = (seq) => {
let [timer, timedout] = timeout(this._lockMs)
timedout = timedout.catch(() => Promise.reject(new Error(`${path} txn lock timeout`)))
let work = txn.lock(seq)
work = Promise.race([timedout, work])
work.catch(noop).finally(() => clearTimeout(timer))
return work.then((ok) => this._sync(ok)).catch((err) => {
this._sync()
return Promise.reject(err)
})
}
const commit = () => {
let [timer, timedout] = timeout(this._commitMs)
timedout = timedout.catch(() => Promise.reject(new Error(`${path} txn commit timeout`)))
let work = txn.commit()
work = Promise.race([timedout, work])
work.catch(noop).finally(() => clearTimeout(timer))
return work.then((ok) => this._sync(ok)).catch((err) => {
this._sync()
return Promise.reject(err)
})
}
const abort = () => {
let [timer, timedout] = timeout(this._abortMs)
timedout = timedout.catch(() => Promise.reject(new Error(`${path} txn abort timeout`)))
let work = txn.abort()
work = Promise.race([timedout, work])
work.catch(noop).finally(() => clearTimeout(timer))
return work.then((ok) => this._sync(ok)).catch((err) => {
this._sync()
return Promise.reject(err)
})
}
return { append, appendBatch, lock, commit, abort }
}
txn(seq=undefined) {
const { path } = this
const [timer, timedout] = timeout(this._txnMs)
const work = new Promise((res, rej) => {
timedout.catch((err) => rej(new Error(`${path} txn timeout`)))
this.log.txn(seq).then((txn) => {
const wrap = this._wrapTxn(txn)
res(wrap)
}).catch(rej)
})
work.catch(noop).finally(() => clearTimeout(timer))
return work.then((ok) => this._sync(ok)).catch((err) => {
this._sync()
return Promise.reject(err)
})
}
append(data, seq=null) {
const { path } = this
const [timer, timedout] = timeout(this._appendMs)
const work = new Promise((res, rej) => {
timedout.catch((err) => rej(new Error(`${path} append timeout`)))
this.log.append(data, seq).then(res).catch(rej)
})
work.catch(noop).finally(() => clearTimeout(timer))
return work.then((ok) => this._sync(ok)).catch((err) => {
this._sync()
return Promise.reject(err)
})
}
appendBatch(data, seq=null) {
const { path } = this
const [timer, timedout] = timeout(this._appendBatchMs)
const work = new Promise((res, rej) => {
timedout.catch((err) => rej(new Error(`${path} appendBatch timeout`)))
this.log.appendBatch(data, seq).then(res).catch(rej)
})
work.catch(noop).finally(() => clearTimeout(timer))
return work.then((ok) => this._sync(ok)).catch((err) => {
this._sync()
return Promise.reject(err)
})
}
async trim(seq=-1n, txn=false) {
const { path } = this
if (typeof seq !== 'bigint') { throw new Error(`${path} seq must be big int`) }
if (seq < -1n) { throw new Error(`${path} seq must be >= -1`) }
this.iterators.filter((iter) => iter.last > seq).forEach((iter) => iter.close())
this.iterators = this.iterators.filter((iter) => iter._open)
const [timer, timedout] = timeout(this._trimMs)
const work = new Promise((res, rej) => {
timedout.catch((err) => rej(new Error(`${path} trim timeout`)))
this.log.trim(seq, txn).then(res).catch(rej)
})
work.catch(noop).finally(() => clearTimeout(timer))
return work.then((ok) => this._sync(ok)).catch((err) => {
this._sync()
return Promise.reject(err)
}).then((ok) => {
if (!txn) { return ok }
return this._wrapTxn(ok)
})
}
iter(seq=0n, opts={}) {
const o = {...opts, clazz: true}
const clazz = this.log.iter(seq, o)
opts.iterMs = opts.iterMs ?? this._iterMs
const iter = new TimeoutIterator(this, seq, clazz, opts)
this.iterators = this.iterators.filter((iter) => iter._open)
this.iterators.push(iter)
return opts.clazz ? iter : iter.lazy()
}
del() {
const { path } = this
const [timer, timedout] = timeout(this._delMs)
const work = new Promise((res, rej) => {
timedout.catch((err) => rej(new Error(`${path} del timeout`)))
this.log.del().then(res).catch(rej)
})
work.catch(noop).finally(() => clearTimeout(timer))
return work.then((ok) => this._sync(ok)).catch((err) => {
this._sync()
return Promise.reject(err)
})
}
}
class TimeoutIterator {
constructor(log, seq, clazz, opts) {
opts = { name: 'iter', ...opts }
this.path = `${log.path}-${opts.name}`
this.iterMs = opts.iterMs
this.seq = seq
this.last = log.seq
this._clazz = clazz
this._open = true
}
close() {
this._clazz.close()
this._open = false
}
async *lazy() {
let timer = null
let timedout = null
let next = this.seq
try {
if (!this._open) { return }
const lazy = this._clazz.lazy()
while (next <= this.last) {
if (!this._open) { return }
const arr = timeout(this.iterMs)
timer = arr[0]; timedout = arr[1]
timedout = timedout.catch(() => Promise.reject(new Error(`${this.path} iter timeout`)))
let nextt = lazy.next()
const work = Promise.race([timedout, nextt])
nextt = await work
clearTimeout(timer)
if (!this._open) { return }
if (nextt.done) { break }
next++
yield nextt.value
}
} finally {
clearTimeout(timer)
this.close()
}
}
}
module.exports = { TimeoutLog }