
    g                     8    d Z ddlmZ ddlmZ dZ G d d      Zy)	a  
h2/windows
~~~~~~~~~~

Defines tools for managing HTTP/2 flow control windows.

The objects defined in this module are used to automatically manage HTTP/2
flow control windows. Specifically, they keep track of what the size of the
window is, how much data has been consumed from that window, and how much data
the user has already used. It then implements a basic algorithm that attempts
to manage the flow control window without user input, trying to ensure that it
does not emit too many WINDOW_UPDATE frames.
    )division   )FlowControlErroric                   .    e Zd ZdZd Zd Zd Zd Zd Zy)WindowManagerz
    A basic HTTP/2 window manager.

    :param max_window_size: The maximum size of the flow control window.
    :type max_window_size: ``int``
    c                 D    |t         k  sJ || _        || _        d| _        y )Nr   )LARGEST_FLOW_CONTROL_WINDOWmax_window_sizecurrent_window_size_bytes_processed)selfr
   s     ?/var/www/openai/venv/lib/python3.12/site-packages/h2/windows.py__init__zWindowManager.__init__   s)    "====.#2  !    c                 b    | xj                   |z  c_         | j                   dk  rt        d      y)aD  
        We have received a certain number of bytes from the remote peer. This
        necessarily shrinks the flow control window!

        :param size: The number of flow controlled bytes we received from the
            remote peer.
        :type size: ``int``
        :returns: Nothing.
        :rtype: ``None``
        r   z"Flow control window shrunk below 0N)r   r   r   sizes     r   window_consumedzWindowManager.window_consumed%   s4     	  D( ##a'"#GHH (r   c                     | xj                   |z  c_         | j                   t        kD  rt        dt        z        | j                   | j                  kD  r| j                   | _        yy)aE  
        The flow control window has been incremented, either because of manual
        flow control management or because of the user changing the flow
        control settings. This can have the effect of increasing what we
        consider to be the "maximum" flow control window size.

        This does not increase our view of how many bytes have been processed,
        only of how much space is in the window.

        :param size: The increment to the flow control window we received.
        :type size: ``int``
        :returns: Nothing
        :rtype: ``None``
        z%Flow control window mustn't exceed %dN)r   r	   r   r
   r   s     r   window_openedzWindowManager.window_opened4   se     	  D( ##&AA"7+, 
 ##d&:&::#'#;#;D  ;r   c                 L    | xj                   |z  c_         | j                         S )aF  
        The application has informed us that it has processed a certain number
        of bytes. This may cause us to want to emit a window update frame. If
        we do want to emit a window update frame, this method will return the
        number of bytes that we should increment the window by.

        :param size: The number of flow controlled bytes that the application
            has processed.
        :type size: ``int``
        :returns: The number of bytes to increment the flow control window by,
            or ``None``.
        :rtype: ``int`` or ``None``
        )r   _maybe_update_windowr   s     r   process_byteszWindowManager.process_bytesN   s$     	%((**r   c                    | j                   sy| j                  | j                  z
  }d}| j                  dk(  rD| j                   t        d| j                  dz        kD  rt        | j                   |      }d| _         n9| j                   | j                  dz  k\  rt        | j                   |      }d| _         | xj                  |z  c_        |S )a  
        Run the algorithm.

        Our current algorithm can be described like this.

        1. If no bytes have been processed, we immediately return 0. There is
           no meaningful way for us to hand space in the window back to the
           remote peer, so let's not even try.
        2. If there is no space in the flow control window, and we have
           processed at least 1024 bytes (or 1/4 of the window, if the window
           is smaller), we will emit a window update frame. This is to avoid
           the risk of blocking a stream altogether.
        3. If there is space in the flow control window, and we have processed
           at least 1/2 of the window worth of bytes, we will emit a window
           update frame. This is to minimise the number of window update frames
           we have to emit.

        In a healthy system with large flow control windows, this will
        irregularly emit WINDOW_UPDATE frames. This prevents us starving the
        connection by emitting eleventy bajillion WINDOW_UPDATE frames,
        especially in situations where the remote peer is sending a lot of very
        small DATA frames.
        Nr   i         )r   r
   r   min)r   max_increment	increments      r   r   z"WindowManager._maybe_update_window_   s    4 $$--0H0HH	
 $$)%%D$2F2F!2K(LLD11=AI$%D!""t';';q'@AD11=AI$%D!  I- r   N)	__name__
__module____qualname____doc__r   r   r   r   r    r   r   r   r      s!    "I<4+",r   r   N)r#   
__future__r   
exceptionsr   r	   r   r$   r   r   <module>r'      s&      ( ( s sr   