writesched_random.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. }