Wed, 31 Dec 2014 06:55:46 +0100
Added tag TORBROWSER_REPLICA for changeset 6474c204b198
michael@0 | 1 | var scheduler = exports; |
michael@0 | 2 | |
michael@0 | 3 | // |
michael@0 | 4 | // ### function Scheduler (connection) |
michael@0 | 5 | // #### @connection {spdy.Connection} active connection |
michael@0 | 6 | // Connection's streams scheduler |
michael@0 | 7 | // |
michael@0 | 8 | function Scheduler(connection) { |
michael@0 | 9 | this.connection = connection; |
michael@0 | 10 | this.priorities = [[], [], [], [], [], [], [], []]; |
michael@0 | 11 | this._tickListener = null; |
michael@0 | 12 | } |
michael@0 | 13 | |
michael@0 | 14 | // |
michael@0 | 15 | // ### function create (connection) |
michael@0 | 16 | // #### @connection {spdy.Connection} active connection |
michael@0 | 17 | // |
michael@0 | 18 | exports.create = function create(connection) { |
michael@0 | 19 | return new Scheduler(connection); |
michael@0 | 20 | }; |
michael@0 | 21 | |
michael@0 | 22 | // |
michael@0 | 23 | // ### function schedule (stream, data) |
michael@0 | 24 | // #### @stream {spdy.Stream} Source stream |
michael@0 | 25 | // #### @data {Buffer} data to write on tick |
michael@0 | 26 | // Use stream priority to invoke callbacks in right order |
michael@0 | 27 | // |
michael@0 | 28 | Scheduler.prototype.schedule = function schedule(stream, data) { |
michael@0 | 29 | this.priorities[stream.priority].push(data); |
michael@0 | 30 | }; |
michael@0 | 31 | |
michael@0 | 32 | // |
michael@0 | 33 | // ### function tick () |
michael@0 | 34 | // Add .nextTick callback if not already present |
michael@0 | 35 | // |
michael@0 | 36 | Scheduler.prototype.tick = function tick() { |
michael@0 | 37 | if (this._tickListener !== null) return; |
michael@0 | 38 | var self = this; |
michael@0 | 39 | this._tickListener = function() { |
michael@0 | 40 | var priorities = self.priorities; |
michael@0 | 41 | |
michael@0 | 42 | self._tickListener = null; |
michael@0 | 43 | self.priorities = [[], [], [], [], [], [], [], []]; |
michael@0 | 44 | |
michael@0 | 45 | // Run all priorities |
michael@0 | 46 | for (var i = 0; i < 8; i++) { |
michael@0 | 47 | for (var j = 0; j < priorities[i].length; j++) { |
michael@0 | 48 | self.connection.write( |
michael@0 | 49 | priorities[i][j] |
michael@0 | 50 | ); |
michael@0 | 51 | } |
michael@0 | 52 | } |
michael@0 | 53 | }; |
michael@0 | 54 | |
michael@0 | 55 | if (this.connection.parser.drained) { |
michael@0 | 56 | process.nextTick(this._tickListener); |
michael@0 | 57 | } else { |
michael@0 | 58 | this.connection.parser.once('drain', this._tickListener); |
michael@0 | 59 | } |
michael@0 | 60 | }; |