Skip to content

Commit

Permalink
Merge pull request #93 from fortinet/patch_remove_caching_of_load_set…
Browse files Browse the repository at this point in the history
…tings

remove caching of loadSettings
  • Loading branch information
JaydenLiang authored Oct 25, 2021
2 parents 2cf5267 + 191d62d commit b3ca979
Show file tree
Hide file tree
Showing 3 changed files with 625 additions and 6,801 deletions.
58 changes: 18 additions & 40 deletions fortigate-autoscale/azure/azure-platform-adaptee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,6 @@ import { SecretClient } from '@azure/keyvault-secrets';
import * as msRestNodeAuth from '@azure/ms-rest-nodeauth';
import { BlobServiceClient, StorageSharedKeyCredential } from '@azure/storage-blob';
import * as DBDef from '@fortinet/autoscale-core/db-definitions';
import {
Blob,
jsonParseReviver,
jsonStringifyReplacer,
PlatformAdaptee,
SettingItem,
Settings
} from '..';
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';
import fs from 'fs';
import * as HttpStatusCodes from 'http-status-codes';
Expand All @@ -38,6 +30,14 @@ import {
CosmosDBQueryWhereClause,
CosmosDbTableMetaData
} from '.';
import {
Blob,
jsonParseReviver,
jsonStringifyReplacer,
PlatformAdaptee,
SettingItem,
Settings
} from '..';

export enum requiredEnvVars {
AUTOSCALE_DB_ACCOUNT = 'AUTOSCALE_DB_ACCOUNT',
Expand Down Expand Up @@ -107,8 +107,7 @@ export enum ApiCacheOption {
const TTLS = {
listInstances: 600,
describeInstance: 600,
listNetworkInterfaces: 600,
loadSettings: 60
listNetworkInterfaces: 600
};

export class AzurePlatformAdaptee implements PlatformAdaptee {
Expand Down Expand Up @@ -176,32 +175,17 @@ export class AzurePlatformAdaptee implements PlatformAdaptee {
);
}

async reloadSettings(invalidateCache: boolean): Promise<ApiCache<Settings>> {
const req: ApiCacheRequest = {
api: 'loadSettings',
parameters: [],
ttl: TTLS.loadSettings // expected time to live
};

const requestProcessor = async (): Promise<AzureSettingsDbItem[]> => {
const table = new AzureSettings();
const queryResult: CosmosDBQueryResult<AzureSettingsDbItem> = await this.listItemFromDb<
AzureSettingsDbItem
>(table);
return queryResult.result || [];
};
async reloadSettings(invalidateCache: boolean): Promise<Settings> {
const table = new AzureSettings();
const queryResult: CosmosDBQueryResult<AzureSettingsDbItem> = await this.listItemFromDb<
AzureSettingsDbItem
>(table);
const res = queryResult.result || [];
if (invalidateCache) {
this.settings = null;
}
const res = await this.requestWithCaching<AzureSettingsDbItem[]>(
req,
invalidateCache ? ApiCacheOption.ReadCacheAndDelete : ApiCacheOption.ReadCacheFirst,
requestProcessor
);
const records: Map<string, AzureSettingsDbItem> = new Map();
if (res && res.result) {
res.result.forEach(rec => records.set(rec.settingKey, rec));
}
res.forEach(rec => records.set(rec.settingKey, rec));
const settings: Settings = new Map<string, SettingItem>();
Object.values(AzureFortiGateAutoscaleSetting).forEach(value => {
if (records.has(value)) {
Expand All @@ -216,21 +200,15 @@ export class AzurePlatformAdaptee implements PlatformAdaptee {
settings.set(value, settingItem);
}
});
const cache: ApiCache<Settings> = {
result: settings,
hitCache: res.hitCache,
cacheTime: res.cacheTime,
ttl: res.ttl
};
return cache;
return settings;
}

async loadSettings(): Promise<Settings> {
if (this.settings) {
return this.settings;
}
const data = await this.reloadSettings(false);
this.settings = data.result;
this.settings = data;
return this.settings;
}

Expand Down
Loading

0 comments on commit b3ca979

Please sign in to comment.