1 // Copyright 2014 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
9 // NewRandomWriteScheduler constructs a WriteScheduler that ignores HTTP/2
10 // priorities. Control frames like SETTINGS and PING are written before DATA
11 // frames, but if no control frames are queued and multiple streams have queued
12 // HEADERS or DATA frames, Pop selects a ready stream arbitrarily.
13 func NewRandomWriteScheduler() WriteScheduler {
14 return &randomWriteScheduler{sq: make(map[uint32]*writeQueue)}
17 type randomWriteScheduler struct {
18 // zero are frames not associated with a specific stream.
21 // sq contains the stream-specific queues, keyed by stream ID.
22 // When a stream is idle or closed, it's deleted from the map.
23 sq map[uint32]*writeQueue
25 // pool of empty queues for reuse.
26 queuePool writeQueuePool
29 func (ws *randomWriteScheduler) OpenStream(streamID uint32, options OpenStreamOptions) {
30 // no-op: idle streams are not tracked
33 func (ws *randomWriteScheduler) CloseStream(streamID uint32) {
34 q, ok := ws.sq[streamID]
38 delete(ws.sq, streamID)
42 func (ws *randomWriteScheduler) AdjustStream(streamID uint32, priority PriorityParam) {
43 // no-op: priorities are ignored
46 func (ws *randomWriteScheduler) Push(wr FrameWriteRequest) {
54 q = ws.queuePool.get()
60 func (ws *randomWriteScheduler) Pop() (FrameWriteRequest, bool) {
61 // Control frames first.
63 return ws.zero.shift(), true
65 // Iterate over all non-idle streams until finding one that can be consumed.
66 for _, q := range ws.sq {
67 if wr, ok := q.consume(math.MaxInt32); ok {
71 return FrameWriteRequest{}, false