8000 fix: unpkg lock by elrrrrrrr · Pull Request #629 · cnpm/cnpmcore · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix: unpkg lock #629

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

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
8000
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 { PackageVersionFile } from '../entity/PackageVersionFile';
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 @@ export class PackageVersionFileService extends AbstractService {
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 @@ export class PackageVersionFileService extends AbstractService {
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
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/foonot-exists@1.0.40000404 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);
});
});
});
0