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
diff options
context:
space:
mode:
authorAlex <alxmorais8@msn.com>2021-07-24 01:05:25 +0300
committerGitHub <noreply@github.com>2021-07-24 01:05:25 +0300
commit39bb33c3cf694cdee45378ea4b30c66c93576d2a (patch)
tree654d454bbcc6bba1deab2195ccf6bcbc2bbe4f60 /index.js
parent524618edde211a2ce2c9d1e40f68a091699442fd (diff)
feat: add speed limit to client (#2062)
* Add speed limit to client * Fix standard * Update docs/api.md * Add changes from PR feedback Co-authored-by: Kadu Diógenes <kadu@fnix.com.br> Co-authored-by: Ivan Gorbanev <ivang@van.work> Co-authored-by: ultimate-tester <jordimueters@hotmail.com> Co-authored-by: Julen Garcia Leunda <hicom150@gmail.com> Co-authored-by: Niklas Johansson <niklas.y.johansson@se.abb.com> Co-authored-by: ThaUnknown <kapi.skowronek@gmail.com> Co-authored-by: Diego Rodríguez Baquero <github@diegorbaquero.com>
Diffstat (limited to 'index.js')
-rw-r--r--index.js40
1 files changed, 39 insertions, 1 deletions
diff --git a/index.js b/index.js
index e4a315b..e971298 100644
--- a/index.js
+++ b/index.js
@@ -11,9 +11,10 @@ const parallel = require('run-parallel')
const parseTorrent = require('parse-torrent')
const path = require('path')
const Peer = require('simple-peer')
+const queueMicrotask = require('queue-microtask')
const randombytes = require('randombytes')
const speedometer = require('speedometer')
-const queueMicrotask = require('queue-microtask')
+const { ThrottleGroup } = require('speed-limiter')
const ConnPool = require('./lib/conn-pool') // browser exclude
const Torrent = require('./lib/torrent')
@@ -76,11 +77,19 @@ class WebTorrent extends EventEmitter {
this.maxConns = Number(opts.maxConns) || 55
this.utp = WebTorrent.UTP_SUPPORT && opts.utp !== false
+ this._downloadLimit = Math.max(Number(opts.downloadLimit) || -1, -1)
+ this._uploadLimit = Math.max(Number(opts.uploadLimit) || -1, -1)
+
this._debug(
'new webtorrent (peerId %s, nodeId %s, port %s)',
this.peerId, this.nodeId, this.torrentPort
)
+ this.throttleGroups = {
+ down: new ThrottleGroup({ rate: Math.max(this._downloadLimit, 0), enabled: this._downloadLimit >= 0 }),
+ up: new ThrottleGroup({ rate: Math.max(this._uploadLimit, 0), enabled: this._uploadLimit >= 0 })
+ }
+
if (this.tracker) {
if (typeof this.tracker !== 'object') this.tracker = {}
if (global.WRTC && !this.tracker.wrtc) this.tracker.wrtc = global.WRTC
@@ -353,6 +362,32 @@ class WebTorrent extends EventEmitter {
}
/**
+ * Set global download throttle rate.
+ * @param {Number} rate (must be bigger or equal than zero, or -1 to disable throttling)
+ */
+ throttleDownload (rate) {
+ rate = Number(rate)
+ if (isNaN(rate) || !isFinite(rate) || rate < -1) return false
+ this._downloadLimit = rate
+ if (this._downloadLimit < 0) return this.throttleGroups.down.setEnabled(false)
+ this.throttleGroups.down.setEnabled(true)
+ this.throttleGroups.down.setRate(this._downloadLimit)
+ }
+
+ /**
+ * Set global upload throttle rate
+ * @param {Number} rate (must be bigger or equal than zero, or -1 to disable throttling)
+ */
+ throttleUpload (rate) {
+ rate = Number(rate)
+ if (isNaN(rate) || !isFinite(rate) || rate < -1) return false
+ this._uploadLimit = rate
+ if (this._uploadLimit < 0) return this.throttleGroups.up.setEnabled(false)
+ this.throttleGroups.up.setEnabled(true)
+ this.throttleGroups.up.setRate(this._uploadLimit)
+ }
+
+ /**
* Destroy the client, including all torrents and connections to peers.
* @param {function} cb
*/
@@ -388,6 +423,9 @@ class WebTorrent extends EventEmitter {
this.torrents = []
this._connPool = null
this.dht = null
+
+ this.throttleGroups.down.destroy()
+ this.throttleGroups.up.destroy()
}
_onListening () {