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

Persist DHT nodes #1431

Open
wants to merge 9 commits into
base: master
from
Prev

Split DHT persistence options

Splits from single option dhtState into 2 options:
* persistDht - Boolean. Flag to enable.
* persistDhtPath - String. Optional custom path for save file.
  • Loading branch information
bookmoons committed Aug 6, 2018
commit 7f26a8c6e13095232aa80bef37cb03bc3fe4234b
@@ -58,7 +58,8 @@ If `opts` is specified, then the default options (shown below) will be overridde
peerId: String|Buffer, // Wire protocol peer ID (default=randomly generated)
tracker: Boolean|Object, // Enable trackers (default=true), or options object for Tracker
dht: Boolean|Object, // Enable DHT (default=true), or options object for DHT
dhtState: Boolean|String, // Persist DHT nodes (default=true), or save file path
persistDht: Boolean, // Persist DHT nodes (default=true)
persistDhtPath: String, // DHT persist save file (default=dht.json under OS appdata dir)
webSeeds: Boolean // Enable BEP19 web seeds (default=true)
}
```
@@ -80,8 +80,8 @@ function WebTorrent (opts) {
}
self.nodeIdBuffer = Buffer.from(self.nodeId, 'hex')

// Default opts.dhtState to true
if (!('dhtState' in opts)) opts.dhtState = true
// Default DHT persistence flag
if (!('persistDht' in opts)) opts.persistDht = true

self._debugId = self.peerId.toString('hex').substring(0, 7)

@@ -130,12 +130,11 @@ function WebTorrent (opts) {
if (opts.dht !== false && typeof DHT === 'function' /* browser exclude */) {
var dhtOpts = extend({ nodeId: self.nodeId }, opts.dht)

if (opts.dhtState) {
if (opts.persistDht) {
// Construct state save location
self.dhtSaveFile =
opts.dhtState === true
? path.join(appDataFolder('webtorrent'), 'dht.json')
: opts.dhtState
opts.persistDhtPath ||
path.join(appDataFolder('webtorrent'), 'dht.json')

if (!dhtOpts.bootstrap) {
// Load persisted state
@@ -154,7 +153,7 @@ function WebTorrent (opts) {
// use a single DHT instance for all torrents, so the routing table can be reused
self.dht = new DHT(dhtOpts)

if (opts.dhtState) {
if (opts.persistDht) {
// Persist state periodically
var saveInterval = 15 * 60 * 1000 // 15 minutes
self.saveDhtStateTimer = setInterval(function saveDhtState () {
@@ -18,7 +18,7 @@ test('Save DHT state', function (t) {
dhtServer.listen(port, function handleServerListening () {
var client = new WebTorrent({
dht: { bootstrap: false, host: localAddress },
dhtState: saveFile
persistDhtPath: saveFile
})
client.on('error', function (err) { t.fail(err) })
client.on('warning', function (err) { t.fail(err) })
@@ -59,7 +59,7 @@ test('Load DHT state', function (t) {
dhtServer.listen(port, function handleServerListening () {
var client = new WebTorrent({
dht: { host: localAddress },
dhtState: saveFile
persistDhtPath: saveFile
})
client.on('error', function (err) { t.fail(err) })
client.on('warning', function (err) { t.fail(err) })
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.