writesched.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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.
  4. package http2
  5. import "fmt"
  6. // WriteScheduler is the interface implemented by HTTP/2 write schedulers.
  7. // Methods are never called concurrently.
  8. type WriteScheduler interface {
  9. // OpenStream opens a new stream in the write scheduler.
  10. // It is illegal to call this with streamID=0 or with a streamID that is
  11. // already open -- the call may panic.
  12. OpenStream(streamID uint32, options OpenStreamOptions)
  13. // CloseStream closes a stream in the write scheduler. Any frames queued on
  14. // this stream should be discarded. It is illegal to call this on a stream
  15. // that is not open -- the call may panic.
  16. CloseStream(streamID uint32)
  17. // AdjustStream adjusts the priority of the given stream. This may be called
  18. // on a stream that has not yet been opened or has been closed. Note that
  19. // RFC 7540 allows PRIORITY frames to be sent on streams in any state. See:
  20. // https://tools.ietf.org/html/rfc7540#section-5.1
  21. AdjustStream(streamID uint32, priority PriorityParam)
  22. // Push queues a frame in the scheduler.
  23. Push(wr FrameWriteRequest)
  24. // Pop dequeues the next frame to write. Returns false if no frames can
  25. // be written. Frames with a given wr.StreamID() are Pop'd in the same
  26. // order they are Push'd.
  27. Pop() (wr FrameWriteRequest, ok bool)
  28. }
  29. // OpenStreamOptions specifies extra options for WriteScheduler.OpenStream.
  30. type OpenStreamOptions struct {
  31. // PusherID is zero if the stream was initiated by the client. Otherwise,
  32. // PusherID names the stream that pushed the newly opened stream.
  33. PusherID uint32
  34. }
  35. // FrameWriteRequest is a request to write a frame.
  36. type FrameWriteRequest struct {
  37. // write is the interface value that does the writing, once the
  38. // WriteScheduler has selected this frame to write. The write
  39. // functions are all defined in write.go.
  40. write writeFramer
  41. // stream is the stream on which this frame will be written.
  42. // nil for non-stream frames like PING and SETTINGS.
  43. stream *stream
  44. // done, if non-nil, must be a buffered channel with space for
  45. // 1 message and is sent the return value from write (or an
  46. // earlier error) when the frame has been written.
  47. done chan error
  48. }
  49. // StreamID returns the id of the stream this frame will be written to.
  50. // 0 is used for non-stream frames such as PING and SETTINGS.
  51. func (wr FrameWriteRequest) StreamID() uint32 {
  52. if wr.stream == nil {
  53. return 0
  54. }
  55. return wr.stream.id
  56. }
  57. // DataSize returns the number of flow control bytes that must be consumed
  58. // to write this entire frame. This is 0 for non-DATA frames.
  59. func (wr FrameWriteRequest) DataSize() int {
  60. if wd, ok := wr.write.(*writeData); ok {
  61. return len(wd.p)
  62. }
  63. return 0
  64. }
  65. // Consume consumes min(n, available) bytes from this frame, where available
  66. // is the number of flow control bytes available on the stream. Consume returns
  67. // 0, 1, or 2 frames, where the integer return value gives the number of frames
  68. // returned.
  69. //
  70. // If flow control prevents consuming any bytes, this returns (_, _, 0). If
  71. // the entire frame was consumed, this returns (wr, _, 1). Otherwise, this
  72. // returns (consumed, rest, 2), where 'consumed' contains the consumed bytes and
  73. // 'rest' contains the remaining bytes. The consumed bytes are deducted from the
  74. // underlying stream's flow control budget.
  75. func (wr FrameWriteRequest) Consume(n int32) (FrameWriteRequest, FrameWriteRequest, int) {
  76. var empty FrameWriteRequest
  77. // Non-DATA frames are always consumed whole.
  78. wd, ok := wr.write.(*writeData)
  79. if !ok || len(wd.p) == 0 {
  80. return wr, empty, 1
  81. }
  82. // Might need to split after applying limits.
  83. allowed := wr.stream.flow.available()
  84. if n < allowed {
  85. allowed = n
  86. }
  87. if wr.stream.sc.maxFrameSize < allowed {
  88. allowed = wr.stream.sc.maxFrameSize
  89. }
  90. if allowed <= 0 {
  91. return empty, empty, 0
  92. }
  93. if len(wd.p) > int(allowed) {
  94. wr.stream.flow.take(allowed)
  95. consumed := FrameWriteRequest{
  96. stream: wr.stream,
  97. write: &writeData{
  98. streamID: wd.streamID,
  99. p: wd.p[:allowed],
  100. // Even if the original had endStream set, there
  101. // are bytes remaining because len(wd.p) > allowed,
  102. // so we know endStream is false.
  103. endStream: false,
  104. },
  105. // Our caller is blocking on the final DATA frame, not
  106. // this intermediate frame, so no need to wait.
  107. done: nil,
  108. }
  109. rest := FrameWriteRequest{
  110. stream: wr.stream,
  111. write: &writeData{
  112. streamID: wd.streamID,
  113. p: wd.p[allowed:],
  114. endStream: wd.endStream,
  115. },
  116. done: wr.done,
  117. }
  118. return consumed, rest, 2
  119. }
  120. // The frame is consumed whole.
  121. // NB: This cast cannot overflow because allowed is <= math.MaxInt32.
  122. wr.stream.flow.take(int32(len(wd.p)))
  123. return wr, empty, 1
  124. }
  125. // String is for debugging only.
  126. func (wr FrameWriteRequest) String() string {
  127. var streamID uint32
  128. if wr.stream != nil {
  129. streamID = wr.stream.id
  130. }
  131. var des string
  132. if s, ok := wr.write.(fmt.Stringer); ok {
  133. des = s.String()
  134. } else {
  135. des = fmt.Sprintf("%T", wr.write)
  136. }
  137. return fmt.Sprintf("[FrameWriteRequest stream=%d, ch=%v, writer=%v]", streamID, wr.done != nil, des)
  138. }
  139. // writeQueue is used by implementations of WriteScheduler.
  140. type writeQueue struct {
  141. s []FrameWriteRequest
  142. }
  143. func (q *writeQueue) empty() bool { return len(q.s) == 0 }
  144. func (q *writeQueue) push(wr FrameWriteRequest) {
  145. q.s = append(q.s, wr)
  146. }
  147. func (q *writeQueue) shift() FrameWriteRequest {
  148. if len(q.s) == 0 {
  149. panic("invalid use of queue")
  150. }
  151. wr := q.s[0]
  152. // TODO: less copy-happy queue.
  153. copy(q.s, q.s[1:])
  154. q.s[len(q.s)-1] = FrameWriteRequest{}
  155. q.s = q.s[:len(q.s)-1]
  156. return wr
  157. }
  158. // consume consumes up to n bytes from q.s[0]. If the frame is
  159. // entirely consumed, it is removed from the queue. If the frame
  160. // is partially consumed, the frame is kept with the consumed
  161. // bytes removed. Returns true iff any bytes were consumed.
  162. func (q *writeQueue) consume(n int32) (FrameWriteRequest, bool) {
  163. if len(q.s) == 0 {
  164. return FrameWriteRequest{}, false
  165. }
  166. consumed, rest, numresult := q.s[0].Consume(n)
  167. switch numresult {
  168. case 0:
  169. return FrameWriteRequest{}, false
  170. case 1:
  171. q.shift()
  172. case 2:
  173. q.s[0] = rest
  174. }
  175. return consumed, true
  176. }
  177. type writeQueuePool []*writeQueue
  178. // put inserts an unused writeQueue into the pool.
  179. func (p *writeQueuePool) put(q *writeQueue) {
  180. for i := range q.s {
  181. q.s[i] = FrameWriteRequest{}
  182. }
  183. q.s = q.s[:0]
  184. *p = append(*p, q)
  185. }
  186. // get returns an empty writeQueue.
  187. func (p *writeQueuePool) get() *writeQueue {
  188. ln := len(*p)
  189. if ln == 0 {
  190. return new(writeQueue)
  191. }
  192. x := ln - 1
  193. q := (*p)[x]
  194. (*p)[x] = nil
  195. *p = (*p)[:x]
  196. return q
  197. }