Permalink
Browse files

lazily create 'static' Endpoints

  • Loading branch information...
dannycoates committed Jun 13, 2012
1 parent 7cd4b13 commit 1f198504860ec8c2ce3a91640a3f8cf39bfb04ed
Showing with 34 additions and 31 deletions.
  1. +34 −31 lib/endpoint.js
View
@@ -40,12 +40,11 @@ module.exports = function (inherits, EventEmitter) {
this.successes = 0
this.failures = 0
- this.timeout = options.timeout || (60 * 1000)
+ this.timeout = (options.timeout === 0) ? 0 : options.timeout || (60 * 1000)
this.resolution = (options.resolution === 0) ? 0 : options.resolution || 1000
-
- this.timeoutInterval = (this.resolution > 0) ?
- setInterval(this.checkTimeouts.bind(this), this.resolution) :
- null
+ if (this.resolution > 0 && this.timeout > 0) {
+ this.timeoutInterval = setInterval(this.checkTimeouts.bind(this), this.resolution)
+ }
if (!clockInterval) {
clockInterval = setInterval(function () { clock = Date.now() }, 10)
@@ -136,9 +135,9 @@ module.exports = function (inherits, EventEmitter) {
req.node = this
req.options = options
req.id = this.requestCount++
- req.stream = (options.stream === undefined) ? callback.length === 2 : options.stream
- req.lastTouched = clock
req.callback = callback || noop
+ req.stream = (options.stream === undefined) ? req.callback.length === 2 : options.stream
+ req.lastTouched = clock
req.on('response', gotResponse)
req.on('error', gotError)
@@ -269,37 +268,41 @@ module.exports = function (inherits, EventEmitter) {
, this.request)
}
- var overloaded = new Endpoint({Agent: Object})
- clearInterval(overloaded.timeoutInterval)
- overloaded.healthy = false
- overloaded.request = function (options, callback) {
- return callback(
- { reason: 'full'
- , delay: true
- , attempt: { options: options }
- , message: 'too many pending requests'
- }
- )
- }
+ var overloaded = null
Endpoint.overloaded = function () {
+ if (!overloaded) {
+ overloaded = new Endpoint({Agent: Object}, null, null, {timeout: 0})
+ overloaded.healthy = false
+ overloaded.request = function (options, callback) {
+ return callback(
+ { reason: 'full'
+ , delay: true
+ , attempt: { options: options }
+ , message: 'too many pending requests'
+ }
+ )
+ }
+ }
return overloaded
}
- var unhealthy = new Endpoint({Agent: Object})
- clearInterval(unhealthy.timeoutInterval)
- unhealthy.healthy = false
- unhealthy.request = function (options, callback) {
- return callback(
- { reason: 'unhealthy'
- , delay: true
- , attempt: { options: options }
- , message: 'no nodes'
- }
- )
- }
+ var unhealthy = null
Endpoint.unhealthy = function () {
+ if (!unhealthy) {
+ unhealthy = new Endpoint({Agent: Object}, null, null, {timeout: 0})
+ unhealthy.healthy = false
+ unhealthy.request = function (options, callback) {
+ return callback(
+ { reason: 'unhealthy'
+ , delay: true
+ , attempt: { options: options }
+ , message: 'no nodes'
+ }
+ )
+ }
+ }
return unhealthy
}

0 comments on commit 1f19850

Please sign in to comment.