From 2924fa7abea5bc90284bf8c3aa421f8402a30fe9 Mon Sep 17 00:00:00 2001 From: claimundefine Date: Wed, 4 Dec 2024 13:50:57 -0500 Subject: [PATCH] Add singleton principle to RestService --- schemaregistry/package.json | 1 + schemaregistry/rest-service.ts | 19 +++++++++++++++---- .../dekregistry/dekregistry-client.ts | 2 +- schemaregistry/schemaregistry-client.ts | 2 +- 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/schemaregistry/package.json b/schemaregistry/package.json index ca98bc95..cd310f20 100644 --- a/schemaregistry/package.json +++ b/schemaregistry/package.json @@ -59,6 +59,7 @@ "scripts": { "lint": "make lint", "test": "make test", + "integtest": "make integtest", "build": "rm -rf ./dist && tsc -p tsconfig-build.json" }, "keywords": [ diff --git a/schemaregistry/rest-service.ts b/schemaregistry/rest-service.ts index 7e5876c0..45a263ac 100644 --- a/schemaregistry/rest-service.ts +++ b/schemaregistry/rest-service.ts @@ -51,14 +51,15 @@ export interface ClientConfig { const toBase64 = (str: string): string => Buffer.from(str).toString('base64'); export class RestService { + private static instance: RestService; private client: AxiosInstance; private baseURLs: string[]; private oauthClient?: OAuthClient; private oauthBearer: boolean = false; constructor(baseURLs: string[], isForward?: boolean, axiosDefaults?: CreateAxiosDefaults, - basicAuthCredentials?: BasicAuthCredentials, bearerAuthCredentials?: BearerAuthCredentials, - maxRetries?: number, retriesWaitMs?: number, retriesMaxWaitMs?: number) { + basicAuthCredentials?: BasicAuthCredentials, bearerAuthCredentials?: BearerAuthCredentials, + maxRetries?: number, retriesWaitMs?: number, retriesMaxWaitMs?: number) { this.client = axios.create(axiosDefaults); axiosRetry(this.client, { retries: maxRetries ?? 2, @@ -80,6 +81,16 @@ export class RestService { this.handleBearerAuth(maxRetries ?? 2, retriesWaitMs ?? 1000, retriesMaxWaitMs ?? 20000, bearerAuthCredentials); } + static getInstance(baseURLs: string[], isForward?: boolean, axiosDefaults?: CreateAxiosDefaults, + basicAuthCredentials?: BasicAuthCredentials, bearerAuthCredentials?: BearerAuthCredentials, + maxRetries?: number, retriesWaitMs?: number, retriesMaxWaitMs?: number): RestService { + if (!this.instance) { + this.instance = new RestService(baseURLs, isForward, axiosDefaults, basicAuthCredentials, bearerAuthCredentials, + maxRetries, retriesWaitMs, retriesMaxWaitMs); + } + return this.instance; + } + handleBasicAuth(basicAuthCredentials?: BasicAuthCredentials): void { if (basicAuthCredentials) { switch (basicAuthCredentials.credentialsSource) { @@ -111,7 +122,7 @@ export class RestService { } } - handleBearerAuth(maxRetries: number, + handleBearerAuth(maxRetries: number, retriesWaitMs: number, retriesMaxWaitMs: number, bearerAuthCredentials?: BearerAuthCredentials): void { if (bearerAuthCredentials) { delete this.client.defaults.auth; @@ -150,7 +161,7 @@ export class RestService { } const issuerEndPointUrl = new URL(bearerAuthCredentials.issuerEndpointUrl!); this.oauthClient = new OAuthClient(bearerAuthCredentials.clientId!, bearerAuthCredentials.clientSecret!, - issuerEndPointUrl.origin, issuerEndPointUrl.pathname, bearerAuthCredentials.scope!, + issuerEndPointUrl.origin, issuerEndPointUrl.pathname, bearerAuthCredentials.scope!, maxRetries, retriesWaitMs, retriesMaxWaitMs); break; default: diff --git a/schemaregistry/rules/encryption/dekregistry/dekregistry-client.ts b/schemaregistry/rules/encryption/dekregistry/dekregistry-client.ts index 891bdbd7..217631c0 100644 --- a/schemaregistry/rules/encryption/dekregistry/dekregistry-client.ts +++ b/schemaregistry/rules/encryption/dekregistry/dekregistry-client.ts @@ -70,7 +70,7 @@ class DekRegistryClient implements DekClient { }; - this.restService = new RestService(config.baseURLs, config.isForward, config.createAxiosDefaults, + this.restService = RestService.getInstance(config.baseURLs, config.isForward, config.createAxiosDefaults, config.basicAuthCredentials, config.bearerAuthCredentials, config.maxRetries, config.retriesWaitMs, config.retriesMaxWaitMs); this.kekCache = new LRUCache(cacheOptions); diff --git a/schemaregistry/schemaregistry-client.ts b/schemaregistry/schemaregistry-client.ts index e323cbbc..fc7dfa90 100644 --- a/schemaregistry/schemaregistry-client.ts +++ b/schemaregistry/schemaregistry-client.ts @@ -201,7 +201,7 @@ export class SchemaRegistryClient implements Client { ...(config.cacheLatestTtlSecs !== undefined && { ttl: config.cacheLatestTtlSecs * 1000 }) }; - this.restService = new RestService(config.baseURLs, config.isForward, config.createAxiosDefaults, + this.restService = RestService.getInstance(config.baseURLs, config.isForward, config.createAxiosDefaults, config.basicAuthCredentials, config.bearerAuthCredentials, config.maxRetries, config.retriesWaitMs, config.retriesMaxWaitMs);