Skip to content
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

fix: avoid db query on sync mode all #527

Merged
merged 1 commit into from
Jun 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion app/port/controller/package/DownloadPackageVersionTar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import { FULLNAME_REG_STRING, getScopeAndName } from '../../../common/PackageUtil';
import { NFSAdapter } from '../../../common/adapter/NFSAdapter';
import { PackageManagerService } from '../../../core/service/PackageManagerService';
import { SyncMode } from '../../../common/constants';

@HTTPController()
export class DownloadPackageVersionTarController extends AbstractController {
Expand All @@ -28,11 +29,17 @@
method: HTTPMethodEnum.GET,
})
async download(@Context() ctx: EggContext, @HTTPParam() fullname: string, @HTTPParam() filenameWithVersion: string) {
// try nfs url first, avoid db query
// tgz file storeKey: `/packages/${this.fullname}/${version}/${filename}`
const version = this.getAndCheckVersionFromFilename(ctx, fullname, filenameWithVersion);
const storeKey = `/packages/${fullname}/${version}/${filenameWithVersion}.tgz`;
const downloadUrl = await this.nfsAdapter.getDownloadUrl(storeKey);
if (this.config.cnpmcore.syncMode === SyncMode.all && downloadUrl) {
// try nfs url first, avoid db query
this.packageManagerService.plusPackageVersionCounter(fullname, version);
ctx.redirect(downloadUrl);
return;
}

Check warning on line 41 in app/port/controller/package/DownloadPackageVersionTar.ts

View check run for this annotation

Codecov / codecov/patch

app/port/controller/package/DownloadPackageVersionTar.ts#L37-L41

Added lines #L37 - L41 were not covered by tests

// check package version in database
const pkg = await this.getPackageEntityByFullname(fullname);
const packageVersion = await this.getPackageVersionEntity(pkg, version);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

此代码 patch 主要是增加了一个条件判断和对对应业务的处理:

  1. 新增 import 语句导入 SyncMode 常量。
  2. 在 download 方法中新增对变量 this.config.cnpmcore.syncMode 的判断,如果该值等于 SyncMode.all 且下载链接存在,会先使用该下载链接以避免数据库查询,然后增加包版本计数器(这里的具体实现需要结合其他代码进行评估),最后进行重定向。
  3. 如果不存在下载链接,则继续检查数据库中是否存在对应的包和版本记录。根据语境,该方法在用户请求下载时会调用。

评估该 patch 后可能含有的缺陷和改进建议:

  • 需要确认 SyncMode.all 判断条件的意义和设计合理性
  • 涉及到 packageManagerService.plusPackageVersionCounter() 功能的实现需要确认其正确性和是否遗漏其他关键操作(例如异常处理、事务等)
  • 需要补充注释来解释相关意思和解释该 patch 是出于何种原因而做出的

Expand Down