Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 163 #525

Closed
wants to merge 7 commits into from
Closed

Issue 163 #525

Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -316,6 +316,14 @@ through the `client.torrents` array. Returns `null` if no matching torrent found

Seed ratio for all torrents in the client.

#### `client.throttleDownload(Bps)`

Throttle global download limit for all torrents in bytes per second.

#### `client.throttleUpload(Bps)`

Throttle global upload limit for all torrents in bytes per second.


### torrent api

@@ -13,6 +13,7 @@ var parseTorrent = require('parse-torrent')
var speedometer = require('speedometer')
var zeroFill = require('zero-fill')
var path = require('path')
var ThrottleGroup = require('stream-throttle').ThrottleGroup

var Torrent = require('./lib/torrent')

@@ -64,6 +65,11 @@ function WebTorrent (opts) {
self.downloadSpeed = speedometer()
self.uploadSpeed = speedometer()

self.throttleGroups = {
down: new ThrottleGroup({rate: Number.MAX_VALUE, chunksize: 512}),
up: new ThrottleGroup({rate: Number.MAX_VALUE, chunksize: 512})
}

self.peerId = typeof opts.peerId === 'string'
? opts.peerId
: (opts.peerId || new Buffer(VERSION_PREFIX + hat(48))).toString('hex')
@@ -292,3 +298,36 @@ WebTorrent.prototype.destroy = function (cb) {

parallel(tasks, cb)
}

/**
* Set global download throttle in Bps
* @param {Number} rate
*/
WebTorrent.prototype.throttleDownload = function (rate) {
var self = this
if (!Number(rate)) return
self.downloadThrottleRate = rate
self.throttleGroups.down = new ThrottleGroup({rate: rate})
self.torrents.forEach(function (torrent) {
if (torrent.swarm && torrent.swarm.updateDownloadThrottle) {
torrent.swarm.updateDownloadThrottle()
}
})
}

/**
* Set global upload throttle in Bps
* @param {Number} rate
*/
WebTorrent.prototype.throttleUpload = function (rate) {
var self = this
if (!Number(rate)) return
self.uploadThrottleRate = rate
self.throttleGroups.up = new ThrottleGroup({rate: rate})
self.torrents.forEach(function (torrent) {
if (torrent.swarm && torrent.swarm.updateUploadThrottle) {
torrent.swarm.updateUploadThrottle()
}
})
}

@@ -201,7 +201,8 @@ Torrent.prototype._onParsedTorrent = function (parsedTorrent) {
self.swarm = new Swarm(self.infoHash, self.client.peerId, {
handshake: {
dht: self.private ? false : !!self.client.dht
}
},
throttleGroups: self.client.throttleGroups
})
self.swarm.on('error', self._onError.bind(self))
self.swarm.on('wire', self._onWire.bind(self))
@@ -1,7 +1,7 @@
{
"name": "webtorrent",
"description": "Streaming torrent client",
"version": "0.63.3",
"version": "0.63.4",
"author": {
"name": "Feross Aboukhadijeh",
"email": "feross@feross.org",
@@ -57,6 +57,7 @@
"run-parallel": "^1.0.0",
"simple-sha1": "^2.0.0",
"speedometer": "^1.0.0",
"stream-throttle": "^0.1.3",
"thunky": "^0.1.0",
"torrent-discovery": "^4.0.0",
"torrent-piece": "^1.0.0",
@@ -0,0 +1,34 @@
var test = require('tape')
var WebTorrent = require('../')

test('client download/upload throttle setting and rate reporting', function (t) {
var client = new WebTorrent({ dht: false, tracker: false })

client.on('error', function (err) { t.fail(err) })
client.on('warning', function (err) { t.fail(err) })

client.throttleDownload(100000)
client.throttleUpload(10000)

t.equal(client.downloadThrottleRate, 100000)
t.equal(client.uploadThrottleRate, 10000)

client.destroy()
t.end()
})

test('client download/upload throttle setting bad values', function (t) {
var client = new WebTorrent({ dht: false, tracker: false })

client.on('error', function (err) { t.fail(err) })
client.on('warning', function (err) { t.fail(err) })

client.throttleDownload("I'm a string")
client.throttleUpload(NaN)

t.equal(client.downloadThrottleRate, undefined)
t.equal(client.uploadThrottleRate, undefined)

client.destroy()
t.end()
})

Large diffs are not rendered by default.

ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.