windows.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. """
  2. h2/windows
  3. ~~~~~~~~~~
  4. Defines tools for managing HTTP/2 flow control windows.
  5. The objects defined in this module are used to automatically manage HTTP/2
  6. flow control windows. Specifically, they keep track of what the size of the
  7. window is, how much data has been consumed from that window, and how much data
  8. the user has already used. It then implements a basic algorithm that attempts
  9. to manage the flow control window without user input, trying to ensure that it
  10. does not emit too many WINDOW_UPDATE frames.
  11. """
  12. from __future__ import annotations
  13. from .exceptions import FlowControlError
  14. # The largest acceptable value for a HTTP/2 flow control window.
  15. LARGEST_FLOW_CONTROL_WINDOW = 2**31 - 1
  16. class WindowManager:
  17. """
  18. A basic HTTP/2 window manager.
  19. :param max_window_size: The maximum size of the flow control window.
  20. :type max_window_size: ``int``
  21. """
  22. def __init__(self, max_window_size: int) -> None:
  23. assert max_window_size <= LARGEST_FLOW_CONTROL_WINDOW
  24. self.max_window_size = max_window_size
  25. self.current_window_size = max_window_size
  26. self._bytes_processed = 0
  27. def window_consumed(self, size: int) -> None:
  28. """
  29. We have received a certain number of bytes from the remote peer. This
  30. necessarily shrinks the flow control window!
  31. :param size: The number of flow controlled bytes we received from the
  32. remote peer.
  33. :type size: ``int``
  34. :returns: Nothing.
  35. :rtype: ``None``
  36. """
  37. self.current_window_size -= size
  38. if self.current_window_size < 0:
  39. msg = "Flow control window shrunk below 0"
  40. raise FlowControlError(msg)
  41. def window_opened(self, size: int) -> None:
  42. """
  43. The flow control window has been incremented, either because of manual
  44. flow control management or because of the user changing the flow
  45. control settings. This can have the effect of increasing what we
  46. consider to be the "maximum" flow control window size.
  47. This does not increase our view of how many bytes have been processed,
  48. only of how much space is in the window.
  49. :param size: The increment to the flow control window we received.
  50. :type size: ``int``
  51. :returns: Nothing
  52. :rtype: ``None``
  53. """
  54. self.current_window_size += size
  55. if self.current_window_size > LARGEST_FLOW_CONTROL_WINDOW:
  56. msg = f"Flow control window mustn't exceed {LARGEST_FLOW_CONTROL_WINDOW}"
  57. raise FlowControlError(msg)
  58. self.max_window_size = max(self.current_window_size, self.max_window_size)
  59. def process_bytes(self, size: int) -> int | None:
  60. """
  61. The application has informed us that it has processed a certain number
  62. of bytes. This may cause us to want to emit a window update frame. If
  63. we do want to emit a window update frame, this method will return the
  64. number of bytes that we should increment the window by.
  65. :param size: The number of flow controlled bytes that the application
  66. has processed.
  67. :type size: ``int``
  68. :returns: The number of bytes to increment the flow control window by,
  69. or ``None``.
  70. :rtype: ``int`` or ``None``
  71. """
  72. self._bytes_processed += size
  73. return self._maybe_update_window()
  74. def _maybe_update_window(self) -> int | None:
  75. """
  76. Run the algorithm.
  77. Our current algorithm can be described like this.
  78. 1. If no bytes have been processed, we immediately return 0. There is
  79. no meaningful way for us to hand space in the window back to the
  80. remote peer, so let's not even try.
  81. 2. If there is no space in the flow control window, and we have
  82. processed at least 1024 bytes (or 1/4 of the window, if the window
  83. is smaller), we will emit a window update frame. This is to avoid
  84. the risk of blocking a stream altogether.
  85. 3. If there is space in the flow control window, and we have processed
  86. at least 1/2 of the window worth of bytes, we will emit a window
  87. update frame. This is to minimise the number of window update frames
  88. we have to emit.
  89. In a healthy system with large flow control windows, this will
  90. irregularly emit WINDOW_UPDATE frames. This prevents us starving the
  91. connection by emitting eleventy bajillion WINDOW_UPDATE frames,
  92. especially in situations where the remote peer is sending a lot of very
  93. small DATA frames.
  94. """
  95. # TODO: Can the window be smaller than 1024 bytes? If not, we can
  96. # streamline this algorithm.
  97. if not self._bytes_processed:
  98. return None
  99. max_increment = (self.max_window_size - self.current_window_size)
  100. increment = 0
  101. # Note that, even though we may increment less than _bytes_processed,
  102. # we still want to set it to zero whenever we emit an increment. This
  103. # is because we'll always increment up to the maximum we can.
  104. if ((self.current_window_size == 0) and (
  105. self._bytes_processed > min(1024, self.max_window_size // 4))) or self._bytes_processed >= (self.max_window_size // 2):
  106. increment = min(self._bytes_processed, max_increment)
  107. self._bytes_processed = 0
  108. self.current_window_size += increment
  109. return increment