From 68561c89e97cc313e40ea6e7da8114d78b8045c9 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Thu, 4 Apr 2019 13:00:11 +0100 Subject: [PATCH] fix: stop IPNS republisher ASAP (#1976) This PR refactors the stop logic to perform stop actions in parallel for various components (where it is safe to do so). This means that libp2p gets stopped at the same time as the republisher. When libp2p stops it closes all connections meaning that the republsher is able to complete a running republish sooner. Before, we had to wait for republish to complete before stop happens (since there's no way to cancel a DHT put right now). License: MIT Signed-off-by: Alan Shaw --- package.json | 4 ++-- src/core/components/stop.js | 36 ++++++++++++++++++------------------ test/core/interface.spec.js | 25 ++++++++++++++++++++++++- test/core/preload.spec.js | 13 ++++++++++--- test/http-api/interface.js | 25 ++++++++++++++++++++++++- 5 files changed, 78 insertions(+), 25 deletions(-) diff --git a/package.json b/package.json index f7600f3248..d18da53b4a 100644 --- a/package.json +++ b/package.json @@ -68,7 +68,7 @@ "execa": "^1.0.0", "form-data": "^2.3.3", "hat": "0.0.3", - "interface-ipfs-core": "~0.99.0", + "interface-ipfs-core": "~0.99.1", "ipfsd-ctl": "~0.42.0", "libp2p-websocket-star": "~0.10.2", "ncp": "^2.0.0", @@ -130,7 +130,7 @@ "libp2p": "~0.25.0-rc.5", "libp2p-bootstrap": "~0.9.3", "libp2p-crypto": "~0.16.0", - "libp2p-kad-dht": "~0.14.11", + "libp2p-kad-dht": "~0.14.12", "libp2p-keychain": "~0.4.1", "libp2p-mdns": "~0.12.0", "libp2p-mplex": "~0.8.4", diff --git a/src/core/components/stop.js b/src/core/components/stop.js index 2981e66842..5f2ddc7959 100644 --- a/src/core/components/stop.js +++ b/src/core/components/stop.js @@ -1,6 +1,6 @@ 'use strict' -const series = require('async/series') +const parallel = require('async/parallel') const promisify = require('promisify-es6') module.exports = (self) => { @@ -17,30 +17,30 @@ module.exports = (self) => { return callback(new Error('Not able to stop from state: ' + self.state.state())) } - const done = (err) => { - if (err) { - self.emit('error', err) - return callback(err) - } - self.state.stopped() - self.emit('stop') - callback() - } - self.state.stop() self._blockService.unsetExchange() self._bitswap.stop() self._preload.stop() - series([ - (cb) => self._ipns.republisher.stop(cb), - (cb) => self._mfsPreload.stop(cb), - (cb) => { + parallel([ + cb => self._ipns.republisher.stop(cb), + cb => self._mfsPreload.stop(cb), + cb => { const libp2p = self.libp2p self.libp2p = null libp2p.stop(cb) - }, - (cb) => self._repo.close(cb) - ], done) + } + ], err => { + self._repo.close(closeErr => { + if (err || closeErr) { + self.emit('error', err || closeErr) + return callback(err || closeErr) + } + + self.state.stopped() + self.emit('stop') + callback() + }) + }) }) } diff --git a/test/core/interface.spec.js b/test/core/interface.spec.js index 21531d294e..8134ffc702 100644 --- a/test/core/interface.spec.js +++ b/test/core/interface.spec.js @@ -79,7 +79,18 @@ describe('interface-ipfs-core tests', function () { tests.key(CommonFactory.create({ spawnOptions: { args: ['--pass ipfs-is-awesome-software'], - initOptions: { bits: 512 } + initOptions: { bits: 512 }, + config: { + Bootstrap: [], + Discovery: { + MDNS: { + Enabled: false + }, + webRTCStar: { + Enabled: false + } + } + } } })) @@ -183,6 +194,18 @@ describe('interface-ipfs-core tests', function () { config = null } + config = config || { + Bootstrap: [], + Discovery: { + MDNS: { + Enabled: false + }, + webRTCStar: { + Enabled: false + } + } + } + const spawnOptions = { repoPath, config, initOptions: { bits: 512 } } ipfsFactory.spawn(spawnOptions, (err, _ipfsd) => { diff --git a/test/core/preload.spec.js b/test/core/preload.spec.js index 4d4bd4848f..edb3c2153b 100644 --- a/test/core/preload.spec.js +++ b/test/core/preload.spec.js @@ -340,7 +340,8 @@ describe('preload disabled', function () { config: { Addresses: { Swarm: [] - } + }, + Bootstrap: [] }, preload: { enabled: false, @@ -353,9 +354,15 @@ describe('preload disabled', function () { afterEach((done) => MockPreloadNode.clearPreloadCids(done)) - after((done) => ipfs.stop(done)) + after(function (done) { + this.timeout(50 * 1000) + ipfs.stop(done) + }) - after((done) => repo.teardown(done)) + after(function (done) { + this.timeout(50 * 1000) + repo.teardown(done) + }) it('should not preload if disabled', (done) => { ipfs.add(Buffer.from(hat()), (err, res) => { diff --git a/test/http-api/interface.js b/test/http-api/interface.js index 3ff76c65c8..c72a6529b5 100644 --- a/test/http-api/interface.js +++ b/test/http-api/interface.js @@ -59,7 +59,18 @@ describe('interface-ipfs-core over ipfs-http-client tests', () => { tests.key(CommonFactory.create({ spawnOptions: { args: ['--pass ipfs-is-awesome-software'], - initOptions: { bits: 512 } + initOptions: { bits: 512 }, + config: { + Bootstrap: [], + Discovery: { + MDNS: { + Enabled: false + }, + webRTCStar: { + Enabled: false + } + } + } } })) @@ -119,6 +130,18 @@ describe('interface-ipfs-core over ipfs-http-client tests', () => { config = undefined } + config = config || { + Bootstrap: [], + Discovery: { + MDNS: { + Enabled: false + }, + webRTCStar: { + Enabled: false + } + } + } + const spawnOptions = { repoPath, config, initOptions: { bits: 512 } } ipfsFactory.spawn(spawnOptions, (err, _ipfsd) => {