Skip to content

Commit

Permalink
feat: 加入多线程下载
Browse files Browse the repository at this point in the history
  • Loading branch information
bangbang93 committed Jan 25, 2024
1 parent 4500f08 commit 94e952d
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ export class Cluster {
total: totalSize,
width: 80,
})
for (const [i, file] of missingFiles.entries()) {
const parallel = parseInt(process.env.SYNC_PARALLEL ?? '1', 10) || 1
const noopen = process.env.FORCE_NOOPEN === 'true' && parallel === 1 ? '1' : ''
await Bluebird.map(missingFiles, async (file, i) => {
const path = join(this.cacheDir, file.hash.substring(0, 2), file.hash)
if (process.stderr.isTTY) {
bar.interrupt(`${colors.green('downloading')} ${colors.underline(file.path)}`)
Expand All @@ -128,7 +130,7 @@ export class Cluster {
let lastProgress = 0
const res = await this.got.get<Buffer>(file.path.substring(1), {
searchParams: {
noopen: process.env.FORCE_NOOPEN === 'true' ? 1 : '',
noopen,
},
retry: {
limit: 10,
Expand All @@ -143,7 +145,9 @@ export class Cluster {
throw new Error(`文件${file.path}校验失败`)
}
await fse.outputFile(path, res.body)
}
}, {
concurrency: parallel,
})
await this.gc(fileList)
}

Expand Down

0 comments on commit 94e952d

Please sign in to comment.