mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-27 03:14:11 +00:00
feat(transcode): optimize video operations using ffmpeg http range requests (#393)
* feat(transcode): optimize video operations using ffmpeg http range requests * fix(transcode): preserve non-retryable errors and ensure mapConcurrent settles on rejection
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { mapConcurrent } from './async'
|
||||
|
||||
describe('mapConcurrent', () => {
|
||||
it('should process items and preserve original ordering', async () => {
|
||||
const items = [10, 20, 30, 40, 50]
|
||||
const results = await mapConcurrent(items, 2, async (item, idx) => {
|
||||
return item * 2 + idx
|
||||
})
|
||||
|
||||
expect(results).toEqual([20, 41, 62, 83, 104])
|
||||
})
|
||||
|
||||
it('should respect concurrency limits', async () => {
|
||||
let running = 0
|
||||
let maxRunning = 0
|
||||
const items = Array.from({ length: 20 }, (_, i) => i)
|
||||
|
||||
const results = await mapConcurrent(items, 4, async (item) => {
|
||||
running++
|
||||
maxRunning = Math.max(maxRunning, running)
|
||||
await new Promise((resolve) => setTimeout(resolve, 10))
|
||||
running--
|
||||
return item * 2
|
||||
})
|
||||
|
||||
expect(results).toHaveLength(20)
|
||||
expect(maxRunning).toBeLessThanOrEqual(4)
|
||||
expect(maxRunning).toBeGreaterThan(1)
|
||||
})
|
||||
|
||||
it('should handle empty input array', async () => {
|
||||
const results = await mapConcurrent([], 5, async () => 1)
|
||||
expect(results).toEqual([])
|
||||
})
|
||||
|
||||
it('should wait for all in-flight tasks to settle and not start queued tasks on error', async () => {
|
||||
let task1Settled = false
|
||||
let task2Started = false
|
||||
|
||||
const items = [0, 1, 2]
|
||||
// concurrency 2: items 0 and 1 start immediately. Item 2 is queued.
|
||||
await expect(
|
||||
mapConcurrent(items, 2, async (item) => {
|
||||
if (item === 0) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 10))
|
||||
throw new Error('Task 0 failed')
|
||||
}
|
||||
if (item === 1) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 30))
|
||||
task1Settled = true
|
||||
return 'ok-1'
|
||||
}
|
||||
task2Started = true
|
||||
return 'ok-2'
|
||||
}),
|
||||
).rejects.toThrow('Task 0 failed')
|
||||
|
||||
expect(task1Settled).toBe(true)
|
||||
expect(task2Started).toBe(false)
|
||||
})
|
||||
|
||||
it('should rethrow the first encountered error when multiple concurrent workers fail', async () => {
|
||||
let task1Settled = false
|
||||
|
||||
const items = [0, 1]
|
||||
await expect(
|
||||
mapConcurrent(items, 2, async (item) => {
|
||||
if (item === 0) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 10))
|
||||
throw new Error('First error')
|
||||
}
|
||||
await new Promise((resolve) => setTimeout(resolve, 30))
|
||||
task1Settled = true
|
||||
throw new Error('Second error')
|
||||
}),
|
||||
).rejects.toThrow('First error')
|
||||
|
||||
expect(task1Settled).toBe(true)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
* Maps an array of items with an async mapping function subject to a concurrency limit.
|
||||
* Results are returned in the same order as the input items array.
|
||||
*/
|
||||
export async function mapConcurrent<T, TResult>(
|
||||
items: T[],
|
||||
limit: number,
|
||||
fn: (item: T, index: number) => Promise<TResult>,
|
||||
): Promise<TResult[]> {
|
||||
if (items.length === 0) {
|
||||
return []
|
||||
}
|
||||
|
||||
const concurrency = Math.max(1, Math.min(limit, items.length))
|
||||
const results: TResult[] = new Array(items.length)
|
||||
let nextIndex = 0
|
||||
let firstError: unknown = null
|
||||
let hasError = false
|
||||
|
||||
const workers = Array.from({ length: concurrency }, async () => {
|
||||
while (!hasError && nextIndex < items.length) {
|
||||
const idx = nextIndex++
|
||||
try {
|
||||
results[idx] = await fn(items[idx], idx)
|
||||
} catch (err) {
|
||||
if (!hasError) {
|
||||
hasError = true
|
||||
firstError = err
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
await Promise.all(workers)
|
||||
|
||||
if (hasError) {
|
||||
throw firstError
|
||||
}
|
||||
|
||||
return results
|
||||
}
|
||||
Reference in New Issue
Block a user