Skip to content

Commit

Permalink
Configurable API Poll Interval
Browse files Browse the repository at this point in the history
  • Loading branch information
lidel committed Feb 4, 2016
1 parent 2fc5480 commit 0d53002
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 8 deletions.
11 changes: 7 additions & 4 deletions lib/peer-watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ const gui = require('./gui.js')
const gw = require('./gateways.js')
const _ = require('sdk/l10n').get

const interval = 3000

function ipfsHealthCheck () {
api.getSwarmPeers((peers) => {
const peerCount = (peers && peers.Strings) ? peers.Strings.length : null
Expand All @@ -28,10 +26,15 @@ function ipfsHealthCheck () {
})
}

const checkRunner = setInterval(ipfsHealthCheck, interval)
let checkRunner = null
require('sdk/simple-prefs').on('apiPollInterval', (function f () {
clearInterval(checkRunner)
checkRunner = setInterval(ipfsHealthCheck, prefs.apiPollInterval)
return f
})())

require('sdk/system/unload').when(() => {
clearInterval(checkRunner)
})

exports.interval = interval
exports.triggerManualCheck = ipfsHealthCheck
3 changes: 3 additions & 0 deletions locale/en-US.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ customGatewayPort_description=HTTP2IPFS gateway service (eg. local instance of g
customApiPort_title=IPFS API Port
customApiPort_description=Used for pinning (on the Gateway host)

apiPollInterval_title=API Poll Interval
apiPollInterval_description=How often peer connection number is updated from API (in miliseconds)

automatic_title=Automatic Mode
automatic_description=Periodically fetch peer information via API and automatically enable/disable redirect based on the response.
automatic_up_notification_title=IPFS is Up
Expand Down
3 changes: 3 additions & 0 deletions locale/pl-PL.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ customGatewayPort_description=Usługa bramy HTTP2IPFS (np. z lokalnej instancji
customApiPort_title=Port IPFS API
customApiPort_description=Używany do przypinania (na hoście Bramy)

apiPollInterval_title=Częstotliwość zapytań do API
apiPollInterval_description=Jak często API jest odpytywane o liczbę dostępnych węzłów (w milisekundach)

automatic_title=Tryb automatyczny
automatic_description=Okresowo pobiera z API informacje o połączeniach i automatycznie włącza/wyłącza przekierowanie na podstawie odebranych danych.
automatic_up_notification_title=IPFS jest dostępne
Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@
"type": "integer",
"value": 5001
},
{
"name": "apiPollInterval",
"title": "API Poll Interval",
"type": "integer",
"value": 3000
},
{
"name": "dns",
"title": "Enable DNS Lookup",
Expand Down
12 changes: 8 additions & 4 deletions test/test-automatic-mode.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
'use strict'

require('../lib/peer-watch.js')

const { setTimeout } = require('sdk/timers')
const { prefs } = require('sdk/simple-prefs')
const tabs = require('sdk/tabs')
const gw = require('../lib/gateways.js')
const autoMode = require('../lib/peer-watch.js')

const ipfsPath = 'ipfs/QmTkzDwWqPbnAh5YiV5VwcTLnGdwSNsNTn2aDxdXBFca7D/'

exports['test automatic mode disabling redirect when IPFS API is offline'] = function (assert, done) {
let apiPort = prefs.customApiPort
const origApiPort = prefs.customApiPort
const origApiPollInterval = prefs.apiPollInterval
prefs.apiPollInterval = 100 // faster test
prefs.customApiPort = 59999 // change to something that will always fail
prefs.useCustomGateway = true
prefs.automatic = true
Expand All @@ -23,11 +26,12 @@ exports['test automatic mode disabling redirect when IPFS API is offline'] = fun
onReady: function onReady (tab) {
assert.equal(tab.url, 'http://ipfs.io/' + ipfsPath, 'expected no redirect')
prefs.automatic = false
prefs.customApiPort = apiPort
prefs.customApiPort = origApiPort
prefs.apiPollInterval = origApiPollInterval
tab.close(done)
}
})
}, autoMode.interval + 100)
}, prefs.apiPollInterval + 500)
}

require('sdk/test').run(exports)

0 comments on commit 0d53002

Please sign in to comment.