Skip to content

Commit 4dd1bae

Browse files
authored
Improved RPM getter and overall service performance (#19)
1 parent 9a02b6c commit 4dd1bae

File tree

6 files changed

+22
-25
lines changed

6 files changed

+22
-25
lines changed

.env-cmdrc-template

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
"SWITCHER_API_LOGGER": false,
2121
"SWITCHER_API_ENABLE": false,
22+
"SWITCHER_API_THROTTLE": 5000,
2223
"SWITCHER_API_URL": "http://localhost:3000",
2324
"SWITCHER_API_KEY": "MOCK_SWITCHER_API_KEY",
2425
"SWITCHER_API_DOMAIN": "MOCK_SWITCHER_API_DOMAIN",

docker-compose.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@ services:
3535
- SWITCHER_API_URL=${SWITCHER_API_URL}
3636
- SWITCHER_API_KEY=${SWITCHER_API_KEY}
3737
- SWITCHER_API_DOMAIN=${SWITCHER_API_DOMAIN}
38-
- SWITCHER_API_ENVIRONMENT=${SWITCHER_API_ENVIRONMENT}
38+
- SWITCHER_API_ENVIRONMENT=${SWITCHER_API_ENVIRONMENT}
39+
- SWITCHER_API_THROTTLE=${SWITCHER_API_THROTTLE}

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,17 @@
4444
"express-rate-limit": "^7.2.0",
4545
"express-validator": "^7.0.1",
4646
"graphql": "^16.8.1",
47-
"graphql-http": "^1.22.0",
47+
"graphql-http": "^1.22.1",
4848
"graphql-tag": "^2.12.6",
4949
"helmet": "^7.1.0",
5050
"jsonwebtoken": "^9.0.2",
5151
"moment": "^2.30.1",
5252
"mongodb": "^6.5.0",
53-
"mongoose": "^8.3.0",
54-
"pino": "^8.19.0",
53+
"mongoose": "^8.3.2",
54+
"pino": "^8.20.0",
5555
"pino-pretty": "^11.0.0",
5656
"swagger-ui-express": "^5.0.0",
57-
"switcher-client": "^3.2.1",
57+
"switcher-client": "^4.0.2",
5858
"validator": "^13.11.0"
5959
},
6060
"devDependencies": {

src/client/relay/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { StrategiesToRelayDataType, RelayMethods } from '../../models/config.js'
44
import { checkHttpsAgent } from '../../external/switcher-api-facade.js';
55

66
const agent = async (url) => {
7-
const rejectUnauthorized = !(await checkHttpsAgent(url));
8-
return new https.Agent({ rejectUnauthorized });
7+
const response = await checkHttpsAgent(url);
8+
return new https.Agent({ rejectUnauthorized: !(response?.result) });
99
};
1010

1111
export async function resolveNotification(relay, entry, environment) {

src/external/switcher-api-facade.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const environment = process.env.SWITCHER_API_ENVIRONMENT;
77
const domainName = process.env.SWITCHER_API_DOMAIN;
88
const url = process.env.SWITCHER_API_URL;
99
const logger = process.env.SWITCHER_API_LOGGER == 'true';
10+
const throttle = process.env.SWITCHER_API_THROTTLE;
1011
const certPath = process.env.SSL_CERT;
1112
const component = 'switcherapi';
1213

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

2021
async function checkFeature(feature, params) {
2122
const switcher = Switcher.factory();
22-
return switcher.isItOn(feature, params, true);
23+
24+
if (throttle) {
25+
switcher.throttle(throttle);
26+
}
27+
28+
return switcher.detail().isItOn(feature, params);
2329
}
2430

2531
export async function getRateLimit(key, component) {
2632
if (process.env.SWITCHER_API_ENABLE === 'true' && key !== process.env.SWITCHER_API_KEY) {
2733
const domain = await getDomainById(component.domain);
28-
const result = await checkFeature(SwitcherKeys.RATE_LIMIT, [
34+
const response = await checkFeature(SwitcherKeys.RATE_LIMIT, [
2935
checkValue(String(domain.owner))
3036
]);
3137

32-
if (result) {
33-
const log = Switcher.getLogger(SwitcherKeys.RATE_LIMIT)
34-
.find(log => log.input[0][1] === String(domain.owner));
35-
36-
return JSON.parse(log.response.message).rate_limit;
38+
if (response.result) {
39+
return response.metadata.rate_limit;
3740
}
3841
}
3942

tests/unit-test/switcher-api-facade.test.js

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@ import {
44
getRateLimit
55
} from '../../src/external/switcher-api-facade';
66
import {
7-
setupDatabase,
8-
domainDocument,
7+
setupDatabase,
98
component1,
109
component1Key
1110
} from '../fixtures/db_api';
12-
import { Switcher, checkValue } from 'switcher-client';
13-
import ExecutionLogger from 'switcher-client/src/lib/utils/executionLogger';
11+
import { Switcher } from 'switcher-client';
1412

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

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

3028
test('UNIT_API_FACADE - Should read rate limit - 100 Request Per Minute', async () => {
3129
const call = async () => {
32-
Switcher.assume(SwitcherKeys.RATE_LIMIT).true();
33-
ExecutionLogger.add(
34-
{ message: JSON.stringify({ rate_limit: 100 }) },
35-
SwitcherKeys.RATE_LIMIT,
36-
[checkValue(domainDocument.owner.toString())]
37-
);
38-
30+
Switcher.assume(SwitcherKeys.RATE_LIMIT).true().withMetadata({ rate_limit: 100 });
3931
return getRateLimit(component1Key, component1);
4032
};
4133

0 commit comments

Comments
 (0)