1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00

fix: Configuration revision service singleton (#6493)

This commit is contained in:
Mateusz Kwasniewski 2024-03-11 13:22:52 +01:00 committed by GitHub
parent c841e72244
commit eae373f386
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 41 additions and 46 deletions

View File

@ -1,4 +1,3 @@
import { EventEmitter } from 'stream';
import { Logger } from '../../logger';
import {
IEventStore,
@ -6,10 +5,13 @@ import {
IUnleashConfig,
IUnleashStores,
} from '../../types';
import EventEmitter from 'events';
export const UPDATE_REVISION = 'UPDATE_REVISION';
export default class ConfigurationRevisionService extends EventEmitter {
private static instance: ConfigurationRevisionService;
private logger: Logger;
private eventStore: IEventStore;
@ -18,7 +20,7 @@ export default class ConfigurationRevisionService extends EventEmitter {
private flagResolver: IFlagResolver;
constructor(
private constructor(
{ eventStore }: Pick<IUnleashStores, 'eventStore'>,
{
getLogger,
@ -32,6 +34,23 @@ export default class ConfigurationRevisionService extends EventEmitter {
this.revisionId = 0;
}
static getInstance(
{ eventStore }: Pick<IUnleashStores, 'eventStore'>,
{
getLogger,
flagResolver,
}: Pick<IUnleashConfig, 'getLogger' | 'flagResolver'>,
) {
if (!ConfigurationRevisionService.instance) {
ConfigurationRevisionService.instance =
new ConfigurationRevisionService(
{ eventStore },
{ getLogger, flagResolver },
);
}
return ConfigurationRevisionService.instance;
}
async getMaxRevisionId(): Promise<number> {
if (this.revisionId > 0) {
return this.revisionId;

View File

@ -10,12 +10,10 @@ import {
createFeatureToggleService,
} from '../features';
import ConfigurationRevisionService from '../features/feature-toggle/configuration-revision-service';
import EventStore from '../features/events/event-store';
import { GlobalFrontendApiCache } from './global-frontend-api-cache';
import ClientFeatureToggleReadModel from './client-feature-toggle-read-model';
import { FakeSegmentReadModel } from '../features/segment/fake-segment-read-model';
import FakeSettingStore from '../../test/fixtures/fake-setting-store';
import FakeEventStore from '../../test/fixtures/fake-event-store';
import FakeClientFeatureToggleReadModel from './fake-client-feature-toggle-read-model';
import { IUnleashConfig } from '../types';
import { Db } from '../db/db';
@ -25,6 +23,7 @@ export const createProxyService = (
config: IUnleashConfig,
// client metrics service needs to be shared because it uses in-memory cache
clientMetricsServiceV2: ClientMetricsServiceV2,
configurationRevisionService: ConfigurationRevisionService,
): ProxyService => {
const segmentReadModel = new SegmentReadModel(db);
const settingStore = new SettingStore(db, config.getLogger);
@ -36,15 +35,6 @@ export const createProxyService = (
);
// TODO: remove this dependency after we migrate frontend API
const featureToggleServiceV2 = createFeatureToggleService(db, config);
const eventStore = new EventStore(
db,
config.getLogger,
config.flagResolver,
);
const configurationRevisionService = new ConfigurationRevisionService(
{ eventStore },
config,
);
const clientFeatureToggleReadModel = new ClientFeatureToggleReadModel(
db,
config.eventBus,
@ -71,6 +61,7 @@ export const createProxyService = (
export const createFakeProxyService = (
config: IUnleashConfig,
clientMetricsServiceV2: ClientMetricsServiceV2,
configurationRevisionService: ConfigurationRevisionService,
): ProxyService => {
const segmentReadModel = new FakeSegmentReadModel();
const settingStore = new FakeSettingStore();
@ -82,11 +73,6 @@ export const createFakeProxyService = (
);
// TODO: remove this dependency after we migrate frontend API
const featureToggleServiceV2 = createFakeFeatureToggleService(config);
const eventStore = new FakeEventStore();
const configurationRevisionService = new ConfigurationRevisionService(
{ eventStore },
config,
);
const clientFeatureToggleReadModel = new FakeClientFeatureToggleReadModel();
const globalFrontendApiCache = new GlobalFrontendApiCache(
config,

View File

@ -111,9 +111,10 @@ import {
import { InactiveUsersService } from '../users/inactive/inactive-users-service';
import { SegmentReadModel } from '../features/segment/segment-read-model';
import { FakeSegmentReadModel } from '../features/segment/fake-segment-read-model';
import ClientFeatureToggleReadModel from '../proxy/client-feature-toggle-read-model';
import FakeClientFeatureToggleReadModel from '../proxy/fake-client-feature-toggle-read-model';
import { GlobalFrontendApiCache } from '../proxy/global-frontend-api-cache';
import {
createFakeProxyService,
createProxyService,
} from '../proxy/createProxyService';
export const createServices = (
stores: IUnleashStores,
@ -287,36 +288,25 @@ export const createServices = (
segmentReadModel,
);
const configurationRevisionService = new ConfigurationRevisionService(
stores,
config,
);
const configurationRevisionService =
ConfigurationRevisionService.getInstance(stores, config);
const clientFeatureToggleService = db
? createClientFeatureToggleService(db, config)
: createFakeClientFeatureToggleService(config);
const clientFeatureToggleReadModel = db
? new ClientFeatureToggleReadModel(db, config.eventBus)
: new FakeClientFeatureToggleReadModel();
const globalFrontendApiCache = new GlobalFrontendApiCache(
config,
segmentReadModel,
clientFeatureToggleReadModel,
configurationRevisionService,
);
const proxyService = new ProxyService(
config,
stores,
{
featureToggleServiceV2,
clientMetricsServiceV2,
settingService,
configurationRevisionService,
},
globalFrontendApiCache,
);
const proxyService = db
? createProxyService(
db,
config,
clientMetricsServiceV2,
configurationRevisionService,
)
: createFakeProxyService(
config,
clientMetricsServiceV2,
configurationRevisionService,
);
const edgeService = new EdgeService({ apiTokenService }, config);