Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defaults: Generate environment fallbacks #682

Merged
merged 1 commit into from
Sep 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions common/lib/util/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ Defaults.getHttpScheme = function(options) {
return options.tls ? 'https://' : 'http://';
};

// construct environment fallback hosts as per RSC15i
Defaults.environmentFallbackHosts = function(environment) {
return [
environment + '-a-fallback.ably-realtime.com',
environment + '-b-fallback.ably-realtime.com',
environment + '-c-fallback.ably-realtime.com',
environment + '-d-fallback.ably-realtime.com',
environment + '-e-fallback.ably-realtime.com',
];
};

Defaults.getFallbackHosts = function(options) {
var fallbackHosts = options.fallbackHosts,
httpMaxRetryCount = typeof(options.httpMaxRetryCount) !== 'undefined' ? options.httpMaxRetryCount : Defaults.httpMaxRetryCount;
Expand Down Expand Up @@ -116,16 +127,22 @@ Defaults.normaliseOptions = function(options) {
if(!('queueMessages' in options))
options.queueMessages = true;

var production = false;
if(options.restHost) {
options.realtimeHost = options.realtimeHost || options.restHost;
lmars marked this conversation as resolved.
Show resolved Hide resolved
options.fallbackHosts = options.fallbackHostsUseDefault && !options.port && !options.tlsPort ? Defaults.FALLBACK_HOSTS : options.fallbackHosts;
lmars marked this conversation as resolved.
Show resolved Hide resolved
} else {
var environment = (options.environment && String(options.environment).toLowerCase()) || Defaults.ENVIRONMENT;
production = !environment || (environment === 'production');
var production = !environment || (environment === 'production');
options.restHost = production ? Defaults.REST_HOST : environment + '-' + Defaults.REST_HOST;
lmars marked this conversation as resolved.
Show resolved Hide resolved
options.realtimeHost = production ? Defaults.REALTIME_HOST : environment + '-' + Defaults.REALTIME_HOST;
lmars marked this conversation as resolved.
Show resolved Hide resolved
if(!options.fallbackHosts && !options.port && !options.tlsPort) {
sacOO7 marked this conversation as resolved.
Show resolved Hide resolved
if(production || options.fallbackHostsUseDefault) {
lmars marked this conversation as resolved.
Show resolved Hide resolved
options.fallbackHosts = Defaults.FALLBACK_HOSTS;
} else {
options.fallbackHosts = Defaults.environmentFallbackHosts(environment);
}
}
}
options.fallbackHosts = (production || options.fallbackHostsUseDefault) ? Defaults.FALLBACK_HOSTS : options.fallbackHosts;
Utils.arrForEach((options.fallbackHosts || []).concat(options.restHost, options.realtimeHost), checkHost);

options.port = options.port || Defaults.PORT;
Expand Down
14 changes: 8 additions & 6 deletions spec/rest/defaults.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,18 @@ define(['ably', 'shared_helper'], function(Ably, helper) {

/* init with given environment */
exports.defaults_given_environment = function(test) {
test.expect(10);
test.expect(11);
var normalisedOptions = Defaults.normaliseOptions({environment: 'sandbox'});

test.equal(normalisedOptions.restHost, 'sandbox-rest.ably.io');
test.equal(normalisedOptions.realtimeHost, 'sandbox-realtime.ably.io');
test.equal(normalisedOptions.port, 80);
test.equal(normalisedOptions.tlsPort, 443);
test.equal(normalisedOptions.fallbackHosts, undefined);
test.deepEqual(normalisedOptions.fallbackHosts.sort(), Defaults.environmentFallbackHosts('sandbox').sort());
test.equal(normalisedOptions.tls, true);

test.deepEqual(Defaults.getHosts(normalisedOptions), [normalisedOptions.restHost]);
test.deepEqual(Defaults.getHosts(normalisedOptions).length, 4);
test.deepEqual(Defaults.getHosts(normalisedOptions)[0], normalisedOptions.restHost);
test.deepEqual(Defaults.getHost(normalisedOptions, 'sandbox-rest.ably.io', false), 'sandbox-rest.ably.io');
test.deepEqual(Defaults.getHost(normalisedOptions, 'sandbox-rest.ably.io', true), 'sandbox-realtime.ably.io');

Expand Down Expand Up @@ -160,18 +161,19 @@ define(['ably', 'shared_helper'], function(Ably, helper) {

/* init with no endpoint-related options and given default environment */
exports.defaults_set_default_environment = function(test) {
test.expect(10);
test.expect(11);
Defaults.ENVIRONMENT = 'sandbox';
var normalisedOptions = Defaults.normaliseOptions({});

test.equal(normalisedOptions.restHost, 'sandbox-rest.ably.io');
test.equal(normalisedOptions.realtimeHost, 'sandbox-realtime.ably.io');
test.equal(normalisedOptions.port, 80);
test.equal(normalisedOptions.tlsPort, 443);
test.equal(normalisedOptions.fallbackHosts, undefined);
test.deepEqual(normalisedOptions.fallbackHosts.sort(), Defaults.environmentFallbackHosts('sandbox').sort());
test.equal(normalisedOptions.tls, true);

test.deepEqual(Defaults.getHosts(normalisedOptions), [normalisedOptions.restHost]);
test.deepEqual(Defaults.getHosts(normalisedOptions).length, 4);
test.deepEqual(Defaults.getHosts(normalisedOptions)[0], normalisedOptions.restHost);
test.deepEqual(Defaults.getHost(normalisedOptions, 'sandbox-rest.ably.io', false), 'sandbox-rest.ably.io');
test.deepEqual(Defaults.getHost(normalisedOptions, 'sandbox-rest.ably.io', true), 'sandbox-realtime.ably.io');

Expand Down