Skip to content

Commit 51bdb6d

Browse files
committed
support unannounce, move to maxAge instead of timeslotting
1 parent 8bb5fe5 commit 51bdb6d

File tree

2 files changed

+37
-23
lines changed

2 files changed

+37
-23
lines changed

lib/Grape.js

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ class Grape extends Events {
4545
host: this.conf.host || false,
4646
bootstrap: this.conf.dht_bootstrap,
4747
timeBucketOutdated: this.conf.dht_nodeLiveness,
48-
verify: crypto.verify
48+
verify: crypto.verify,
49+
maxAge: this.conf.timeslot
4950
})
5051

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

58+
dht.on('unannounce', (_peer, ih) => {
59+
const val = this.hex2str(ih)
60+
debug(this.conf.dht_port, 'unannounce', val)
61+
this.emit('unannounce', val)
62+
})
63+
5764
dht.on('warning', () => {
5865
debug(this.conf.dht_port, 'warning')
5966
this.emit('warning')
@@ -128,15 +135,6 @@ class Grape extends Events {
128135
}
129136
}
130137

131-
timeslot (offset, ts) {
132-
offset = offset || 0
133-
ts = ts || Date.now()
134-
ts -= offset * this.conf.timeslot * -1
135-
ts = ts - (ts % this.conf.timeslot)
136-
137-
return ts
138-
}
139-
140138
onRequest (type, data, cb) {
141139
const met = `handlePeer${_.upperFirst(_.camelCase(`-${type}`))}`
142140

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

155-
async.reduce([
156-
`${_val}-${this.timeslot(0)}`,
157-
`${_val}-${this.timeslot(-1)}`
158-
], [], (acc, slot, next) => {
159-
this.lookup(
160-
slot,
161-
(err, res) => {
162-
next(null, err ? acc : _.union(acc, res))
163-
}
164-
)
165-
}, cb)
153+
this.lookup(
154+
_val,
155+
cb
156+
)
166157
}
167158

168159
handlePeerAnnounce (data, cb) {
@@ -171,7 +162,16 @@ class Grape extends Events {
171162
}
172163

173164
const [val, port] = [data[0], data[1]]
174-
this.announce(`${val}-${this.timeslot(0)}`, port, cb)
165+
this.announce(val, port, cb)
166+
}
167+
168+
handlePeerUnannounce (data, cb) {
169+
if (!data || !_.isArray(data)) {
170+
return cb(new Error('ERR_GRAPE_UNANNOUNCE'))
171+
}
172+
173+
const [val, port] = [data[0], data[1]]
174+
this.unannounce(val, port, cb)
175175
}
176176

177177
handlePeerPut (opts, cb) {
@@ -209,6 +209,19 @@ class Grape extends Events {
209209
)
210210
}
211211

212+
unannounce (val, port, cb) {
213+
if (!_.isInteger(port)) {
214+
return cb(new Error('ERR_GRAPE_SERVICE_PORT'))
215+
}
216+
217+
cb = cb || noop
218+
this.node.unannounce(
219+
this.str2hex(val),
220+
port || this.conf.dht_port,
221+
cb
222+
)
223+
}
224+
212225
lookup (val, cb) {
213226
const ih = this.str2hex(val)
214227

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

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
],
1212
"dependencies": {
1313
"async": "^2.6.0",
14-
"bittorrent-dht": "^7.8.2",
14+
"bittorrent-dht": "webtorrent/bittorrent-dht#unannounce",
1515
"cbq": "0.0.1",
1616
"debug": "^2.2.0",
1717
"ed25519-supercop": "^1.0.2",

0 commit comments

Comments
 (0)