Skip to content

Commit

Permalink
feat: Use a cache on the chunk store (#2095)
Browse files Browse the repository at this point in the history
  • Loading branch information
jhiesey committed Jun 30, 2021
1 parent 421d6c9 commit d540058
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
3 changes: 2 additions & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 94,7 @@ If `opts` is specified, then the default options (shown below) will be overridde
private: Boolean, // If true, client will not share the hash with the DHT nor with PEX (default is the privacy of the parsed torrent)
store: Function // Custom chunk store (must follow [abstract-chunk-store](https://www.npmjs.com/package/abstract-chunk-store) API)
destroyStoreOnDestroy: Boolean // If truthy, client will delete the torrent's chunk store (e.g. files on disk) when the torrent is destroyed
storeCacheSlots: Number // Number of chunk store entries (torrent pieces) to cache in memory [default=20]; 0 to disable caching
}
```

Expand All @@ -110,7 111,7 @@ If you provide `opts.store`, it will be called as

* `storeOpts.length` - size of all the files in the torrent
* `storeOpts.files` - an array of torrent file objects
* `storeOpts.torrent` - the torrent instance being stored
* `storeOpts.name` - the info hash of the torrent instance being stored

**Note:** Downloading a torrent automatically seeds it, making it available for download by other peers.

Expand Down
21 changes: 19 additions & 2 deletions lib/torrent.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 2,7 @@

const addrToIPPort = require('addr-to-ip-port')
const BitField = require('bitfield').default
const CacheChunkStore = require('cache-chunk-store')
const ChunkStoreWriteStream = require('chunk-store-stream/write')
const cpus = require('cpus')
const debug = require('debug')('webtorrent:torrent')
Expand All @@ -12,6 13,7 @@ const FSChunkStore = require('fs-chunk-store') // browser: `memory-chunk-store`
const get = require('simple-get')
const ImmediateChunkStore = require('immediate-chunk-store')
const ltDontHave = require('lt_donthave')
const MemoryChunkStore = require('memory-chunk-store')
const MultiStream = require('multistream')
const net = require('net') // browser exclude
const os = require('os') // browser exclude
Expand Down Expand Up @@ -75,6 77,7 @@ class Torrent extends EventEmitter {
this.skipVerify = !!opts.skipVerify
this._store = opts.store || FSChunkStore
this._preloadedStore = opts.preloadedStore || null
this._storeCacheSlots = opts.storeCacheSlots !== undefined ? opts.storeCacheSlots : 20
this._destroyStoreOnDestroy = opts.destroyStoreOnDestroy || false
this._getAnnounceOpts = opts.getAnnounceOpts

Expand Down Expand Up @@ -470,8 473,11 @@ class Torrent extends EventEmitter {

this._rarityMap = new RarityMap(this)

this.store = new ImmediateChunkStore(
this._preloadedStore || new this._store(this.pieceLength, {
let rawStore = this._preloadedStore
if (!rawStore) {
rawStore = new this._store(this.pieceLength, {
// opts.torrent is deprecated (replaced by the name property).
// it doesn't appear to be used by current versions of any stores on npm.
torrent: {
infoHash: this.infoHash
},
Expand All @@ -483,6 489,17 @@ class Torrent extends EventEmitter {
length: this.length,
name: this.infoHash
})
}

// don't use the cache if the store is already in memory
if (this._storeCacheSlots > 0 && !(rawStore instanceof MemoryChunkStore)) {
rawStore = new CacheChunkStore(rawStore, {
max: this._storeCacheSlots
})
}

this.store = new ImmediateChunkStore(
rawStore
)

this.files = this.files.map(file => new File(this, file))
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 40,7 @@
"bitfield": "^4.0.0",
"bittorrent-dht": "^10.0.0",
"bittorrent-protocol": "^3.3.1",
"cache-chunk-store": "^3.2.2",
"chrome-net": "^3.3.4",
"chunk-store-stream": "^4.3.0",
"cpus": "^1.0.3",
Expand Down

0 comments on commit d540058

Please sign in to comment.