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
// WriteScheduler is the interface implemented by HTTP/2 write schedulers.
10
// Methods are never called concurrently.
11
type WriteScheduler interface {
12
// OpenStream opens a new stream in the write scheduler.
13
// It is illegal to call this with streamID=0 or with a streamID that is
14
// already open -- the call may panic.
15
OpenStream(streamID uint32, options OpenStreamOptions)
17
// CloseStream closes a stream in the write scheduler. Any frames queued on
18
// this stream should be discarded. It is illegal to call this on a stream
19
// that is not open -- the call may panic.
20
CloseStream(streamID uint32)
22
// AdjustStream adjusts the priority of the given stream. This may be called
23
// on a stream that has not yet been opened or has been closed. Note that
24
// RFC 7540 allows PRIORITY frames to be sent on streams in any state. See:
25
// https://tools.ietf.org/html/rfc7540#section-5.1
26
AdjustStream(streamID uint32, priority PriorityParam)
28
// Push queues a frame in the scheduler. In most cases, this will not be
29
// called with wr.StreamID()!=0 unless that stream is currently open. The one
30
// exception is RST_STREAM frames, which may be sent on idle or closed streams.
31
Push(wr FrameWriteRequest)
33
// Pop dequeues the next frame to write. Returns false if no frames can
34
// be written. Frames with a given wr.StreamID() are Pop'd in the same
35
// order they are Push'd, except RST_STREAM frames. No frames should be
36
// discarded except by CloseStream.
37
Pop() (wr FrameWriteRequest, ok bool)
40
// OpenStreamOptions specifies extra options for WriteScheduler.OpenStream.
41
type OpenStreamOptions struct {
42
// PusherID is zero if the stream was initiated by the client. Otherwise,
43
// PusherID names the stream that pushed the newly opened stream.
47
// FrameWriteRequest is a request to write a frame.
48
type FrameWriteRequest struct {
49
// write is the interface value that does the writing, once the
50
// WriteScheduler has selected this frame to write. The write
51
// functions are all defined in write.go.
54
// stream is the stream on which this frame will be written.
55
// nil for non-stream frames like PING and SETTINGS.
56
// nil for RST_STREAM streams, which use the StreamError.StreamID field instead.
59
// done, if non-nil, must be a buffered channel with space for
60
// 1 message and is sent the return value from write (or an
61
// earlier error) when the frame has been written.
65
// StreamID returns the id of the stream this frame will be written to.
66
// 0 is used for non-stream frames such as PING and SETTINGS.
67
func (wr FrameWriteRequest) StreamID() uint32 {
69
if se, ok := wr.write.(StreamError); ok {
70
// (*serverConn).resetStream doesn't set
71
// stream because it doesn't necessarily have
72
// one. So special case this type of write
81
// isControl reports whether wr is a control frame for MaxQueuedControlFrames
82
// purposes. That includes non-stream frames and RST_STREAM frames.
83
func (wr FrameWriteRequest) isControl() bool {
84
return wr.stream == nil
87
// DataSize returns the number of flow control bytes that must be consumed
88
// to write this entire frame. This is 0 for non-DATA frames.
89
func (wr FrameWriteRequest) DataSize() int {
90
if wd, ok := wr.write.(*writeData); ok {
96
// Consume consumes min(n, available) bytes from this frame, where available
97
// is the number of flow control bytes available on the stream. Consume returns
98
// 0, 1, or 2 frames, where the integer return value gives the number of frames
101
// If flow control prevents consuming any bytes, this returns (_, _, 0). If
102
// the entire frame was consumed, this returns (wr, _, 1). Otherwise, this
103
// returns (consumed, rest, 2), where 'consumed' contains the consumed bytes and
104
// 'rest' contains the remaining bytes. The consumed bytes are deducted from the
105
// underlying stream's flow control budget.
106
func (wr FrameWriteRequest) Consume(n int32) (FrameWriteRequest, FrameWriteRequest, int) {
107
var empty FrameWriteRequest
109
// Non-DATA frames are always consumed whole.
110
wd, ok := wr.write.(*writeData)
111
if !ok || len(wd.p) == 0 {
115
// Might need to split after applying limits.
116
allowed := wr.stream.flow.available()
120
if wr.stream.sc.maxFrameSize < allowed {
121
allowed = wr.stream.sc.maxFrameSize
124
return empty, empty, 0
126
if len(wd.p) > int(allowed) {
127
wr.stream.flow.take(allowed)
128
consumed := FrameWriteRequest{
131
streamID: wd.streamID,
133
// Even if the original had endStream set, there
134
// are bytes remaining because len(wd.p) > allowed,
135
// so we know endStream is false.
138
// Our caller is blocking on the final DATA frame, not
139
// this intermediate frame, so no need to wait.
142
rest := FrameWriteRequest{
145
streamID: wd.streamID,
147
endStream: wd.endStream,
151
return consumed, rest, 2
154
// The frame is consumed whole.
155
// NB: This cast cannot overflow because allowed is <= math.MaxInt32.
156
wr.stream.flow.take(int32(len(wd.p)))
160
// String is for debugging only.
161
func (wr FrameWriteRequest) String() string {
163
if s, ok := wr.write.(fmt.Stringer); ok {
166
des = fmt.Sprintf("%T", wr.write)
168
return fmt.Sprintf("[FrameWriteRequest stream=%d, ch=%v, writer=%v]", wr.StreamID(), wr.done != nil, des)
171
// replyToWriter sends err to wr.done and panics if the send must block
172
// This does nothing if wr.done is nil.
173
func (wr *FrameWriteRequest) replyToWriter(err error) {
180
panic(fmt.Sprintf("unbuffered done channel passed in for type %T", wr.write))
182
wr.write = nil // prevent use (assume it's tainted after wr.done send)
185
// writeQueue is used by implementations of WriteScheduler.
186
type writeQueue struct {
187
s []FrameWriteRequest
190
func (q *writeQueue) empty() bool { return len(q.s) == 0 }
192
func (q *writeQueue) push(wr FrameWriteRequest) {
193
q.s = append(q.s, wr)
196
func (q *writeQueue) shift() FrameWriteRequest {
198
panic("invalid use of queue")
201
// TODO: less copy-happy queue.
203
q.s[len(q.s)-1] = FrameWriteRequest{}
204
q.s = q.s[:len(q.s)-1]
208
// consume consumes up to n bytes from q.s[0]. If the frame is
209
// entirely consumed, it is removed from the queue. If the frame
210
// is partially consumed, the frame is kept with the consumed
211
// bytes removed. Returns true iff any bytes were consumed.
212
func (q *writeQueue) consume(n int32) (FrameWriteRequest, bool) {
214
return FrameWriteRequest{}, false
216
consumed, rest, numresult := q.s[0].Consume(n)
219
return FrameWriteRequest{}, false
225
return consumed, true
228
type writeQueuePool []*writeQueue
230
// put inserts an unused writeQueue into the pool.
231
func (p *writeQueuePool) put(q *writeQueue) {
233
q.s[i] = FrameWriteRequest{}
239
// get returns an empty writeQueue.
240
func (p *writeQueuePool) get() *writeQueue {
243
return new(writeQueue)