Permalink
Browse files

event handler accounting

  • Loading branch information...
1 parent 24877e7 commit 956284f2d07e7b0dc805982020abb953a4481ae3 @dannycoates committed Nov 17, 2012
Showing with 301 additions and 268 deletions.
  1. +0 −27 broker-pool.js
  2. +53 −33 broker.js
  3. +55 −43 client/client.js
  4. +8 −8 client/fetch-body.js
  5. +8 −8 client/produce-request.js
  6. +33 −27 client/receiver.js
  7. +34 −34 message-buffer.js
  8. +33 −23 partition-set.js
  9. +62 −56 partition.js
  10. +15 −9 topic.js
View
@@ -38,33 +38,6 @@ module.exports = function (logger, inherits, EventEmitter) {
return this.brokers[this.current]
}
- BrokerPool.prototype.nextReady = function () {
- for (var i = 0; i < this.brokers.length; i++) {
- var b = this.next()
- if (b.isReady()) {
- break
- }
- }
- return b
- }
-
- BrokerPool.prototype.randomReady = function () {
- var len = this.brokers.length
- var n = (Math.floor(Math.random() * len))
- for (var i = 0; i < len; i++) {
- var b = this.brokers[n]
- if (b.isReady()) {
- break
- }
- n = (n + 1) % len
- }
- return b
- }
-
- BrokerPool.prototype.areAnyReady = function () {
- return this.brokers.some(function (b) { return b.isReady() })
- }
-
BrokerPool.prototype.get = function (id) {
return this.brokersById[id]
}
View
@@ -11,16 +11,13 @@ module.exports = function (
this.reconnectAttempts = 0
this.options = options
this.connector = this.connect.bind(this)
+ this.onClientEnd = clientEnd.bind(this)
+ this.onClientReady = clientReady.bind(this)
+ this.onClientConnect = clientConnect.bind(this)
EventEmitter.call(this)
}
inherits(Broker, EventEmitter)
- function exponentialBackoff(attempt) {
- return Math.floor(
- Math.random() * Math.pow(2, attempt) * 10
- )
- }
-
Broker.prototype.connect = function () {
var options = this.options
logger.info(
@@ -29,33 +26,12 @@ module.exports = function (
'port', options.port
)
this.client = new Client(this.id, options)
- this.client.once(
- 'connect',
- function () {
- logger.info('broker connected', this.id)
- this.reconnectAttempts = 0
- this.emit('connect', this)
- this.emit('ready')
- }.bind(this)
- )
- this.client.once(
- 'end',
- function () {
- this.reconnectAttempts++
- logger.info('broker ended', this.id, this.reconnectAttempts)
- setTimeout(
- this.connector,
- exponentialBackoff(this.reconnectAttempts)
- )
- }.bind(this)
- )
- this.client.on(
- 'ready',
- function () {
- logger.info('broker ready', this.id)
- this.emit('ready')
- }.bind(this)
- )
+
+ this.client.once('connect', this.onClientConnect)
+ this.client.once('end', this.onClientEnd)
+ this.client.on('ready', this.onClientReady)
+
+ this.reconnectTimer = null
}
Broker.prototype.isReady = function () {
@@ -74,6 +50,50 @@ module.exports = function (
this.client.drain(cb)
}
+ Broker.prototype.destroy = function () {
+ clearTimeout(this.reconnectTimer)
+ this.reconnectTimer = null
+ this.client.removeListener('connect', this.onClientConnect)
+ this.client.removeListener('end', this.onClientEnd)
+ this.client.removeListener('ready', this.onClientReady)
+ this.client.end()
+ this.client = Client.nil
+ this.emit('destroy')
+ }
+
+ function exponentialBackoff(attempt) {
+ return Math.floor(
+ Math.random() * Math.pow(2, attempt) * 10
+ )
+ }
+
+ function clientConnect() {
+ logger.info('broker connected', this.id)
+ this.reconnectAttempts = 0
+ this.emit('connect', this)
+ this.emit('ready')
+ }
+
+ function clientEnd() {
+ this.reconnectAttempts++
+ logger.info(
+ 'broker ended', this.id,
+ 'reconnects', this.reconnectAttempts
+ )
+ this.client.removeListener('connect', this.onClientConnect)
+ this.client.removeListener('end', this.onClientEnd)
+ this.client.removeListener('ready', this.onClientReady)
+ this.reconnectTimer = setTimeout(
+ this.connector,
+ exponentialBackoff(this.reconnectAttempts)
+ )
+ }
+
+ function clientReady() {
+ logger.info('broker ready', this.id)
+ this.emit('ready')
+ }
+
Broker.nil = new Broker()
return Broker
View
@@ -11,53 +11,24 @@ module.exports = function (
OffsetsRequest
) {
function Client(id, options) {
- this.connection = net.connect(options)
- this.connection.on(
- 'connect',
- function () {
- logger.info('client connect')
- this.readableSteam = new ReadableStream()
- this.readableSteam.wrap(this.connection)
- this.receiver = new Receiver(this.readableSteam)
- this.ready = true
- this.emit('connect')
- }.bind(this)
- )
- this.connection.on(
- 'end',
- function () {
- logger.info('client end')
- this.ready = false
- this.emit('end')
- this.connection = null
- }.bind(this)
- )
- this.connection.on(
- 'drain',
- function () {
- if (!this.ready) { //TODO: why is connection.drain so frequent?
- this.ready = true
- this.emit('ready')
- }
- }.bind(this)
- )
- this.connection.on(
- 'error',
- function (err) {
- logger.info('client error', err.message)
- }
- )
- this.connection.on(
- 'close',
- function (hadError) {
- logger.info('client closed. with error:', hadError)
- this.emit('end')
- }.bind(this)
- )
this.id = id
this.ready = false
this.readableSteam = null
this.receiver = null
+
+ this.connection = net.connect(options)
+ this.onConnectionConnect = connectionConnect.bind(this)
+ this.onConnectionEnd = connectionEnd.bind(this)
+ this.onConnectionDrain = connectionDrain.bind(this)
+ this.onConnectionError = connectionError.bind(this)
+ this.onConnectionClose = connectionClose.bind(this)
+
+ this.connection.on('connect', this.onConnectionConnect)
+ this.connection.on('end', this.onConnectionEnd)
+ this.connection.on('drain', this.onConnectionDrain)
+ this.connection.on('error', this.onConnectionError)
+ this.connection.on('close', this.onConnectionClose)
+
EventEmitter.call(this)
}
inherits(Client, EventEmitter)
@@ -74,6 +45,10 @@ module.exports = function (
)
}
+ Client.prototype.end = function () {
+ this.connection.end()
+ }
+
Client.prototype._send = function (request, cb) {
request.serialize(
this.connection,
@@ -137,5 +112,42 @@ module.exports = function (
Client.nil = { ready: false }
+ function connectionConnect() {
+ logger.info('client connect')
+ this.readableSteam = new ReadableStream()
+ this.readableSteam.wrap(this.connection)
+ this.receiver = new Receiver(this.readableSteam)
+ this.ready = true
+ this.emit('connect')
+ }
+
+ function connectionEnd() {
+ logger.info('client end')
+ this.ready = false
+ this.emit('end')
+ this.connection = null
+ }
+
+ function connectionDrain() {
+ if (!this.ready) { //TODO: why is connection.drain so frequent?
+ this.ready = true
+ this.emit('ready')
+ }
+ }
+
+ function connectionError() {
+ logger.info('client error', err.message)
+ }
+
+ function connectionClose(hadError) {
+ logger.info('client closed. with error', hadError)
+ this.connection.removeListener('connect', this.onConnectionConnect)
+ this.connection.removeListener('end', this.onConnectionEnd)
+ this.connection.removeListener('drain', this.onConnectionDrain)
+ this.connection.removeListener('error', this.onConnectionError)
+ this.connection.removeListener('close', this.onConnectionClose)
+ this.emit('end')
+ }
+
return Client
}
View
@@ -3,14 +3,6 @@ module.exports = function (
State,
Message) {
- function FetchError(message, messageLength) {
- this.message = message
- this.messageLength = messageLength
- Error.call(this)
- }
- inherits(FetchError, Error)
- FetchError.prototype.name = 'Fetch Error'
-
function FetchBody(bytes) {
this.bytesParsed = 0
this.lastMessageLength = 0
@@ -55,5 +47,13 @@ module.exports = function (
return err
}
+ function FetchError(message, messageLength) {
+ this.message = message
+ this.messageLength = messageLength
+ Error.call(this)
+ }
+ inherits(FetchError, Error)
+ FetchError.prototype.name = 'Fetch Error'
+
return FetchBody
}
View
@@ -4,14 +4,6 @@ module.exports = function (
Message,
State) {
- function ProduceError(message, length) {
- this.message = message
- this.length = length
- Error.call(this)
- }
- inherits(ProduceError, Error)
- ProduceError.prototype.name = 'Produce Error'
-
function ProduceRequest(topic, partitionId, messages) {
this.topic = topic
this.partitionId = partitionId
@@ -91,5 +83,13 @@ module.exports = function (
return State.done
}
+ function ProduceError(message, length) {
+ this.message = message
+ this.length = length
+ Error.call(this)
+ }
+ inherits(ProduceError, Error)
+ ProduceError.prototype.name = 'Produce Error'
+
return ProduceRequest
}
Oops, something went wrong.

0 comments on commit 956284f

Please sign in to comment.