Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: id and child logger for request and response #27

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added id and child logger to request and response
delvedor committed Dec 20, 2017
commit d98d013b67ef51b869b3a6ace5e383ed59d7e497
19 changes: 18 additions & 1 deletion lib/tcp-server.js
Original file line number Diff line number Diff line change
@@ -9,6 +9,9 @@ module.exports = function serverPlugin (upring, opts, next) {
upring._server.listen(opts.port, opts.host, onListen)
upring._server.on('error', upring.emit.bind(upring, 'error'))

const genReqId = opts.genReqId || reqIdGenFactory()
upring.genReqId = genReqId

function handler (stream) {
if (upring.closed) {
stream.on('error', noop)
@@ -24,13 +27,27 @@ module.exports = function serverPlugin (upring, opts, next) {
upring.log.debug({ address: stream.address() }, 'closed connection')
upring._inbound.delete(instance)
})
instance.on('request', upring._dispatch)
instance.on('request', onRequest)
}

function onRequest (req, reply) {
req.id = genReqId(req)
req.log = upring.log.child({ reqId: req.id })
upring._dispatch(req, reply)
}

function onListen () {
upring.log.debug({ address: upring._server.address() }, 'listening')
next()
}

function reqIdGenFactory () {
var maxInt = 2147483647
var nextReqId = 0
return function _genReqId (req) {
return req.id || (nextReqId = (nextReqId + 1) & maxInt)
}
}
}

function noop () {}
6 changes: 6 additions & 0 deletions upring.js
Original file line number Diff line number Diff line change
@@ -208,6 +208,8 @@ UpRing.prototype.request = function (obj, callback, _count) {
return
}

obj.id = this.genReqId(obj)

if (this._hashring.allocatedToMe(obj.key)) {
this.log.trace({ msg: obj }, 'local call')
this._dispatch(obj, dezalgo(callback))
@@ -249,6 +251,10 @@ UpRing.prototype.request = function (obj, callback, _count) {
return
}
}
if (obj.id != null) {
result.id = obj.id
result.log = this.log.child({ reqId: result.id })
}
callback(err, result)
})
}