You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
1.9 KiB

  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 "math"
  6. // NewRandomWriteScheduler constructs a WriteScheduler that ignores HTTP/2
  7. // priorities. Control frames like SETTINGS and PING are written before DATA
  8. // frames, but if no control frames are queued and multiple streams have queued
  9. // HEADERS or DATA frames, Pop selects a ready stream arbitrarily.
  10. func NewRandomWriteScheduler() WriteScheduler {
  11. return &randomWriteScheduler{sq: make(map[uint32]*writeQueue)}
  12. }
  13. type randomWriteScheduler struct {
  14. // zero are frames not associated with a specific stream.
  15. zero writeQueue
  16. // sq contains the stream-specific queues, keyed by stream ID.
  17. // When a stream is idle or closed, it's deleted from the map.
  18. sq map[uint32]*writeQueue
  19. // pool of empty queues for reuse.
  20. queuePool writeQueuePool
  21. }
  22. func (ws *randomWriteScheduler) OpenStream(streamID uint32, options OpenStreamOptions) {
  23. // no-op: idle streams are not tracked
  24. }
  25. func (ws *randomWriteScheduler) CloseStream(streamID uint32) {
  26. q, ok := ws.sq[streamID]
  27. if !ok {
  28. return
  29. }
  30. delete(ws.sq, streamID)
  31. ws.queuePool.put(q)
  32. }
  33. func (ws *randomWriteScheduler) AdjustStream(streamID uint32, priority PriorityParam) {
  34. // no-op: priorities are ignored
  35. }
  36. func (ws *randomWriteScheduler) Push(wr FrameWriteRequest) {
  37. id := wr.StreamID()
  38. if id == 0 {
  39. ws.zero.push(wr)
  40. return
  41. }
  42. q, ok := ws.sq[id]
  43. if !ok {
  44. q = ws.queuePool.get()
  45. ws.sq[id] = q
  46. }
  47. q.push(wr)
  48. }
  49. func (ws *randomWriteScheduler) Pop() (FrameWriteRequest, bool) {
  50. // Control frames first.
  51. if !ws.zero.empty() {
  52. return ws.zero.shift(), true
  53. }
  54. // Iterate over all non-idle streams until finding one that can be consumed.
  55. for _, q := range ws.sq {
  56. if wr, ok := q.consume(math.MaxInt32); ok {
  57. return wr, true
  58. }
  59. }
  60. return FrameWriteRequest{}, false
  61. }