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

Support unannounce, move to maxAge instead of timeslotting #45

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
58 changes: 36 additions & 22 deletions lib/Grape.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ class Grape extends Events {
host: this.conf.host || false,
bootstrap: this.conf.dht_bootstrap,
timeBucketOutdated: this.conf.dht_nodeLiveness,
verify: crypto.verify
verify: crypto.verify,
maxAge: this.conf.timeslot
Copy link
Collaborator

@prdn prdn Jan 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mafintosh can we call this dht_timeslot or dht_peer_maxAge ?
edit: I'm referring to this.conf.timeslot that should be renamed into dht_peer_maxAge

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

})

dht.on('announce', (_peer, ih) => {
Expand All @@ -54,6 +55,12 @@ class Grape extends Events {
this.emit('announce', val)
})

dht.on('unannounce', (_peer, ih) => {
const val = this.hex2str(ih)
debug(this.conf.dht_port, 'unannounce', val)
this.emit('unannounce', val)
})

dht.on('warning', () => {
debug(this.conf.dht_port, 'warning')
this.emit('warning')
Expand Down Expand Up @@ -128,15 +135,6 @@ class Grape extends Events {
}
}

timeslot (offset, ts) {
offset = offset || 0
ts = ts || Date.now()
ts -= offset * this.conf.timeslot * -1
ts = ts - (ts % this.conf.timeslot)

return ts
}

onRequest (type, data, cb) {
const met = `handlePeer${_.upperFirst(_.camelCase(`-${type}`))}`

Expand All @@ -152,17 +150,10 @@ class Grape extends Events {
return cb(new Error('ERR_GRAPE_LOOKUP'))
}

async.reduce([
`${_val}-${this.timeslot(0)}`,
`${_val}-${this.timeslot(-1)}`
], [], (acc, slot, next) => {
this.lookup(
slot,
(err, res) => {
next(null, err ? acc : _.union(acc, res))
}
)
}, cb)
this.lookup(
_val,
cb
)
}

handlePeerAnnounce (data, cb) {
Expand All @@ -171,7 +162,16 @@ class Grape extends Events {
}

const [val, port] = [data[0], data[1]]
this.announce(`${val}-${this.timeslot(0)}`, port, cb)
this.announce(val, port, cb)
}

handlePeerUnannounce (data, cb) {
if (!data || !_.isArray(data)) {
return cb(new Error('ERR_GRAPE_UNANNOUNCE'))
}

const [val, port] = [data[0], data[1]]
this.unannounce(val, port, cb)
}

handlePeerPut (opts, cb) {
Expand Down Expand Up @@ -209,6 +209,19 @@ class Grape extends Events {
)
}

unannounce (val, port, cb) {
if (!_.isInteger(port)) {
return cb(new Error('ERR_GRAPE_SERVICE_PORT'))
}

cb = cb || noop
this.node.unannounce(
this.str2hex(val),
port || this.conf.dht_port,
cb
)
}

lookup (val, cb) {
const ih = this.str2hex(val)

Expand All @@ -219,6 +232,7 @@ class Grape extends Events {
const kcnt = this.cbq0.cnt(ih)
if (kcnt > 1) return

this._mem.remove(ih)
this.node.lookup(ih, (err, cnt) => {
debug(`lookup ${val} found ${cnt} nodes`)

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
],
"dependencies": {
"async": "^2.6.0",
"bittorrent-dht": "^7.8.2",
"bittorrent-dht": "webtorrent/bittorrent-dht#unannounce",
"cbq": "0.0.1",
"debug": "^2.2.0",
"ed25519-supercop": "^1.0.2",
Expand Down
18 changes: 0 additions & 18 deletions test/Grape.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,6 @@ describe('Grape', () => {
grape.stop(done)
})

it('timeslot returns the same for immediate calls', (done) => {
const grape = new Grape({
dht_port: 20000,
api_port: 20001
})

const now = Date.now()

const ts1 = grape.timeslot(0, now)
const ts2 = grape.timeslot(0, now)
assert.equal(ts1, ts2)

const ts3 = grape.timeslot(-1, now)
const ts4 = grape.timeslot(-1, now)
assert.equal(ts3, ts4)
done()
})

it('requires a port', (done) => {
const grape = new Grape({
dht_port: 20000
Expand Down