Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/webtorrent/webtorrent.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJohn Hiesey <john@hiesey.com>2021-06-30 23:19:18 +0300
committerGitHub <noreply@github.com>2021-06-30 23:19:18 +0300
commitd540058ebd7f32e613d26c33e8a99b16d39a13d8 (patch)
tree43b93ccb50b0c5fcd3a9379eef671a6089a76739 /lib
parent421d6c90edb197be2f8c948eb1b15d8adb8f7dae (diff)
feat: Use a cache on the chunk store (#2095)
Diffstat (limited to 'lib')
-rw-r--r--lib/torrent.js21
1 files changed, 19 insertions, 2 deletions
diff --git a/lib/torrent.js b/lib/torrent.js
index 1f0be5b..b46cde3 100644
--- a/lib/torrent.js
+++ b/lib/torrent.js
@@ -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')
@@ -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
@@ -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
@@ -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
},
@@ -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))