Skip to content

Commit

Permalink
v2.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
flesler committed Oct 31, 2016
1 parent f2fb2d1 commit 3240b8f
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 39 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
# Wi-Fine Version History

## v2.2.0 - 2016-10-30
### Removed
- When checking for updates, gh-releases is not longer used and updates are not automatic
### Added
- Implemented DNS-based pinging but not using it for now, due to a bug in Electron/Chromium
- About modal dialog reports which method is being used for pinging (HTTP/DNS)
### Changed
- Reverted update checker to only open releases page (gh-releases isn't working)
- Reverted updates module to only open releases page when there's a newer version
- Connectivity status is now based on the median of latest samples, will hopefully reduce back-and-forth changes
- HTTP-based pinging (in use) aborts requests on timeout so result timing in consistent and they don't stack
- Updated some dependencies

## v2.1.0 - 2016-10-24
### Added
Expand Down
2 changes: 2 additions & 0 deletions app/lib/about.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const electron = require('electron');
const dialog = electron.dialog;

const status = require('./status');
const connectivity = require('./connectivity');
const pkg = require('../package.json');

exports.show = function() {
Expand All @@ -18,5 +19,6 @@ function getMessage() {
'Author: ' + pkg.author,
'Description: ' + pkg.description,
'Homepage: ' + pkg.homepage,
'Ping mode: ' + connectivity.getMode()
].join('\n');
}
68 changes: 34 additions & 34 deletions app/lib/connectivity.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
const dns = require('dns');
const http = require('http');
/**
* DNS is usually a better way to test connectivity than HTTP
* HTTP is used regardless due to a bug in Electron/Chromium
* @see https://github.com/electron/electron/issues/2299
*/
const ping = require('./ping/http'); // require('./ping/dns')
const onetime = require('onetime');
const status = require('./status');

const HOST = {method: 'HEAD', host: 'google.com', path: '/'};
const INTERVAL = 3000;
const MAX_MS = 800;
// How many SLOW in a row to conclude
const SLOW_ATTEMPTS = 5;
const PING_INTERVAL = 3000;
const SAMPLE_SIZE = 5;

let last;
let count = 0;
let samples = [];

exports.monitor = function(cb) {
setInterval(function() { check(cb); }, INTERVAL);
setInterval(function() { check(cb); }, PING_INTERVAL);
check(cb);
};

exports.getMode = function() {
return ping.NAME;
};

function check(cb) {
ping(function(err, ms) {
const curr = getStatus(err, ms);
const start = Date.now();
ping.run(onetime(function(err) {
record(err ? Infinity : Date.now() - start);
const curr = getStatus();
if (curr !== last) {
last = curr;
cb(curr);
}
});
}));
}

function getStatus(err, ms) {
if (err) return status.OFFLINE;
if (ms <= MAX_MS) {
count = 0;
return status.ONLINE;
function record(ms) {
samples.push(ms);
if (samples.length > SAMPLE_SIZE) {
samples.shift();
}
if (++count >= SLOW_ATTEMPTS || !last) {
return status.SLOW;
}
return last;
}

function ping(done) {
// Using DNS is not good enough because it has caching
// Using native ping I don't like because it means more running processes
const start = Date.now();
const req = http.request(HOST);
req.on('response', function(res) {
// Flush
res.resume();
done(null, Date.now() - start);
})
req.on('error', done);
req.end();
function getStatus() {
const sorted = samples.concat().sort(function(a, b) {
return a - b;
});
// Use median rather than average to reduce the impact of outliers
const mid = Math.ceil((sorted.length - 1) / 2);
const median = sorted[mid];
if (median === Infinity) return status.OFFLINE;
if (median < ping.THRESHOLD) return status.ONLINE;
return status.SLOW;
}

24 changes: 24 additions & 0 deletions app/lib/ping/dns.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* DNS is usually a better way to test connectivity as it relies on less layers and is ligther
* Electron/Chromium has a bug, with no connection the callback is never called
* and Chromium logs an error to the console without any way to catch it
* @see https://github.com/electron/electron/issues/2299
*/
const dns = require('dns');

// Google DNS + OpenDNS
const IPS = ['8.8.8.8', '8.8.4.4', '208.67.222.222', '208.67.220.220'];
const HOST = 'google.com';

dns.setServers(IPS);

exports.THRESHOLD = 300;

exports.NAME = 'DNS';

exports.run = function(done) {
// dns.lookup() won't work because OS caches results
dns.resolve(HOST, done);
// TODO: How to avoid request stacking?
setTimeout(exports.THRESHOLD, done);
};
23 changes: 23 additions & 0 deletions app/lib/ping/http.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const http = require('http');

const HOST = {method: 'HEAD', host: 'google.com', path: '/'};

// Natually takes longer than dns queries
exports.THRESHOLD = 800;

exports.NAME = 'HTTP';

exports.run = function(done) {
const req = http.request(HOST);
req.on('response', function(res) {
res.resume();
done();
});
// Don't let it take ages so they don't stack
req.setTimeout(exports.THRESHOLD, function () {
req.abort();
done();
});
req.on('error', done);
req.end();
};
5 changes: 3 additions & 2 deletions app/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "wifine",
"productName": "Wi-Fine",
"version": "2.1.0",
"version": "2.2.0",
"author": "Ariel Flesler <aflesler@gmail.com>",
"description": "A connectivity monitoring app, built on Electron",
"homepage": "https://github.com/flesler/electron-wi-fine",
Expand All @@ -15,6 +15,7 @@
"electron-config": "0.2.1",
"electron-notifications": "0.1.1",
"electron-squirrel-startup": "1.0.0",
"request": "2.75.0"
"onetime": "2.0.0",
"request": "2.76.0"
}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wifine",
"version": "2.1.0",
"version": "2.2.0",
"scripts": {
"start": "electron app/ --enable-logging",
"pack": "build --dir",
Expand All @@ -10,7 +10,7 @@
},
"devDependencies": {
"electron": "1.4.4",
"electron-builder": "7.14.2"
"electron-builder": "7.15.2"
},
"repository": "flesler/electron-wi-fine",
"build": {
Expand Down

0 comments on commit 3240b8f

Please sign in to comment.