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: unpkg lock #629

Merged
merged 1 commit into from
Dec 25, 2023
Merged
Show file tree
Hide file tree
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
14 changes: 13 additions & 1 deletion app/core/service/PackageVersionFileService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import { PackageVersion } from '../entity/PackageVersion';
import { Package } from '../entity/Package';
import { PackageManagerService } from './PackageManagerService';
import { CacheAdapter } from '../../common/adapter/CacheAdapter';
import { ConflictError } from 'egg-errors';

@SingletonProto({
accessLevel: AccessLevel.PUBLIC,
Expand All @@ -34,6 +36,8 @@
private readonly distRepository: DistRepository;
@Inject()
private readonly packageManagerService: PackageManagerService;
@Inject()
private readonly cacheAdapter: CacheAdapter;

async listPackageVersionFiles(pkgVersion: PackageVersion, directory: string) {
await this.#ensurePackageVersionFilesSync(pkgVersion);
Expand All @@ -50,8 +54,16 @@
async #ensurePackageVersionFilesSync(pkgVersion: PackageVersion) {
const hasFiles = await this.packageVersionFileRepository.hasPackageVersionFiles(pkgVersion.packageVersionId);
if (!hasFiles) {
await this.syncPackageVersionFiles(pkgVersion);
const lockRes = await this.cacheAdapter.usingLock(`${pkgVersion.packageVersionId}:syncFiles`, 60, async () => {
Copy link
Member

Choose a reason for hiding this comment

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

哈哈,这个 api 设计没有 tryLock 那个好。

await this.syncPackageVersionFiles(pkgVersion);
});
// lock fail
if (!lockRes) {
this.logger.warn('[package:version:syncPackageVersionFiles] check lock fail');
throw new ConflictError('Package version file sync is currently in progress. Please try again later.');
}
}

}

// 基于 latest version 同步 package readme
Expand Down Expand Up @@ -93,9 +105,9 @@
await fs.rm(tarFile, { force: true });
await fs.rm(tmpdir, { recursive: true, force: true });
} catch (err) {
this.logger.warn('[PackageVersionFileService.syncPackageReadme:warn] remove tmpdir: %s, error: %s',
tmpdir, err);
}

Check warning on line 110 in app/core/service/PackageVersionFileService.ts

View check run for this annotation

Codecov / codecov/patch

app/core/service/PackageVersionFileService.ts#L108-L110

Added lines #L108 - L110 were not covered by tests
}
}

Expand Down Expand Up @@ -224,11 +236,11 @@
if (markdownRE.test(file)) {
return file;
} else if (file.toLowerCase() === 'README') {
fallback = i;
}

Check warning on line 240 in app/core/service/PackageVersionFileService.ts

View check run for this annotation

Codecov / codecov/patch

app/core/service/PackageVersionFileService.ts#L239-L240

Added lines #L239 - L240 were not covered by tests
}
// prefer README.md, followed by README; otherwise, return
// the first filename (which could be README)
return files[fallback];
}

Check warning on line 245 in app/core/service/PackageVersionFileService.ts

View check run for this annotation

Codecov / codecov/patch

app/core/service/PackageVersionFileService.ts#L243-L245

Added lines #L243 - L245 were not covered by tests
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { strict as assert } from 'node:assert';
import { setTimeout } from 'node:timers/promises';
import { app, mock } from 'egg-mock/bootstrap';
import { TestUtil } from '../../../../test/TestUtil';
import { PackageVersionFileService } from '../../../../app/core/service/PackageVersionFileService';

describe('test/port/controller/PackageVersionFileController/listFiles.test.ts', () => {
let publisher;
Expand Down Expand Up @@ -331,5 +333,24 @@ describe('test/port/controller/PackageVersionFileController/listFiles.test.ts',
assert(!res.headers['cache-control']);
assert.equal(res.body.error, '[NOT_FOUND] @cnpm/[email protected] not found');
});

it('should conflict when syncing', async () => {
mock(app.config.cnpmcore, 'enableUnpkg', true);
const { pkg } = await TestUtil.createPackage({
name: '@cnpm/banana',
version: '1.0.0',
versionObject: {
description: 'mock mock',
},
});
let called = 0;
mock(PackageVersionFileService.prototype, 'syncPackageVersionFiles', async () => {
called++;
await setTimeout(50);
});
const resList = await Promise.all([ 0, 1 ].map(() => app.httpRequest().get(`/${pkg.name}/1.0.0/files/`)));
assert.equal(called, 1);
assert.equal(resList.filter(res => res.status === 409 && res.body.error === '[CONFLICT] Package version file sync is currently in progress. Please try again later.').length, 1);
});
});
});
Loading