-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Using HEAD requests to Google to monitor connectivity, slow status no…
…w supported
- Loading branch information
Showing
9 changed files
with
68 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# editorconfig.org | ||
|
||
root = true | ||
|
||
[*] | ||
charset = utf-8 | ||
end_of_line = lf | ||
|
||
[*.js] | ||
indent_size = 2 | ||
indent_style = tab | ||
insert_final_newline = true | ||
trim_trailing_whitespace = true |
File renamed without changes.
File renamed without changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,54 @@ | ||
const Monitor = new require('electron-online'); | ||
const dns = require('dns'); | ||
const http = require('http'); | ||
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; | ||
|
||
let last; | ||
let count = 0; | ||
|
||
exports.monitor = function(cb) { | ||
const monitor = new Monitor(); | ||
monitor.on('online', function() { | ||
cb(status.ONLINE); | ||
}); | ||
monitor.on('offline', function() { | ||
cb(status.OFFLINE); | ||
}); | ||
setInterval(function() { check(cb); }, INTERVAL); | ||
check(cb); | ||
}; | ||
|
||
function check(cb) { | ||
ping(function(err, ms) { | ||
const curr = getStatus(err, ms); | ||
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; | ||
} | ||
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(); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters