dat-node icon indicating copy to clipboard operation
dat-node copied to clipboard

pause + resume

Open joehand opened this issue 7 years ago • 4 comments

Add dat.pause() and dat.resume() to stop replication without leaving network (join/leave).

It seems like either archive.unreplicate() or archive.unprioritize() are the best options. Unreplicate will be more difficult to resume. Unprioritize may be buggy if anything was manually prioritized before.

Basic implementation with prioritization:

dat.pause = function () {
  archive.metadata.unprioritize({start: 0, end: Infinity, priority: 0})
  archive.content.unprioritize({start: 0, end: Infinity, priority: 0})
}

dat.resume = function () {
  archive.metadata.prioritize({start: 0, end: Infinity, priority: 0})
  archive.content.prioritize({start: 0, end: Infinity, priority: 0})
}

Test for this:

test('Download pause', function (t) {
  Dat(downloadDir, {key: shareKey}, function (err, dat) {
    t.error(err, 'no download init error')

    var paused = false

    var stats = dat.trackStats()
    stats.once('update:blocksProgress', function () {
      dat.pause()
      paused = true
      setTimeout(function () {
        paused = false
        dat.resume()
      }, 300)
    })

    dat.archive.open(function () {
      dat.archive.content.once('download-finished', function () {
        if (paused) t.fail('finished while paused')
        t.same(dat.archive.content.blocks, stats.get().blocksTotal, 'has total blocks')
        t.end()
      })
    })

    dat.joinNetwork()
  })
})

joehand avatar Jan 06 '17 01:01 joehand

will it be possible to pause download and still share data for upload ?

what I have in mind:

  • there is 1000 subscribers to live data source
  • after whole archive is downloaded subscriber starts long lasting import process
  • the network distribution will be rapidly affected if each subscriber starting data import will pause also upload of its data - probably soon everybody will download only directly from original source

k2s avatar Jan 06 '17 08:01 k2s

will it be possible to pause download and still share data for upload ?

Ah, good point! The method I wrote above, using archive.unprioritize(), will work with stopping downloads but not uploads. So we should implement it that way! The other ways (leaving the network or stopping replication would stop all activity).

Archive prioritization changes what is being downloaded but as long as you are uploading, you'll continue sharing whatever blocks you have.

joehand avatar Jan 06 '17 16:01 joehand

Implemented a first version which is a proxy for join and leave.

I had some trouble with unreplicate or unprioritize because of hyperdrive/swarm API issues. I will revisit pause/resume when SLEEP lands and we refactor to use that.

joehand avatar Mar 07 '17 19:03 joehand

Mafintosh said this is something he wants in hyperdrive, so I'll probably leave it as an alias for join/leave until implemented in hd.

joehand avatar May 11 '17 22:05 joehand