Permalink
Browse files

backoff and bug fixes

  • Loading branch information...
1 parent 9430271 commit 878041ad6da539482b72c5f3373970462b3e6d1b @Raynos committed Aug 6, 2012
Showing with 16 additions and 12 deletions.
  1. +14 −11 browser.js
  2. +2 −1 package.json
View
25 browser.js
@@ -5,15 +5,21 @@ var shoe = require("mux-demux-shoe")
, through = require("through")
, PauseStream = require("pause-stream")
, es = require("event-stream")
+ , Backoff = require("backoff").fibonnaci
+ , BACKOFF_OPTIONS = {
+ initialDelay: 500,
+ maxDelay: 10000
+ }
module.exports = reconnecter
function reconnecter(uri) {
- var proxyWrite = PauseStream()
+ var proxyWrite = through()
, proxyRead = through()
, proxy = es.duplex(proxyWrite, proxyRead)
, metaStreams = []
, stream
+ , backoff = Backoff(BACKOFF_OPTIONS)
proxy.createStream = createStream
proxy.createWriteStream = createWriteStream
@@ -30,23 +36,22 @@ function reconnecter(uri) {
stream.once("connect", onconnect)
- proxyRead.pipe(stream).pipe(proxyWrite, {
- end: false
- })
+ proxyWrite.pipe(stream).pipe(proxyRead)
stream.once("end", onend)
}
function onconnect() {
- proxyWrite.resume()
+ backoff.reset()
proxy.emit("connect")
}
function onend() {
- proxyWrite.pause()
proxy.emit("disconnect")
+
// wait a second otherwise it spin locks
- setTimeout(createShoeStream, 1000)
+ var delay = backoff.backoffStrategy_.next()
+ setTimeout(createShoeStream, delay)
}
function proxyMdmStream(details) {
@@ -55,15 +60,13 @@ function reconnecter(uri) {
var mdm = stream.createStream(details.meta, details.opts)
- proxyRead.pipe(mdm).pipe(proxyWrite, {
- end: false
- })
+ proxyWrite.pipe(mdm).pipe(proxyRead)
stream.once("end", mdm.end.bind(mdm))
}
function createStream(meta, opts) {
- var proxyWrite = PauseStream()
+ var proxyWrite = through()
, proxyRead = through()
, proxy = es.duplex(proxyWrite, proxyRead)
View
3 package.json
@@ -21,7 +21,8 @@
"mux-demux-shoe": "~0.1.1",
"through": "~0.1.2",
"pause-stream": "0.0.3",
- "event-stream": "~2.1.8"
+ "event-stream": "~2.1.8",
+ "backoff": "~0.2.0"
},
"devDependencies": {
"browserify": "~1.14.4",

0 comments on commit 878041a

Please sign in to comment.