Skip to content
This repository has been archived by the owner on Jul 10, 2023. It is now read-only.

Commit

Permalink
Added adaptive timeout margin support based on current region
Browse files Browse the repository at this point in the history
  • Loading branch information
Serkan ÖZAL committed Sep 22, 2018
1 parent a917bdd commit ae52abe
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
25 changes: 24 additions & 1 deletion src/Constants.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
import * as url from 'url';

export function getTimeoutMargin(region: string) {
if (region) {
if (region === 'us-west-2') { // our region
return 200;
} else if (region.startsWith('us-west-')) { // Any region at west of USA
return 400;
} else if (region.startsWith('us-')) { // Any region at USA
return 600;
} else if (region.startsWith('ca-')) { // Any region at Canada
return 600;
} else if (region.startsWith('sa-')) { // Any region at South America
return 800;
} else if (region.startsWith('cn-')) { // Any region at China
return 1000;
} else if (region.startsWith('eu-')) { // Any region at Europe
return 1000;
} else if (region.startsWith('ap-')) { // Any region at Asia Pacific
return 1000;
}
}
return 1000;
}

export const DATA_FORMAT_VERSION: string = '1.2';
export const TIMEOUT_MARGIN: number = 200;
export const TIMEOUT_MARGIN: number = getTimeoutMargin(process.env.AWS_REGION);

export const HOOKS = [
'before-invocation',
Expand Down
7 changes: 5 additions & 2 deletions src/plugins/config/ThundraConfig.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
import TraceConfig from './TraceConfig';
import MetricConfig from './MetricConfig';
import InvocationConfig from './InvocationConfig';
import {TIMEOUT_MARGIN} from '../../Constants';
const koalas = require('koalas');

class ThundraConfig {

trustAllCert: boolean;
apiKey: string;
disableThundra: boolean;
traceConfig: TraceConfig;
metricConfig: MetricConfig;
invocationConfig: InvocationConfig;
timeoutMargin: number = 200;
timeoutMargin: number;
plugins: any [];

constructor(options: any) {
options = options ? options : {};
this.plugins = koalas(options.plugins, []);
this.apiKey = koalas(process.env.thundra_apiKey, options.apiKey, '');
this.disableThundra = koalas(process.env.thundra_disable, options.disableThundra, false);
this.timeoutMargin = koalas(process.env.thundra_lambda_timeout_margin, options.timeoutMargin, 200);
this.timeoutMargin = koalas(process.env.thundra_lambda_timeout_margin, options.timeoutMargin, TIMEOUT_MARGIN);
this.traceConfig = new TraceConfig(options.traceConfig);
this.metricConfig = new MetricConfig(options.metricConfig);
this.invocationConfig = new InvocationConfig(options.invocationConfig);
this.trustAllCert = koalas(process.env.thundra_lambda_publish_rest_trustAllCertificates, options.trustAllCert, false);
}

}

export default ThundraConfig;
30 changes: 29 additions & 1 deletion test/constants.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {HOOKS, URL, PROC_STAT_PATH, PROC_IO_PATH, DATA_FORMAT_VERSION} from '../dist/Constants';
import {HOOKS, URL, PROC_STAT_PATH, PROC_IO_PATH, DATA_FORMAT_VERSION, getTimeoutMargin} from '../dist/Constants';

test('DATA_FORMAT_VERSION did not change', () => {
expect(DATA_FORMAT_VERSION).toEqual('1.2');
Expand All @@ -21,4 +21,32 @@ test('PROC_IO_PATH did not change', () => {
expect(PROC_IO_PATH).toEqual('/proc/self/io');
});

test('Timeout margin should be decided based on region', () => {
expect(getTimeoutMargin('us-west-2')).toEqual(200);
expect(getTimeoutMargin('us-west-1')).toEqual(400);
expect(getTimeoutMargin('us-east-2')).toEqual(600);
expect(getTimeoutMargin('us-east-1')).toEqual(600);

expect(getTimeoutMargin('ap-south-1')).toEqual(1000);
expect(getTimeoutMargin('ap-northeast-2')).toEqual(1000);
expect(getTimeoutMargin('ap-southeast-1')).toEqual(1000);
expect(getTimeoutMargin('ap-southeast-2')).toEqual(1000);
expect(getTimeoutMargin('ap-northeast-1')).toEqual(1000);

expect(getTimeoutMargin('ca-central-1')).toEqual(600);
expect(getTimeoutMargin('cn-north-1')).toEqual(1000);

expect(getTimeoutMargin('eu-central-1')).toEqual(1000);
expect(getTimeoutMargin('eu-west-1')).toEqual(1000);
expect(getTimeoutMargin('eu-west-2')).toEqual(1000);
expect(getTimeoutMargin('eu-west-3')).toEqual(1000);

expect(getTimeoutMargin('sa-east-1')).toEqual(800);

expect(getTimeoutMargin('tr-east-1')).toEqual(1000); // Unknown

expect(getTimeoutMargin()).toEqual(1000); // Invalid
});



0 comments on commit ae52abe

Please sign in to comment.