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

Feature/163 global download upload limit #1040

Open
wants to merge 5 commits into
base: master
from
Open
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

Next

Global download and upload limit for all TCP peer connections

throttleDownload() and throttleUpload() helper methods for rate
adjustments
  • Loading branch information
chtrinh committed Feb 11, 2017
commit 3e92cbf87d061facd00b825e0e8f08528060f66c
@@ -18,6 +18,7 @@ var Peer = require('simple-peer')
var randombytes = require('randombytes')
var speedometer = require('speedometer')
var zeroFill = require('zero-fill')
var ThrottleGroup = require('stream-throttle').ThrottleGroup

var TCPPool = require('./lib/tcp-pool') // browser exclude
var Torrent = require('./lib/torrent')
@@ -87,6 +88,8 @@ function WebTorrent (opts) {
self.tracker = opts.tracker !== undefined ? opts.tracker : {}
self.torrents = []
self.maxConns = Number(opts.maxConns) || 55
self.downloadLimit = Number(opts.downloadLimit) || Number.MAX_VALUE
self.uploadLimit = Number(opts.uploadLimit) || Number.MAX_VALUE

self._debug(
'new webtorrent (peerId %s, nodeId %s, port %s)',
@@ -110,6 +113,11 @@ function WebTorrent (opts) {
}
}

self.throttleGroups = {
down: new ThrottleGroup({rate: self.downloadLimit}),
up: new ThrottleGroup({rate: self.uploadLimit})
}

if (typeof TCPPool === 'function') {
self._tcpPool = new TCPPool(self)
} else {
@@ -460,6 +468,26 @@ WebTorrent.prototype._debug = function () {
debug.apply(null, args)
}

/**
* Set global download throttle rate
* @param {Number} rate
*/
WebTorrent.prototype.throttleDownload = function (rate) {
if (!Number(rate) || Number(rate) < 0) return
self.throttleGroups.down.bucket.bucketSize = rate
self.throttleGroups.down.bucket.tokensPerInterval = rate
}

/**
* Set global upload throttle rate
* @param {Number} rate
*/
WebTorrent.prototype.throttleUpload = function (rate) {
if (!Number(rate) || Number(rate) < 0) return
self.throttleGroups.up.bucket.bucketSize = rate
self.throttleGroups.up.bucket.tokensPerInterval = rate
}

/**
* Check if `obj` is a node Readable stream
* @param {*} obj
@@ -143,7 +143,15 @@ Peer.prototype.onConnect = function () {
})
self.startHandshakeTimeout()

conn.pipe(wire).pipe(conn)
if (self.type.indexOf('tcp') !== -1) {
conn.pipe(self.swarm.client.throttleGroups.down.throttle())
.pipe(wire)
.pipe(self.swarm.client.throttleGroups.up.throttle())
.pipe(conn)
} else {
conn.pipe(wire).pipe(conn)
}

if (self.swarm && !self.sentHandshake) self.handshake()
}

@@ -57,6 +57,7 @@
"simple-peer": "^6.0.4",
"simple-sha1": "^2.0.8",
"speedometer": "^1.0.0",
"stream-throttle": "^0.1.3",
"stream-to-blob": "^1.0.0",
"stream-to-blob-url": "^2.1.0",
"stream-with-known-length-to-buffer": "^1.0.0",

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.