blob: 9a7b9e581c1efbb500f17e3ed957d13bf3a2b294 [file] [log] [blame]
khenaidooac637102019-01-14 15:44:34 -05001// 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.
4
5package http2
6
7import "math"
8
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.
13func NewRandomWriteScheduler() WriteScheduler {
14 return &randomWriteScheduler{sq: make(map[uint32]*writeQueue)}
15}
16
17type randomWriteScheduler struct {
18 // zero are frames not associated with a specific stream.
19 zero writeQueue
20
21 // sq contains the stream-specific queues, keyed by stream ID.
Andrea Campanella3614a922021-02-25 12:40:42 +010022 // When a stream is idle, closed, or emptied, it's deleted
23 // from the map.
khenaidooac637102019-01-14 15:44:34 -050024 sq map[uint32]*writeQueue
25
26 // pool of empty queues for reuse.
27 queuePool writeQueuePool
28}
29
30func (ws *randomWriteScheduler) OpenStream(streamID uint32, options OpenStreamOptions) {
31 // no-op: idle streams are not tracked
32}
33
34func (ws *randomWriteScheduler) CloseStream(streamID uint32) {
35 q, ok := ws.sq[streamID]
36 if !ok {
37 return
38 }
39 delete(ws.sq, streamID)
40 ws.queuePool.put(q)
41}
42
43func (ws *randomWriteScheduler) AdjustStream(streamID uint32, priority PriorityParam) {
44 // no-op: priorities are ignored
45}
46
47func (ws *randomWriteScheduler) Push(wr FrameWriteRequest) {
48 id := wr.StreamID()
49 if id == 0 {
50 ws.zero.push(wr)
51 return
52 }
53 q, ok := ws.sq[id]
54 if !ok {
55 q = ws.queuePool.get()
56 ws.sq[id] = q
57 }
58 q.push(wr)
59}
60
61func (ws *randomWriteScheduler) Pop() (FrameWriteRequest, bool) {
62 // Control frames first.
63 if !ws.zero.empty() {
64 return ws.zero.shift(), true
65 }
66 // Iterate over all non-idle streams until finding one that can be consumed.
Andrea Campanella3614a922021-02-25 12:40:42 +010067 for streamID, q := range ws.sq {
khenaidooac637102019-01-14 15:44:34 -050068 if wr, ok := q.consume(math.MaxInt32); ok {
Andrea Campanella3614a922021-02-25 12:40:42 +010069 if q.empty() {
70 delete(ws.sq, streamID)
71 ws.queuePool.put(q)
72 }
khenaidooac637102019-01-14 15:44:34 -050073 return wr, true
74 }
75 }
76 return FrameWriteRequest{}, false
77}