fix(xapi): implement Throttling on Xapi/Remote read

This commit is contained in:
Florent BEAUCHAMP
2025-10-06 12:27:21 +00:00
committed by Bastien Nollet
parent 73aa66a219
commit d58efc7b2d
4 changed files with 34 additions and 5 deletions

View File

@@ -0,0 +1,26 @@
import { Throttle } from '@vates/generator-toolbox'
import { Disk, DiskBlock } from './Disk.mjs'
import { DiskPassthrough } from './DiskPassthrough.mjs'
export class ThrottledDisk extends DiskPassthrough {
#throttle: Throttle
constructor(source: Disk, throttle: Throttle) {
super(source)
this.#throttle = throttle
}
async buildDiskBlockGenerator(): Promise<AsyncGenerator<DiskBlock>> {
const generator = await this.source.buildDiskBlockGenerator()
//throttle want to be able to know the length of the data
async function* generatorWithLength() {
for await (const { index, data } of generator) {
yield {
index,
data,
length: data.length,
}
}
}
const throttledGenerator = this.#throttle.createThrottledGenerator(generatorWithLength())
return throttledGenerator as AsyncGenerator<DiskBlock>
}
}

View File

@@ -9,4 +9,5 @@ export { RawDisk } from './RawDisk.mjs'
export { ReadAhead } from './ReadAhead.mjs'
export { SynchronizedDisk } from './SynchronizedDisk.mjs'
export { TimeoutDisk } from './Timeout.mjs'
export { ThrottledDisk } from './Throttled.mjs'
export { ProgressHandler } from './ProgressHandler.mjs'