Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 61ebe19
Showing
5 changed files
with
184 additions
and
0 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 @@ | ||
/node_modules/ |
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,39 @@ | ||
# Socket retry connect | ||
|
||
Tiny helper lib that will retry connecting to tcp sockets. | ||
|
||
See index.js & test.js for examples. | ||
|
||
```js | ||
var waitForSocket = require('wait-for-socket').waitForSocket; | ||
|
||
waitForSocket({ port: 2828 }, function(err, socket) { | ||
// yey! | ||
}); | ||
|
||
``` | ||
|
||
## LICENSE | ||
|
||
Copyright (c) 2013 Mozilla Foundation | ||
|
||
Contributors: Sahaja James Lal jlal@mozilla.com | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a | ||
copy of this software and associated documentation files (the | ||
"Software"), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included | ||
in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | ||
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | ||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | ||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
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,62 @@ | ||
var debug = require('debug')('socket-retry-connect'), | ||
net = require('net'); | ||
|
||
var MAX_TRIES = 10; | ||
var WAIT_BETWEEN_TRIES_MS = 250; | ||
|
||
/** | ||
* Tries to connect to a given socket location. | ||
* Time between retires grows in relation to attempts (attempt * RETRY_TIMER). | ||
* | ||
* waitForSocket({ port: 2828, maxTries: 10 }, function(err, socket) { | ||
* }); | ||
* | ||
* Note- there is a third argument used to recursion that should | ||
* never be used publicly. | ||
* | ||
* Options: | ||
* - (Number) port: to connect to. | ||
* - (String) host: to connect to. | ||
* - (Number) tries: number of times to attempt the connect. | ||
* | ||
* @param {Object} options for connection. | ||
* @param {Function} callback [err, socket]. | ||
*/ | ||
function waitForSocket(options, callback, _tries) { | ||
if (!options.port) | ||
throw new Error('.port is a required option'); | ||
|
||
var maxTries = options.tries || MAX_TRIES; | ||
var host = options.host || 'localhost'; | ||
var port = options.port; | ||
|
||
|
||
debug('attempt to open socket', port, host); | ||
_tries = _tries || 0; | ||
if (_tries >= maxTries) | ||
return callback(new Error('cannot open socket')); | ||
|
||
function handleError() { | ||
debug('socket is not ready trying'); | ||
// retry connection | ||
setTimeout( | ||
waitForSocket, | ||
// wait at least WAIT_BETWEEN_TRIES_MS or a multiplier | ||
// of the attempts. | ||
(WAIT_BETWEEN_TRIES_MS * _tries) || WAIT_BETWEEN_TRIES_MS, | ||
options, | ||
callback, | ||
++_tries | ||
); | ||
} | ||
|
||
var socket = net.connect(port, host, function(one, two) { | ||
debug('connected', port, host); | ||
socket.removeListener('error', handleError); | ||
callback(null, socket); | ||
}); | ||
socket.once('error', handleError); | ||
} | ||
|
||
module.exports.waitForSocket = waitForSocket; | ||
|
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,25 @@ | ||
{ | ||
"name": "socket-retry-connect", | ||
"version": "0.0.1", | ||
"author": "James Lal [:lightsofapollo]", | ||
"description": "Attempt to connect to socket multiple times", | ||
"main": "index.js", | ||
"keywords": [ | ||
"socket", | ||
"retry" | ||
], | ||
"dependencies" : { | ||
"debug": "*" | ||
}, | ||
"devDependencies": { | ||
"mocha": "~1.7" | ||
}, | ||
"license": "MIT", | ||
"engine": { | ||
"node": ">=0.4" | ||
}, | ||
|
||
"scripts": { | ||
"test": "./node_modules/mocha/bin/mocha --ui tdd test.js" | ||
} | ||
} |
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,57 @@ | ||
suite('socket retry connect', function() { | ||
var PORT = 60012; | ||
var assert = require('assert'), | ||
subject = require('./index').waitForSocket, | ||
net = require('net'); | ||
|
||
var _server; | ||
function createServer(onConnect) { | ||
onConnect = onConnect || function() {}; | ||
// saves reference to last server | ||
return _server = net.createServer(onConnect).listen(PORT); | ||
} | ||
|
||
teardown(function() { | ||
// make sure we actually close the thing | ||
try { | ||
_server.close(); | ||
} catch (e) {} | ||
}); | ||
|
||
function returnsSocket(callback) { | ||
callback = callback || function() {}; | ||
return function(err, socket) { | ||
assert.ok(!err, err && err.message); | ||
assert.ok(socket instanceof net.Socket); | ||
callback(); | ||
} | ||
} | ||
|
||
test('socket is open', function(done) { | ||
var pending = 2; | ||
function next() { | ||
if (--pending === 0) { | ||
done(); | ||
} | ||
} | ||
var didConnect = false; | ||
// verify that both the server gets hit and socket callback fires. | ||
var server = createServer(next); | ||
subject({ port: PORT }, returnsSocket(next)); | ||
}); | ||
|
||
test('waits before connection is open', function(done) { | ||
this.timeout('5s'); | ||
setTimeout(createServer, 575); | ||
subject({ port: PORT }, returnsSocket(done)); | ||
}); | ||
|
||
test('fails to connect after retires', function(done) { | ||
this.timeout('3s'); | ||
subject({ port: PORT, tries: 2 }, function(err) { | ||
assert.ok(err); | ||
assert.ok(err.message.match(/socket/)); | ||
done(); | ||
}); | ||
}); | ||
}); |