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

Add hypertrace instrumentation #74

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const Session = require('./lib/session')
const peer = require('./lib/peer')
const { UNKNOWN_COMMAND, INVALID_TOKEN } = require('./lib/errors')
const { PING, PING_NAT, FIND_NODE, DOWN_HINT } = require('./lib/commands')
const { createTracer } = require('hypertrace')

const TMP = b4a.allocUnsafe(32)
const TICK_INTERVAL = 5000
Expand All @@ -26,6 +27,7 @@ class DHT extends EventEmitter {
constructor (opts = {}) {
super()

this.tracer = createTracer(this)
this.bootstrapNodes = opts.bootstrap === false ? [] : (opts.bootstrap || []).map(parseNode)
this.table = new Table(randomBytes(32))
this.nodes = new TOS()
Expand Down Expand Up @@ -113,13 +115,15 @@ class DHT extends EventEmitter {
async suspend () {
await this.io.bind()
if (this.suspended || this.destroyed) return
this.tracer.trace('suspend')
this.suspended = true
this.io.suspend()
this.emit('suspend')
}

async resume () {
if (!this.suspended || this.destroyed) return
this.tracer.trace('resume')
this.suspended = false
this._onwakeup()
await this.io.resume()
Expand Down Expand Up @@ -189,6 +193,7 @@ class DHT extends EventEmitter {

query ({ target, command, value }, opts) {
if (this.destroyed) throw new Error('Node destroyed')
this.tracer.trace('query', { command, value })
this._refreshTicks = REFRESH_TICKS
return new Query(this, target, false, command, value || null, opts)
}
Expand All @@ -203,6 +208,7 @@ class DHT extends EventEmitter {
}

request ({ token = null, command, target = null, value = null }, { host, port }, opts) {
this.tracer.trace('request', { command, value, host, port })
const req = this.io.createRequest({ id: null, host, port }, token, false, command, target, value, (opts && opts.session) || null, (opts && opts.ttl))
return this._requestToPromise(req, opts)
}
Expand Down Expand Up @@ -274,6 +280,7 @@ class DHT extends EventEmitter {
}

async destroy () {
this.tracer.trace('destroy')
const emitClose = !this.destroyed
this.destroyed = true
clearInterval(this._tickInterval)
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"compact-encoding-net": "^1.2.0",
"events": "^3.3.0",
"fast-fifo": "^1.1.0",
"hypertrace": "^1.2.1",
"kademlia-routing-table": "^1.0.1",
"nat-sampler": "^1.0.1",
"sodium-universal": "^4.0.0",
Expand Down
Loading