-
Notifications
You must be signed in to change notification settings - Fork 156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(av-cliper): concurrency download for hls-loader #140
Conversation
} | ||
|
||
async function getSegmentBuffer(url: string) { | ||
return segmentBufferFetchqueue[url].then((value) => value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
替换为 return await segmentBufferFetchqueue[url]
|
||
const runTask = createTaskQueue(concurrency); | ||
|
||
for (const [, gData] of Object.entries(segGroup)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
复用 load 中转换的数据 filterSegGroup
,有些片段是无需下载的。
@@ -3,7 +3,7 @@ import { Parser } from 'm3u8-parser'; | |||
/** | |||
* 创建一个 HLS 资源加载器 | |||
*/ | |||
export async function createHLSLoader(m3u8URL: string) { | |||
export async function createHLSLoader(m3u8URL: string, concurrency = 10) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
参数 concurrency = 10
移到内部,默认不对外开放,若有有需求再讨论开放。
可以简化 API 、降低破坏性变更几率。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
需要,不同业务方期望的并行数量应该是不一样的
const task = queue.shift(); | ||
running++; | ||
try { | ||
if (task) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
替换为 await task?.()
await task(); | ||
} | ||
} catch (err) { | ||
console.error(err); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
替换为 Log.error
async function downloadSegGroup() { | ||
function createTaskQueue(concurrency: number) { | ||
let running = 0; | ||
const queue = [] as Function[]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
替换为 as Array<() => Promise<void>>
await task(); | ||
} | ||
} catch (err) { | ||
console.error(err); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
异常时需要中断流,否则流永远不会结束,且使用方无法感知到错误。
} catch (err) { | ||
console.error(err); | ||
// 异常时中断流 | ||
ctrl.close(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
直接 close 不会抛出错误,应使用 ctrl.error(err)
@@ -65,7 +76,7 @@ export async function createHLSLoader(m3u8URL: string, concurrency = 10) { | |||
} | |||
|
|||
async function getSegmentBuffer(url: string) { | |||
return segmentBufferFetchqueue[url].then((value) => value); | |||
return segmentBufferFetchqueue[url]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
移除 async 或 添加 await, await segmentBufferFetchqueue[url]
或移除该函数,因为只有一个行代码
console.error(err); | ||
// 异常时中断流 | ||
ctrl.close(); | ||
Log.error(err); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
一个错误不会完全终止后续并发
抛出错误后,应该加一个状态位,阻止后续的并发请求,或者说阻止后续 next 的执行
如果可以的话,补一个单测覆盖错误场景
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个已经有的,next 已经放到 try 里了,我补下单测
@@ -77,6 +139,7 @@ export async function createHLSLoader(m3u8URL: string) { | |||
actualEndTime, | |||
stream: new ReadableStream<Uint8Array>({ | |||
start: async (ctrl) => { | |||
downloadSegGroup(filterSegGroup, ctrl); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
应该 download segments,而不是 filterSegGroup。
} | ||
|
||
async function getSegmentBuffer(url: string) { | ||
return segmentBufferFetchqueue[url]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
用完的数据需要删除引用,否则不会被回收,增加内存占用。
delete segmentBufferFetchqueue[url]
No description provided.