|
1 var scheduler = exports; |
|
2 |
|
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 } |
|
13 |
|
14 // |
|
15 // ### function create (connection) |
|
16 // #### @connection {spdy.Connection} active connection |
|
17 // |
|
18 exports.create = function create(connection) { |
|
19 return new Scheduler(connection); |
|
20 }; |
|
21 |
|
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 }; |
|
31 |
|
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; |
|
41 |
|
42 self._tickListener = null; |
|
43 self.priorities = [[], [], [], [], [], [], [], []]; |
|
44 |
|
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 }; |
|
54 |
|
55 if (this.connection.parser.drained) { |
|
56 process.nextTick(this._tickListener); |
|
57 } else { |
|
58 this.connection.parser.once('drain', this._tickListener); |
|
59 } |
|
60 }; |