Skip to content

Instantly share code, notes, and snippets.

@numtel
Created May 14, 2017 07:59
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save numtel/7b33d51041d0a12883aedad98bb940b8 to your computer and use it in GitHub Desktop.
Save numtel/7b33d51041d0a12883aedad98bb940b8 to your computer and use it in GitHub Desktop.
Find your closest AWS region
'use strict';
(function() {
// inspired by cloudping.info
var regions = {
'us-east-1': 'US-East (Virginia)',
'us-east-2': 'US East (Ohio)',
'us-west-1': 'US-West (California)',
'us-west-2': 'US-West (Oregon)',
'ca-central-1': 'Canada (Central)',
'eu-west-1': 'Europe (Ireland)',
'eu-west-2': 'Europe (London)',
'eu-central-1': 'Europe (Frankfurt)',
'ap-south-1': 'Asia Pacific (Mumbai)',
'ap-southeast-1': 'Asia Pacific (Singapore)',
'ap-southeast-2': 'Asia Pacific (Sydney)',
'ap-northeast-1': 'Asia Pacific (Tokyo)',
'ap-northeast-2': 'Asia Pacific (Seoul)',
'sa-east-1': 'South America (São Paulo)',
'cn-north-1': 'China (Beijing)',
};
var specialUrls = {
'cn-north-1': 'http://dynamodb.cn-north-1.amazonaws.com.cn/'
};
function testUrl(region) {
return (region in specialUrls ?
specialUrls[region] : 'http://dynamodb.' + region + '.amazonaws.com/') +
'does-not-exist?cache-break=' +
Math.floor(Math.random() * Math.pow(2, 52)).toString(36);
}
function callbackOnError(url, cb) {
var img = new Image;
img.onerror = cb;
img.src = url;
}
function timestamp() {
return (new Date()).getTime();
}
function pingRegion(region, cb) {
var url = testUrl(region);
// First failed request to prime connection
callbackOnError(url, function() {
// Second for measuring duration
var start = timestamp();
callbackOnError(url, function() {
cb(timestamp() - start);
});
});
}
function pingAllRegions(doneCallback, progressCallback, _curr) {
_curr = _curr || [];
var regionNames = Object.keys(regions);
if(_curr.length === regionNames.length)
return doneCallback(_curr.sort(function(a, b) {
return a.ping > b.ping ? 1 : -1;
}));
var nextRegion = regionNames[_curr.length];
pingRegion(nextRegion, function(time) {
_curr.push({
name: nextRegion,
title: regions[nextRegion],
ping: time
});
if(typeof progressCallback === 'function')
progressCallback(_curr.length / regionNames.length);
pingAllRegions(doneCallback, progressCallback, _curr);
});
}
this.AWSPingTest = pingAllRegions;
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment