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 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

Functionality

  • Loading branch information
Aram Drevekenin
Aram Drevekenin committed Dec 12, 2015
commit 3454d6a62287ccd69c1dae8ec8cb56b6e1a0ca0b
@@ -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) {
if (!Number(rate)) return
var self = this
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))
@@ -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",
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.