feat(auth): various Strapi fixes

Because

- We want to spread out any flood of cache resets (unlikely since servers take varying amounts of time to spin up, but possible)
- We want to wait on a pending cachable request between reset periods, simply due to the sheer number of requests that could go through in the interval during a cache reset period.
- We want to enable Strapi in "shadow mode" to log potential mismatches

This commit

- Adds a random spread to cache reset interval
- Waits on pending cachable requests
- Enables Strapi in key_server if config is present
This commit is contained in:
julianpoyourow 2024-11-19 16:38:49 +00:00
Родитель 387cf9e11c
Коммит a76ced3e04
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: EA0570ABC73D47D3
2 изменённых файлов: 33 добавлений и 13 удалений

Просмотреть файл

@ -50,7 +50,7 @@ export class StrapiClient {
event: 'response',
listener: (response: EventResponse) => void
) => EventEmitter;
private graphqlMemCache: Record<string, unknown> = {};
private graphqlMemCache: Record<string, Promise<unknown> | undefined> = {};
constructor(
private strapiClientConfig: StrapiClientConfig,
@ -103,21 +103,35 @@ export class StrapiClient {
};
if (this.graphqlMemCache[cacheKey]) {
this.emitter.emit('response', {
...emitterResponse,
requestEndTime: emitterResponse.requestStartTime,
elapsed: 0,
cache: true,
});
return this.graphqlMemCache[cacheKey] as Result;
let response: Result | undefined;
try {
response = (await this.graphqlMemCache[cacheKey]) as Result;
} catch (e) {
// Do nothing, fall through to do the actual query since the cached instance failed
}
if (response) {
this.emitter.emit('response', {
...emitterResponse,
requestEndTime: emitterResponse.requestStartTime,
elapsed: 0,
cache: true,
});
return response;
}
}
try {
const response = await this.client.request<Result, any>({
const response = this.client.request<Result, any>({
document: query,
variables,
});
this.graphqlMemCache[cacheKey] = response;
await response;
const requestEndTime = Date.now();
this.emitter.emit('response', {
...emitterResponse,
@ -125,8 +139,6 @@ export class StrapiClient {
requestEndTime,
});
this.graphqlMemCache[cacheKey] = response;
return response;
} catch (e) {
const requestEndTime = Date.now();
@ -151,8 +163,10 @@ export class StrapiClient {
const cacheTTL =
(this.strapiClientConfig.memCacheTTL || DEFAULT_MEM_CACHE_TTL) * 1000;
const randomSpread = Math.floor(Math.random() * 100);
setInterval(() => {
this.graphqlMemCache = {};
}, cacheTTL);
}, cacheTTL + randomSpread);
}
}

Просмотреть файл

@ -138,7 +138,13 @@ async function run(config) {
);
Container.set(PromotionCodeManager, promotionCodeManager);
if (config.cms.enabled) {
if (
config.cms.enabled ||
(config.cms.strapiClient &&
config.cms.strapiClient.graphqlApiUri &&
config.cms.strapiClient.apiKey &&
config.cms.strapiClient.firestoreCacheCollectionName)
) {
const strapiClientConfig = config.cms.strapiClient;
const { graphqlApiUri, apiKey, firestoreCacheCollectionName } =
strapiClientConfig;