Skip to content

Commit b75429d

Browse files
committed
Add singleton principle to RestService
1 parent 2d1a4a5 commit b75429d

File tree

4 files changed

+18
-6
lines changed

4 files changed

+18
-6
lines changed

schemaregistry/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"scripts": {
5858
"lint": "make lint",
5959
"test": "make test",
60+
"integtest": "make integtest",
6061
"build": "rm -rf ./dist && tsc -p tsconfig-build.json"
6162
},
6263
"keywords": [

schemaregistry/rest-service.ts

+15-4
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,15 @@ export interface ClientConfig {
5151
const toBase64 = (str: string): string => Buffer.from(str).toString('base64');
5252

5353
export class RestService {
54+
private static instance: RestService;
5455
private client: AxiosInstance;
5556
private baseURLs: string[];
5657
private oauthClient?: OAuthClient;
5758
private oauthBearer: boolean = false;
5859

5960
constructor(baseURLs: string[], isForward?: boolean, axiosDefaults?: CreateAxiosDefaults,
60-
basicAuthCredentials?: BasicAuthCredentials, bearerAuthCredentials?: BearerAuthCredentials,
61-
maxRetries?: number, retriesWaitMs?: number, retriesMaxWaitMs?: number) {
61+
basicAuthCredentials?: BasicAuthCredentials, bearerAuthCredentials?: BearerAuthCredentials,
62+
maxRetries?: number, retriesWaitMs?: number, retriesMaxWaitMs?: number) {
6263
this.client = axios.create(axiosDefaults);
6364
axiosRetry(this.client, {
6465
retries: maxRetries ?? 2,
@@ -80,6 +81,16 @@ export class RestService {
8081
this.handleBearerAuth(maxRetries ?? 2, retriesWaitMs ?? 1000, retriesMaxWaitMs ?? 20000, bearerAuthCredentials);
8182
}
8283

84+
static getInstance(baseURLs: string[], isForward?: boolean, axiosDefaults?: CreateAxiosDefaults,
85+
basicAuthCredentials?: BasicAuthCredentials, bearerAuthCredentials?: BearerAuthCredentials,
86+
maxRetries?: number, retriesWaitMs?: number, retriesMaxWaitMs?: number): RestService {
87+
if (!this.instance) {
88+
this.instance = new RestService(baseURLs, isForward, axiosDefaults, basicAuthCredentials, bearerAuthCredentials,
89+
maxRetries, retriesWaitMs, retriesMaxWaitMs);
90+
}
91+
return this.instance;
92+
}
93+
8394
handleBasicAuth(basicAuthCredentials?: BasicAuthCredentials): void {
8495
if (basicAuthCredentials) {
8596
switch (basicAuthCredentials.credentialsSource) {
@@ -111,7 +122,7 @@ export class RestService {
111122
}
112123
}
113124

114-
handleBearerAuth(maxRetries: number,
125+
handleBearerAuth(maxRetries: number,
115126
retriesWaitMs: number, retriesMaxWaitMs: number, bearerAuthCredentials?: BearerAuthCredentials): void {
116127
if (bearerAuthCredentials) {
117128
delete this.client.defaults.auth;
@@ -150,7 +161,7 @@ export class RestService {
150161
}
151162
const issuerEndPointUrl = new URL(bearerAuthCredentials.issuerEndpointUrl!);
152163
this.oauthClient = new OAuthClient(bearerAuthCredentials.clientId!, bearerAuthCredentials.clientSecret!,
153-
issuerEndPointUrl.origin, issuerEndPointUrl.pathname, bearerAuthCredentials.scope!,
164+
issuerEndPointUrl.origin, issuerEndPointUrl.pathname, bearerAuthCredentials.scope!,
154165
maxRetries, retriesWaitMs, retriesMaxWaitMs);
155166
break;
156167
default:

schemaregistry/rules/encryption/dekregistry/dekregistry-client.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class DekRegistryClient implements DekClient {
7070
};
7171

7272

73-
this.restService = new RestService(config.baseURLs, config.isForward, config.createAxiosDefaults,
73+
this.restService = RestService.getInstance(config.baseURLs, config.isForward, config.createAxiosDefaults,
7474
config.basicAuthCredentials, config.bearerAuthCredentials,
7575
config.maxRetries, config.retriesWaitMs, config.retriesMaxWaitMs);
7676
this.kekCache = new LRUCache<string, Kek>(cacheOptions);

schemaregistry/schemaregistry-client.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ export class SchemaRegistryClient implements Client {
201201
...(config.cacheLatestTtlSecs !== undefined && { ttl: config.cacheLatestTtlSecs * 1000 })
202202
};
203203

204-
this.restService = new RestService(config.baseURLs, config.isForward, config.createAxiosDefaults,
204+
this.restService = RestService.getInstance(config.baseURLs, config.isForward, config.createAxiosDefaults,
205205
config.basicAuthCredentials, config.bearerAuthCredentials,
206206
config.maxRetries, config.retriesWaitMs, config.retriesMaxWaitMs);
207207

0 commit comments

Comments
 (0)