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

tests and code styling

  • Loading branch information
Aram Drevekenin
Aram Drevekenin committed Dec 12, 2015
commit c3f280aa5a777a5c3a24f9a8c8b068828300603b
@@ -13,7 +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 ThrottleGroup = require('stream-throttle').ThrottleGroup

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

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

self.throttleGroups = {
self.throttleGroups = {
down: new ThrottleGroup({rate: Number.MAX_VALUE, chunksize: 512}),
up: new ThrottleGroup({rate: Number.MAX_VALUE, chunksize: 512})
}
@@ -303,31 +303,31 @@ WebTorrent.prototype.destroy = function (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()
}
})
}
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()
}
})
}
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()
}
})
}

@@ -0,0 +1,18 @@
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()
})
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.