testing/xpcshell/node-spdy/lib/spdy/scheduler.js

Wed, 31 Dec 2014 06:55:46 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:46 +0100
changeset 1
ca08bd8f51b2
permissions
-rw-r--r--

Added tag TORBROWSER_REPLICA for changeset 6474c204b198

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

mercurial