Revert "client: socketPool should not be shared across clients"

This reverts commit 3f3db7deb1.
This commit is contained in:
Feross Aboukhadijeh 2017-02-28 15:41:52 -08:00
parent 78722e0a05
commit 1dd1cc32da
2 changed files with 13 additions and 13 deletions

View File

@ -69,11 +69,6 @@ function Client (opts) {
// See: https://github.com/feross/webtorrent-hybrid/issues/46 // See: https://github.com/feross/webtorrent-hybrid/issues/46
self._wrtc = typeof opts.wrtc === 'function' ? opts.wrtc() : opts.wrtc self._wrtc = typeof opts.wrtc === 'function' ? opts.wrtc() : opts.wrtc
// Use a socket pool, so WebSocket tracker clients share WebSocket objects for
// the same server. In practice, WebSockets are pretty slow to establish, so this
// gives a nice performance boost, and saves browser resources.
self._socketPool = {}
var announce = typeof opts.announce === 'string' var announce = typeof opts.announce === 'string'
? [ opts.announce ] ? [ opts.announce ]
: opts.announce == null ? [] : opts.announce : opts.announce == null ? [] : opts.announce

View File

@ -10,6 +10,11 @@ var Socket = require('simple-websocket')
var common = require('../common') var common = require('../common')
var Tracker = require('./tracker') var Tracker = require('./tracker')
// Use a socket pool, so tracker clients share WebSocket objects for the same server.
// In practice, WebSockets are pretty slow to establish, so this gives a nice performance
// boost, and saves browser resources.
var socketPool = {}
var RECONNECT_MINIMUM = 15 * 1000 var RECONNECT_MINIMUM = 15 * 1000
var RECONNECT_MAXIMUM = 30 * 60 * 1000 var RECONNECT_MAXIMUM = 30 * 60 * 1000
var RECONNECT_VARIANCE = 30 * 1000 var RECONNECT_VARIANCE = 30 * 1000
@ -124,15 +129,15 @@ WebSocketTracker.prototype.destroy = function (cb) {
self._onSocketDataBound = null self._onSocketDataBound = null
self._onSocketCloseBound = null self._onSocketCloseBound = null
if (self.client._socketPool[self.announceUrl]) { if (socketPool[self.announceUrl]) {
self.client._socketPool[self.announceUrl].consumers -= 1 socketPool[self.announceUrl].consumers -= 1
} }
// Other instances are using the socket, so there's nothing left to do here // Other instances are using the socket, so there's nothing left to do here
if (self.client._socketPool[self.announceUrl].consumers > 0) return cb() if (socketPool[self.announceUrl].consumers > 0) return cb()
var socket = self.client._socketPool[self.announceUrl] var socket = socketPool[self.announceUrl]
delete self.client._socketPool[self.announceUrl] delete socketPool[self.announceUrl]
socket.on('error', noop) // ignore all future errors socket.on('error', noop) // ignore all future errors
socket.once('close', cb) socket.once('close', cb)
@ -177,11 +182,11 @@ WebSocketTracker.prototype._openSocket = function () {
self._onSocketClose() self._onSocketClose()
} }
self.socket = self.client._socketPool[self.announceUrl] self.socket = socketPool[self.announceUrl]
if (self.socket) { if (self.socket) {
self.client._socketPool[self.announceUrl].consumers += 1 socketPool[self.announceUrl].consumers += 1
} else { } else {
self.socket = self.client._socketPool[self.announceUrl] = new Socket(self.announceUrl) self.socket = socketPool[self.announceUrl] = new Socket(self.announceUrl)
self.socket.consumers = 1 self.socket.consumers = 1
self.socket.once('connect', self._onSocketConnectBound) self.socket.once('connect', self._onSocketConnectBound)
} }