try to avoid `insert` races in `FederatedInstanceService`

Despite the cache, different processes can race to insert a row for
the same remote host.

This is particularly apparent in our unit tests, with
`test/unit/activitypub.ts` randomly failing.

This is a (somewhat clumsy) attempt at working around that race: trap
the "duplicate key value" error, and fetch the record.

Tests pass, with or without values in the cache.
This commit is contained in:
dakkar 2024-10-12 10:43:28 +01:00
parent f3e6b43ea4
commit 06bd29f209
1 changed files with 20 additions and 5 deletions

View File

@ -12,6 +12,8 @@ import { IdService } from '@/core/IdService.js';
import { DI } from '@/di-symbols.js'; import { DI } from '@/di-symbols.js';
import { UtilityService } from '@/core/UtilityService.js'; import { UtilityService } from '@/core/UtilityService.js';
import { bindThis } from '@/decorators.js'; import { bindThis } from '@/decorators.js';
import { QueryFailedError } from 'typeorm';
import { isDuplicateKeyValueError } from '@/misc/is-duplicate-key-value-error.js';
@Injectable() @Injectable()
export class FederatedInstanceService implements OnApplicationShutdown { export class FederatedInstanceService implements OnApplicationShutdown {
@ -56,11 +58,24 @@ export class FederatedInstanceService implements OnApplicationShutdown {
const index = await this.instancesRepository.findOneBy({ host }); const index = await this.instancesRepository.findOneBy({ host });
if (index == null) { if (index == null) {
const i = await this.instancesRepository.insertOne({ let i;
id: this.idService.gen(), try {
host, i = await this.instancesRepository.insertOne({
firstRetrievedAt: new Date(), id: this.idService.gen(),
}); host,
firstRetrievedAt: new Date(),
});
} catch (e: unknown) {
if (e instanceof QueryFailedError) {
if (isDuplicateKeyValueError(e)) {
i = await this.instancesRepository.findOneBy({ host });
}
}
if (i == null) {
throw e;
}
}
this.federatedInstanceCache.set(host, i); this.federatedInstanceCache.set(host, i);
return i; return i;