diff --git a/src/lib/features/feature-toggle/configuration-revision-service.ts b/src/lib/features/feature-toggle/configuration-revision-service.ts index 7deaf447af..ca9bf072c1 100644 --- a/src/lib/features/feature-toggle/configuration-revision-service.ts +++ b/src/lib/features/feature-toggle/configuration-revision-service.ts @@ -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, { getLogger, @@ -32,6 +34,23 @@ export default class ConfigurationRevisionService extends EventEmitter { this.revisionId = 0; } + static getInstance( + { eventStore }: Pick, + { + getLogger, + flagResolver, + }: Pick, + ) { + if (!ConfigurationRevisionService.instance) { + ConfigurationRevisionService.instance = + new ConfigurationRevisionService( + { eventStore }, + { getLogger, flagResolver }, + ); + } + return ConfigurationRevisionService.instance; + } + async getMaxRevisionId(): Promise { if (this.revisionId > 0) { return this.revisionId; diff --git a/src/lib/proxy/createProxyService.ts b/src/lib/proxy/createProxyService.ts index e8c1b25388..cea79f73a2 100644 --- a/src/lib/proxy/createProxyService.ts +++ b/src/lib/proxy/createProxyService.ts @@ -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, diff --git a/src/lib/services/index.ts b/src/lib/services/index.ts index e9d51c0f02..2d840fde5c 100644 --- a/src/lib/services/index.ts +++ b/src/lib/services/index.ts @@ -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);