%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /lib/python3/dist-packages/twisted/protocols/__pycache__/
Upload File :
Create Path :
Current File : //lib/python3/dist-packages/twisted/protocols/__pycache__/htb.cpython-312.pyc

�

Ϫ�f�$���dZddlmZddlmZddlmZmZddlmZGd�d�Z	Gd�d	e�Z
ee
�Gd
�d��ZGd�d
e�ZGd�de�Z
Gd�dej�ZGd�de�ZGd�d�Zy)a�
Hierarchical Token Bucket traffic shaping.

Patterned after U{Martin Devera's Hierarchical Token Bucket traffic
shaper for the Linux kernel<http://luxik.cdi.cz/~devik/qos/htb/>}.

@seealso: U{HTB Linux queuing discipline manual - user guide
  <http://luxik.cdi.cz/~devik/qos/htb/manual/userg.htm>}
@seealso: U{Token Bucket Filter in Linux Advanced Routing & Traffic Control
    HOWTO<http://lartc.org/howto/lartc.qdisc.classless.html#AEN682>}
�)�time)�Optional)�	Interface�implementer)�pcpc�R�eZdZUdZdZeeed<dZeeed<dZ	d	d�Z
d�Zd�Zy)
�Bucketa�
    Implementation of a Token bucket.

    A bucket can hold a certain number of tokens and it drains over time.

    @cvar maxburst: The maximum number of tokens that the bucket can
        hold at any given time. If this is L{None}, the bucket has
        an infinite size.
    @type maxburst: C{int}
    @cvar rate: The rate at which the bucket drains, in number
        of tokens per second. If the rate is L{None}, the bucket
        drains instantaneously.
    @type rate: C{int}
    N�maxburst�raterc�>�d|_||_t�|_y)a(
        Create a L{Bucket} that may have a parent L{Bucket}.

        @param parentBucket: If a parent Bucket is specified,
            all L{add} and L{drip} operations on this L{Bucket}
            will be applied on the parent L{Bucket} as well.
        @type parentBucket: L{Bucket}
        rN)�content�parentBucketr�lastDrip)�selfrs  �7/usr/lib/python3/dist-packages/twisted/protocols/htb.py�__init__zBucket.__init__3s�����(������
�c��|j�|j�|}n#t||j|jz
�}|j�|jj|�}|xj|z
c_|S)at
        Adds tokens to the L{Bucket} and its C{parentBucket}.

        This will add as many of the C{amount} tokens as will fit into both
        this L{Bucket} and its C{parentBucket}.

        @param amount: The number of tokens to try to add.
        @type amount: C{int}

        @returns: The number of tokens that actually fit.
        @returntype: C{int}
        )�dripr
�minr
r�add)r�amount�	allowables   rrz
Bucket.add@sk��	
�	�	���=�=� ��I��F�D�M�M�D�L�L�$@�A�I����(��)�)�-�-�i�8�I����	�!���rc�.�|j�|jj�|j�d|_nMt	�}||j
z
}||jz}t
d|j|z
�|_||_|jdk(S)z�
        Let some of the bucket drain.

        The L{Bucket} drains at the rate specified by the class
        variable C{rate}.

        @returns: C{True} if the bucket is empty after this drip.
        @returntype: C{bool}
        r)rrrr
rr�max)r�now�	deltaTime�deltaTokenss    rrzBucket.dripXs������(����"�"�$��9�9���D�L��&�C��d�m�m�+�I�#�d�i�i�/�K��q�$�,�,��"<�=�D�L��D�M��|�|�q� � r�N)
�__name__�
__module__�__qualname__�__doc__r
r�int�__annotations__r�	_refcountrrr�rrr	r	s9��
�#�H�h�s�m�"��D�(�3�-���I���0!rr	c��eZdZd�Zy)�
IBucketFilterc��y)zn
        Return a L{Bucket} corresponding to the provided parameters.

        @returntype: L{Bucket}
        Nr')�
somethings�some_kws  r�getBucketForzIBucketFilter.getBucketForqs�rN)r r!r"r-r'rrr)r)ps��rr)c�D�eZdZUdZeZdZeee	d<dd�Z
d�Zd�Zd�Z
y)	�HierarchicalBucketFilterz�
    Filter things into buckets that can be nested.

    @cvar bucketFactory: Class of buckets to make.
    @type bucketFactory: L{Bucket}
    @cvar sweepInterval: Seconds between sweeping out the bucket cache.
    @type sweepInterval: C{int}
    N�
sweepIntervalc�>�i|_||_t�|_yr)�buckets�parentFilterr�	lastSweep)rr3s  rrz!HierarchicalBucketFilter.__init__�s�����(������rc��|j�4t�|jz
|jkDr|j�|jr!|jj
|g|��i|��}nd}|j|i|��}|jj|�}|� |j|�}||j|<|S)z�
        Find or create a L{Bucket} corresponding to the provided parameters.

        Any parameters are passed on to L{getBucketKey}, from them it
        decides which bucket you get.

        @returntype: L{Bucket}
        N)
