Skip to content

Commit

Permalink
Improved RPM getter and overall service performance (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
petruki authored Apr 20, 2024
1 parent 9a02b6c commit 4dd1bae
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 25 deletions.
1 change: 1 addition & 0 deletions .env-cmdrc-template
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

"SWITCHER_API_LOGGER": false,
"SWITCHER_API_ENABLE": false,
"SWITCHER_API_THROTTLE": 5000,
"SWITCHER_API_URL": "http://localhost:3000",
"SWITCHER_API_KEY": "MOCK_SWITCHER_API_KEY",
"SWITCHER_API_DOMAIN": "MOCK_SWITCHER_API_DOMAIN",
Expand Down
3 changes: 2 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ services:
- SWITCHER_API_URL=${SWITCHER_API_URL}
- SWITCHER_API_KEY=${SWITCHER_API_KEY}
- SWITCHER_API_DOMAIN=${SWITCHER_API_DOMAIN}
- SWITCHER_API_ENVIRONMENT=${SWITCHER_API_ENVIRONMENT}
- SWITCHER_API_ENVIRONMENT=${SWITCHER_API_ENVIRONMENT}
- SWITCHER_API_THROTTLE=${SWITCHER_API_THROTTLE}
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@
"express-rate-limit": "^7.2.0",
"express-validator": "^7.0.1",
"graphql": "^16.8.1",
"graphql-http": "^1.22.0",
"graphql-http": "^1.22.1",
"graphql-tag": "^2.12.6",
"helmet": "^7.1.0",
"jsonwebtoken": "^9.0.2",
"moment": "^2.30.1",
"mongodb": "^6.5.0",
"mongoose": "^8.3.0",
"pino": "^8.19.0",
"mongoose": "^8.3.2",
"pino": "^8.20.0",
"pino-pretty": "^11.0.0",
"swagger-ui-express": "^5.0.0",
"switcher-client": "^3.2.1",
"switcher-client": "^4.0.2",
"validator": "^13.11.0"
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions src/client/relay/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { StrategiesToRelayDataType, RelayMethods } from '../../models/config.js'
import { checkHttpsAgent } from '../../external/switcher-api-facade.js';

const agent = async (url) => {
const rejectUnauthorized = !(await checkHttpsAgent(url));
return new https.Agent({ rejectUnauthorized });
const response = await checkHttpsAgent(url);
return new https.Agent({ rejectUnauthorized: !(response?.result) });
};

export async function resolveNotification(relay, entry, environment) {
Expand Down
17 changes: 10 additions & 7 deletions src/external/switcher-api-facade.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const environment = process.env.SWITCHER_API_ENVIRONMENT;
const domainName = process.env.SWITCHER_API_DOMAIN;
const url = process.env.SWITCHER_API_URL;
const logger = process.env.SWITCHER_API_LOGGER == 'true';
const throttle = process.env.SWITCHER_API_THROTTLE;
const certPath = process.env.SSL_CERT;
const component = 'switcherapi';

Expand All @@ -19,21 +20,23 @@ export const SwitcherKeys = Object.freeze({

async function checkFeature(feature, params) {
const switcher = Switcher.factory();
return switcher.isItOn(feature, params, true);

if (throttle) {
switcher.throttle(throttle);
}

return switcher.detail().isItOn(feature, params);
}

export async function getRateLimit(key, component) {
if (process.env.SWITCHER_API_ENABLE === 'true' && key !== process.env.SWITCHER_API_KEY) {
const domain = await getDomainById(component.domain);
const result = await checkFeature(SwitcherKeys.RATE_LIMIT, [
const response = await checkFeature(SwitcherKeys.RATE_LIMIT, [
checkValue(String(domain.owner))
]);

if (result) {
const log = Switcher.getLogger(SwitcherKeys.RATE_LIMIT)
.find(log => log.input[0][1] === String(domain.owner));

return JSON.parse(log.response.message).rate_limit;
if (response.result) {
return response.metadata.rate_limit;
}
}

Expand Down
14 changes: 3 additions & 11 deletions tests/unit-test/switcher-api-facade.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import {
getRateLimit
} from '../../src/external/switcher-api-facade';
import {
setupDatabase,
domainDocument,
setupDatabase,
component1,
component1Key
} from '../fixtures/db_api';
import { Switcher, checkValue } from 'switcher-client';
import ExecutionLogger from 'switcher-client/src/lib/utils/executionLogger';
import { Switcher } from 'switcher-client';

import '../../src/db/mongoose';

Expand All @@ -29,13 +27,7 @@ describe('Testing Switcher API Facade', () => {

test('UNIT_API_FACADE - Should read rate limit - 100 Request Per Minute', async () => {
const call = async () => {
Switcher.assume(SwitcherKeys.RATE_LIMIT).true();
ExecutionLogger.add(
{ message: JSON.stringify({ rate_limit: 100 }) },
SwitcherKeys.RATE_LIMIT,
[checkValue(domainDocument.owner.toString())]
);

Switcher.assume(SwitcherKeys.RATE_LIMIT).true().withMetadata({ rate_limit: 100 });
return getRateLimit(component1Key, component1);
};

Expand Down

0 comments on commit 4dd1bae

Please sign in to comment.