Skip to content

Commit

Permalink
some linting
Browse files Browse the repository at this point in the history
reuse the server proxy method when forwarding request
whitespace junk
  • Loading branch information
esatterwhite committed Dec 28, 2016
1 parent 9104910 commit 2f30ba3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 35 deletions.
42 changes: 15 additions & 27 deletions lib/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ class Server extends http.Server {
this._node = opts.node || new Node();
this._router = new Router(this._node);
this._node.on('ringchange', (evt) => {
timer.rebalance(evt, this._node, (data) => {
this.proxy(data);
})
})
timer.rebalance(evt, this._node, (data) => {
this.proxy(data);
});
});
}

/**
Expand Down Expand Up @@ -72,34 +72,22 @@ class Server extends http.Server {
}

/**
 * Joins the node to the configured ring and starts the http server
 * @method module:skyring/lib/server#listen
 * @param {Number} port Port number to listen on
 * @param {String} [host=localhost] host or ip address to listen on
* Joins the node to the configured ring and starts the http server
* @method module:skyring/lib/server#listen
* @param {Number} port Port number to listen on
* @param {String} [host=localhost] host or ip address to listen on
* @param {Number} [backlog]
* @param {Function} [callback] Callback function to call when the server is running
 * @return {module:skyring/lib/server}
 **/
* @return {module:skyring/lib/server}
**/
listen(port, host, backlog, callback) {
this._node.join(null, (err) => {
if (err) return callback(err)
this._node.handle(( req, res ) => {
this._router.handle( req, res );
})
timer.watch('skyring', (err, data) => {
debug('fabricating request', data)
const opts = {
url: '/timer'
, method: 'POST'
, headers: {
"x-timer-id": data.id
}
, payload: JSON.stringify(data)
}
const res = new mock.Response();
const req = new mock.Request( opts );

this._router.handle( req, res );
this.proxy(data)
});
super.listen(port, host, backlog, callback)
})
Expand All @@ -122,11 +110,11 @@ class Server extends http.Server {
this._router.handle( req, res );
}
/**
 * Removes a server from the ring, closes the http server and redistributes
* Removes a server from the ring, closes the http server and redistributes
* any pending timers
 * @method module:skyring/lib/server#close
 * @param {Function} callback A callback to be called when the server is completely shut down
 **/
* @method module:skyring/lib/server#close
* @param {Function} callback A callback to be called when the server is completely shut down
**/
close( cb ){

super.close(()=>{
Expand Down
16 changes: 8 additions & 8 deletions lib/timer.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,25 +100,25 @@ exports.delete = function(id, cb) {
debug('timer cleared', id)
cb && setImmediate(cb, null);
}


exports.rebalance = function(opts, node, cb) {
const callback = cb || noop;
const size = cache.size;
const client = nats.client;
const callback = cb || noop
, size = cache.size
, client = nats.client
;

if( !size ) return;
const records = cache.values();
const run = ( obj ) => {
if (node.owns(obj.id)) {
return;
}
rebalance('no longer the owner of %s', obj.id);
if (node.owns(obj.id)) return;
clearTimeout( obj.timer );
cache.delete( obj.id );
const data = Object.assign({}, obj.payload, {
id: obj.id
, created: obj.created
, count: ++sent
});
rebalance('no longer the owner of %s', obj.id);
cb(data)
}

Expand Down

0 comments on commit 2f30ba3

Please sign in to comment.