r0rr4�sweepr3r-�getBucketKeyr2�get�
bucketFactory)r�a�kwr�key�buckets      rr-z%HierarchicalBucketFilter.getBucketFor�s���
���*�
�V�d�n�n�
$��(:�(:�:��J�J�L����9�4�,�,�9�9�$�I��I�b�I�L��L��d����)�b�)�����!�!�#�&���>��'�'��5�F� &�D�L�L����
rc��y)a%
        Construct a key based on the input parameters to choose a L{Bucket}.

        The default implementation returns the same key for all
        arguments. Override this method to provide L{Bucket} selection.

        @returns: Something to be used as a key in the bucket cache.
        Nr')rr:r;s   rr7z%HierarchicalBucketFilter.getBucketKey�s��rc���|jj�D]5\}}|j�}|jdk(s�&|s�)|j|=�7t	�|_y)z'
        Remove empty buckets.
        rN)r2�itemsrr&rr4)rr<r=�bucket_is_emptys    rr6zHierarchicalBucketFilter.sweep�sV�� �<�<�-�-�/�	&�K�C��$�k�k�m�O�� � �A�%�?��L�L��%�	&�
���rr)r r!r"r#r	r9r0rr$r%rr-r7r6r'rrr/r/ys0����M�#'�M�8�C�=�'� �
�4	�	 rr/c��eZdZdZdZd�Zy)�FilterByHostzF
    A Hierarchical Bucket filter with a L{Bucket} for each host.
    i�c�(�|j�dS�N�)�getPeer�r�	transports  rr7zFilterByHost.getBucketKey����� � �"�1�%�%rN�r r!r"r#r0r7r'rrrCrC�s����M�&rrCc��eZdZdZdZd�Zy)�FilterByServerzI
    A Hierarchical Bucket filter with a L{Bucket} for each service.
    Nc�(�|j�dS)N�)�getHostrHs  rr7zFilterByServer.getBucketKey�rJrrKr'rrrMrM�s����M�&rrMc�&�eZdZdZdZd�Zd�Zd�Zy)�ShapedConsumerzL
    Wraps a C{Consumer} and shapes the rate at which it receives data.
    Fc��tjj||�||_|jxjdz
c_yrE)r�ProducerConsumerProxyrr=r&)r�consumerr=s   rrzShapedConsumer.__init__�s4���!�!�*�*�4��:����������"�rc��|jjt|��}tjj||d|�Sr)r=r�lenrrT�_writeSomeData)r�datars   rrXzShapedConsumer._writeSomeData�s9��
������T��+���(�(�7�7��d�7�F�m�L�Lrc��tjj|�|jxjdzc_yrE)rrT�
stopProducingr=r&)rs rr[zShapedConsumer.stopProducing�s+���!�!�/�/��5�������"�rN)r r!r"r#�iAmStreamingrrXr[r'rrrRrR�s����L�#�
M�#rrRc��eZdZdZdZd�Zy)�ShapedTransportaR
    Wraps a C{Transport} and shapes the rate at which it receives data.

    This is a L{ShapedConsumer} with a little bit of magic to provide for
    the case where the consumer it wraps is also a C{Transport} and people
    will be attempting to access attributes this does not proxy as a
    C{Consumer} (e.g. C{loseConnection}).
    Fc�.�t|j|�Sr)�getattrrU)r�names  r�__getattr__zShapedTransport.__getattr__�s���t�}�}�d�+�+rN)r r!r"r#r\rbr'rrr^r^�s����L�,rr^c��eZdZdZd�Zd�Zy)�ShapedProtocolFactorya�
    Dispense C{Protocols} with traffic shaping on their transports.

    Usage::

        myserver = SomeFactory()
        myserver.protocol = ShapedProtocolFactory(myserver.protocol,
                                                  bucketFilter)

    Where C{SomeServerFactory} is a L{twisted.internet.protocol.Factory}, and
    C{bucketFilter} is an instance of L{HierarchicalBucketFilter}.
    c� �||_||_y)a�
        Tell me what to wrap and where to get buckets.

        @param protoClass: The class of C{Protocol} this will generate
          wrapped instances of.
        @type protoClass: L{Protocol<twisted.internet.interfaces.IProtocol>}
          class
        @param bucketFilter: The filter which will determine how
          traffic is shaped.
        @type bucketFilter: L{HierarchicalBucketFilter}.
        N)�protocol�bucketFilter)r�
protoClassrgs   rrzShapedProtocolFactory.__init__s��#��
�(��rc�`����j|i|��}|j���fd�}||_|S)z�
        Make a C{Protocol} instance with a shaped transport.

        Any parameters will be passed on to the protocol's initializer.

        @returns: A C{Protocol} instance with a L{ShapedTransport}.
        c�b���jj|�}t||�}�|�Sr)rgr-r^)rIr=�shapedTransport�origMakeConnectionrs   ��r�makeConnectionz6ShapedProtocolFactory.__call__.<locals>.makeConnection,s0����&�&�3�3�I�>�F�-�i��@�O�%�o�6�6r)rfrm)rr:r;�protormrls`    @r�__call__zShapedProtocolFactory.__call__!s:�����
�
�q�'�B�'��"�1�1��	7�
 .����rN)r r!r"r#rror'rrrdrds���)�"rrdN)r#r�typingr�zope.interfacerr�twisted.protocolsrr	r)r/rCrMrTrRr^rdr'rr�<module>rss���
� ��1�!�O!�O!�d�I��
�]��@ �@ ��@ �F&�+�&�&�-�&�#�S�.�.�#�6,�n�,�(0�0r

Zerion Mini Shell 1.0